Merge: doc: fixed some typos and other misc. corrections
[nit.git] / lib / popcorn / pop_sessions.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Session handlers
18 #
19 # Here a simple example on how to use sessions with popcorn:
20 # ~~~
21 # import popcorn
22 #
23 # redef class Session
24 # var is_logged = false
25 # end
26 #
27 # class AppLogin
28 # super Handler
29 #
30 # redef fun get(req, res) do
31 # res.html """
32 # <p>Is logged: {{{req.session.as(not null).is_logged}}}</p>
33 # <form action="/" method="POST">
34 # <input type="submit" value="Login" />
35 # </form>"""
36 # end
37 #
38 # redef fun post(req, res) do
39 # req.session.as(not null).is_logged = true
40 # res.redirect("/")
41 # end
42 # end
43 #
44 # var app = new App
45 # app.use_before("/*", new SessionInit)
46 # app.use("/", new AppLogin)
47 # app.listen("localhost", 3000)
48 # ~~~
49 #
50 # Notice the use of the `SessionInit` on the `/*` route. You must use the
51 # `SessionInit` first to initialize the request session.
52 # Without that, your request session will be set to `null`.
53 module pop_sessions
54
55 import pop_handlers
56
57 # Initialize session in request if non existent.
58 #
59 # Should be called before any use of the session.
60 # ~~~
61 # import popcorn
62 #
63 # var app = new App
64 # app.use_before("/*", new SessionInit)
65 # # ... other middlewares
66 # app.listen("localhost", 3000)
67 # ~~~
68 class SessionInit
69 super Handler
70
71 redef fun all(req, res) do if req.session == null then req.session = new Session
72 end