nitcorn -
Lightweight framework for Web applications development
Features
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.
Examples
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:
- opportunity is a meetup planner heavily based on nitcorn.
- tnitter is a micro-blogging platform with a simple Web and RESTful interface.
- benitlux uses a custom
Action
to subscribe people to a mailing list and define a RESTful interface.
Simple hello world server
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
Credits
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.
Content
- nitcorn: Lightweight framework for Web applications development (lib/nitcorn)
- examples (lib/nitcorn/examples)
- src (lib/nitcorn/examples/src)
- htcpcp_server: A server that implements HTCPCP. At the moment there are no additions. (lib/nitcorn/examples/src/htcpcp_server.nit)
- nitcorn_hello_world: Hello World Web server example (lib/nitcorn/examples/src/nitcorn_hello_world.nit)
- nitcorn_reverse_proxy: Minimal example using a
ProxyAction
(lib/nitcorn/examples/src/nitcorn_reverse_proxy.nit) - restful_annot: Example for the
restful
annotation documented atlib/nitcorn/restful.nit
(lib/nitcorn/examples/src/restful_annot.nit) - simple_file_server: Basic file server on port 80 by default, may require
root
to execute (lib/nitcorn/examples/src/simple_file_server.nit) - test_restful_annot (lib/nitcorn/examples/src/test_restful_annot.nit)
- www (lib/nitcorn/examples/www)
- hello_world (lib/nitcorn/examples/www/hello_world)
- src (lib/nitcorn/examples/src)
- file_server: Provides the
FileServer
action, which is a standard and minimal file server (lib/nitcorn/file_server.nit) - http_errors: Offers
ErrorTemplate
to display error pages (lib/nitcorn/http_errors.nit) - http_request: Provides the
HttpRequest
class and services to create it (lib/nitcorn/http_request.nit) - http_request_buffer: Http request parsing for buffered inputs. (lib/nitcorn/http_request_buffer.nit)
- http_response: Provides the
HttpResponse
class andhttp_status_codes
(lib/nitcorn/http_response.nit) - log: Services inserting a timestamp in all prints and to log each requests (lib/nitcorn/log.nit)
- media_types: Services to identify Internet media types (or MIME types, Content-types) (lib/nitcorn/media_types.nit)
- nitcorn: The nitcorn Web server framework creates server-side Web apps in Nit (lib/nitcorn/nitcorn.nit)
- proxy: Provides the
ProxyAction
action, which redirects requests to another interface (lib/nitcorn/proxy.nit) - pthreads: Activate the use of pthreads with
nitcorn
(lib/nitcorn/pthreads.nit) - reactor: Core of the
nitcorn
project, providesHttpFactory
andAction
(lib/nitcorn/reactor.nit) - restful: Support module for the
nitrestful
tool and therestful
annotation (lib/nitcorn/restful.nit) - server_config: Classes and services to configure the server (lib/nitcorn/server_config.nit)
- sessions: Automated session management (lib/nitcorn/sessions.nit)
- signal_handler: Handle SIGINT and SIGTERM to close the server after all active events (lib/nitcorn/signal_handler.nit)
- token: Simple
generate_token
service, independent of the rest of the nitcorn framework (lib/nitcorn/token.nit) - vararg_routes: Routes with parameters. (lib/nitcorn/vararg_routes.nit)
- examples (lib/nitcorn/examples)