The main page, index.html
, is served dynamicly with MyAction
.
The rest of the Web site fetches files from the local directory
www/hello_world/
.
FileServer
action, which is a standard and minimal file server
HttpRequest
class and services to create it
Serializable::inspect
to show more useful information
more_collections :: more_collections
Highly specific, but useful, collections-related classes.serialization :: serialization_core
Abstract services to serialize Nit objects to different formatscore :: union_find
union–find algorithm using an efficient disjoint-set data structure
# Hello World Web server example
#
# The main page, `index.html`, is served dynamicly with `MyAction`.
# The rest of the Web site fetches files from the local directory
# `www/hello_world/`.
module nitcorn_hello_world is example
import nitcorn
# An action that responds by displaying a static html content.
class StaticAction
super Action
redef fun answer(http_request, turi)
do
var response = new HttpResponse(200)
var title = "Hello World from Nitcorn!"
response.body = """
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<title>{{{title}}}</title>
</head>
<body>
<div class="container">
<h1>{{{title}}}</h1>
<p>See also a <a href="/dir/">directory</a>.</p>
</div>
</body>
</html>"""
return response
end
end
# An action that uses parameterized uris to customize the output.
class ParamAction
super Action
redef fun answer(http_request, turi)
do
var response = new HttpResponse(200)
var name = http_request.param("name")
if name == null then
response.body = "No name..."
else
response.body = "Hello {name}"
end
return response
end
end
var vh = new VirtualHost("localhost:8080")
# Serve index.html with our custom handler
vh.routes.add new Route("/index.html", new StaticAction)
vh.routes.add new Route("/hello/:name", new ParamAction)
# Serve everything else with a standard `FileServer` with a root at "www/hello_world/"
vh.routes.add new Route(null, new FileServer("www/hello_world/"))
var factory = new HttpFactory.and_libevent
factory.config.virtual_hosts.add vh
factory.run
lib/nitcorn/examples/src/nitcorn_hello_world.nit:17,1--82,11