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