examples: annotate examples
[nit.git] / contrib / shibuqam / examples / reloadgame.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Example that uses `shibuqam` to authenticate users and count the number of time they reload.
16 module reloadgame is example
17
18 import popcorn
19 import counter
20 import shibuqam
21
22 redef class User
23 # How many reload?
24 var seen = 0
25 end
26
27 # Ugly global class to track the knowledge.
28 class DB
29 # All known users
30 var users = new HashMap[String, User]
31 end
32 # Ugly global instance to track the knowledge.
33 fun db: DB do return once new DB
34
35 redef class HttpRequest
36 # Like `user` but reuse an user if already seen
37 var reuser: nullable User is lazy do
38 var user = self.user
39 if user == null then return null
40
41 var saved = db.users.get_or_null(user.id)
42 if saved != null then return saved
43
44 db.users[user.id] = user
45 return user
46 end
47 end
48
49 # The only handler of the example.
50 class ReloadGame
51 super Handler
52
53 redef fun get(http_request, response)
54 do
55 var body = """
56 <!DOCTYPE html>
57 <head>
58 <meta charset="utf-8">
59 <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
60 <title>Nitcorn on Shibboleth/UQAM</title>
61 </head>
62 <body>
63 <div class="container">
64 <h1>Nitcorn on Shibboleth/UQAM</h1>
65 """
66
67 var user = http_request.user
68
69 if user != null then
70 user.seen += 1
71
72 body += """
73 <p>Welcome {{{user.given_name}}}</p>
74 <ul>
75 <li>Full Name: {{{user.display_name}}}</li>
76 <li>E-Mail: {{{user.email}}}</li>
77 <li>Id: {{{user.id}}}</li>
78 <li>Score: {{{user.seen}}}</li>
79 </ul>
80 """
81
82 #for k, v in http_request.header do body += "<li>{k}: {v}</li>"
83 else
84 # The login page, at the location the reverse proxy is expected to be configured
85 # to force an authentication.
86 var login = "/securep/login"
87 body += """
88 <p>Welcome annonymous, please <a href="{{{login}}}">log in</a>.</p>
89 """
90 end
91
92 var score = new Counter[User]
93 for u in db.users.values do
94 score[u] = u.seen
95 end
96
97 body += "<h2>Scoreboard</h2><ul>"
98 for u in score.sort.reversed do
99 body += "<li><img src='{u.avatar}'> {u.display_name}: {u.seen}</li>"
100 end
101
102
103 body += """</ul>
104 </div>
105 </body>
106 </html>
107 """
108
109 response.html body
110 end
111 end
112
113 var app = new App
114 app.use("/*", new ReloadGame)
115 app.listen("localhost", 3000)