1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Hello World Web server example
19 # The main page, `index.html`, is served dynamicly with `MyAction`.
20 # The rest of the Web site fetches files from the local directory
22 module nitcorn_hello_world
26 # An action that responds by displaying a static html content.
30 redef fun answer
(http_request
, turi
)
32 var response
= new HttpResponse(200)
33 var title
= "Hello World from Nitcorn!"
37 <meta charset="utf-8">
38 <meta http-equiv="X-UA-Compatible" content="IE=edge">
39 <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
40 <title>{{{title}}}</title>
43 <div class="container">
45 <p>See also a <a href="/dir/">directory</a>.</p>
53 # An action that uses parameterized uris to customize the output.
57 redef fun answer
(http_request
, turi
)
59 var response
= new HttpResponse(200)
60 var name
= http_request
.param
("name")
62 response
.body
= "No name..."
64 response
.body
= "Hello {name}"
71 var vh
= new VirtualHost("localhost:8080")
73 # Serve index.html with our custom handler
74 vh
.routes
.add
new Route("/index.html", new StaticAction)
75 vh
.routes
.add
new Route("/hello/:name", new ParamAction)
77 # Serve everything else with a standard `FileServer` with a root at "www/hello_world/"
78 vh
.routes
.add
new Route(null, new FileServer("www/hello_world/"))
80 # Avoid executing when running tests
81 if "NIT_TESTING".environ
== "true" then exit
0
83 var factory
= new HttpFactory.and_libevent
84 factory
.config
.virtual_hosts
.add vh