Dynamic content is served by subclassing Action
and implementing answer
.
This method receives an HttpRequest
and must return an HttpResponse
.
nitcorn provides FileServer
, a simple Action
to serve static files.
HttpRequest
contains the GET and POST arguments as well as session data it one exists.
The produced HttpResponse
should contain the HTTP status code, the body,
session data to preserve or create a session, and optionally list files to append.
Each Action
may be associated to many instances of Route
.
These routes can simply identify the root of a service,
but also define parameters within the URI.
nitcorn instances are configured dynamically in Nit code with the interfaces and routes created as needed.
nitcorn plays well with other Nit services and tools such as serialization
, mongodb
, sqlite
and nitiwiki
.
It also benefits from the full power of the Nit language:
class refinement can be used to customize default services and merge many applications in a single server,
and the FFI enables calling services in different languages.
A minimal example follows with a custom Action
and using FileServer
.
More general examples are available at lib/nitcorn/examples/
.
For an example of a larger project merging many nitcorn applications into one server,
take a look at the configuration of http://xymus.net/
at ../contrib/xymus_net/xymus_net.nit
.
Larger projects using nitcorn can be found in the contrib/
folder:
Action
to subscribe people to a mailing list and define a RESTful interface.import nitcorn
# Simple Action to display the Hello World page and the get arguments
class HelloWorldAction
super Action
redef fun answer(http_request, turi)
do
var title = "Hello World!"
var args = http_request.get_args.join(",", ":")
var response = new HttpResponse(200)
response.body = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{{title}}}</title>
</head>
<body>
<h1>{{{title}}}</h1>
<p>GET args: {{{args}}}</p>
</body>
</html>"""
return response
end
end
# Listen on `localhost:8080`
var vh = new VirtualHost("localhost:8080")
# Serve `http://localhost:8080/hello.html` with our custom action
vh.routes.add new Route("/hello.html", new HelloWorldAction)
# Serve everything else under `http://localhost:8080/` using a `FileServer` with a root at "/var/www/"
vh.routes.add new Route(null, new FileServer("/var/www/"))
# Launch server
var factory = new HttpFactory.and_libevent
factory.config.virtual_hosts.add vh
factory.run
This nitcorn library is a fork from an independent project originally created in 2013 by Jean-Philippe Caissy, Guillaume Auger, Frederic Sevillano, Justin Michaud-Ouellette, Stephan Michaud and Maxime Bélanger.
It has been adapted to a library, and is currently maintained, by Alexis Laferrière.
FileServer
action, which is a standard and minimal file server
HttpRequest
class and services to create it