# This file is part of NIT ( http://www.nitlanguage.org ). # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Example that uses `shibuqam` to authenticate users and count the number of time they reload. module reloadgame is example import popcorn import counter import shibuqam redef class User # How many reload? var seen = 0 end # Ugly global class to track the knowledge. class DB # All known users var users = new HashMap[String, User] end # Ugly global instance to track the knowledge. fun db: DB do return once new DB redef class HttpRequest # Like `user` but reuse an user if already seen var reuser: nullable User is lazy do var user = self.user if user == null then return null var saved = db.users.get_or_null(user.id) if saved != null then return saved db.users[user.id] = user return user end end # The only handler of the example. class ReloadGame super Handler redef fun get(http_request, response) do var body = """ Nitcorn on Shibboleth/UQAM

Nitcorn on Shibboleth/UQAM

""" var user = http_request.user if user != null then user.seen += 1 body += """

Welcome {{{user.given_name}}}

""" #for k, v in http_request.header do body += "
  • {k}: {v}
  • " else # The login page, at the location the reverse proxy is expected to be configured # to force an authentication. var login = "/securep/login" body += """

    Welcome annonymous, please log in.

    """ end var score = new Counter[User] for u in db.users.values do score[u] = u.seen end body += "

    Scoreboard

    """ response.html body end end var app = new App app.use("/*", new ReloadGame) app.listen("localhost", 3000)