1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Example that uses `shibuqam` to authenticate users and count the number of time they reload.
16 module reloadgame
is example
27 # Ugly global class to track the knowledge.
30 var users
= new HashMap[String, User]
32 # Ugly global instance to track the knowledge.
33 fun db
: DB do return once
new DB
35 redef class HttpRequest
36 # Like `user` but reuse an user if already seen
37 var reuser
: nullable User is lazy
do
39 if user
== null then return null
41 var saved
= db
.users
.get_or_null
(user
.id
)
42 if saved
!= null then return saved
44 db
.users
[user
.id
] = user
49 # The only handler of the example.
53 redef fun get
(http_request
, response
)
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>
63 <div class="container">
64 <h1>Nitcorn on Shibboleth/UQAM</h1>
67 var user
= http_request
.user
73 <p>Welcome {{{user.given_name}}}</p>
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>
82 #for k, v in http_request.header do body += "<li>{k}: {v}</li>"
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"
88 <p>Welcome annonymous, please <a href="{{{login}}}">log in</a>.</p>
92 var score
= new Counter[User]
93 for u
in db
.users
.values
do
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>"
114 app
.use
("/*", new ReloadGame)
115 app
.listen
("localhost", 3000)