scope: refuse `&x` where x is a local variable
[nit.git] / lib / popcorn / popcorn.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 # Application server abstraction on top of nitcorn.
18 module popcorn
19
20 import nitcorn
21 import pop_tasks
22 import pop_sessions
23 import pop_logging
24 intrude import pop_handlers
25
26 # App acts like a wrapper around a nitcorn `Action`.
27 redef class App
28 super Action
29
30 # Do not show anything on console
31 var quiet = false is writable
32
33 # Start listening on `host:port`.
34 fun listen(host: String, port: Int) do
35 var iface = "{host}:{port}"
36 var vh = new VirtualHost(iface)
37
38 vh.routes.add new Route("/", self)
39
40 var fac = new HttpFactory.and_libevent
41 fac.config.virtual_hosts.add vh
42
43 if not quiet then
44 print "Launching server on http://{iface}/"
45 end
46 fac.run
47 end
48
49 # Handle request from nitcorn
50 redef fun answer(req, uri) do
51 uri = uri.simplify_path
52 var res = new HttpResponse(404)
53 for route, handler in pre_handlers do
54 handler.handle(route, uri, req, res)
55 end
56 for route, handler in handlers do
57 handler.handle(route, uri, req, res)
58 if res.sent then break
59 end
60 if not res.sent then
61 res.send(error_tpl(res.status_code, res.status_message), 404)
62 end
63 for route, handler in post_handlers do
64 handler.handle(route, uri, req, res)
65 end
66 res.session = req.session
67 return res
68 end
69
70 #
71 fun error_tpl(status: Int, message: nullable String): Template do
72 return new ErrorTpl(status, message)
73 end
74 end
75
76 #
77 class ErrorTpl
78 super Template
79
80 #
81 var status: Int
82
83 #
84 var message: nullable String
85
86 redef fun rendering do add """
87 <!DOCTYPE html>
88 <html>
89 <head>
90 <meta charset="utf-8">
91 <title>{{{message or else status}}}</title>
92 </head>
93 <body>
94 <h1>{{{status}}} {{{message or else ""}}}</h1>
95 </body>
96 </html>"""
97
98 end