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.

All subgroups and modules

module file_server

nitcorn :: file_server

Provides the FileServer action, which is a standard and minimal file server
module http_errors

nitcorn :: http_errors

Offers ErrorTemplate to display error pages
module http_request

nitcorn :: http_request

Provides the HttpRequest class and services to create it
module http_request_buffer

nitcorn :: http_request_buffer

Http request parsing for buffered inputs.
module http_response

nitcorn :: http_response

Provides the HttpResponse class and http_status_codes
module log

nitcorn :: log

Services inserting a timestamp in all prints and to log each requests
module media_types

nitcorn :: media_types

Services to identify Internet media types (or MIME types, Content-types)
module nitcorn

nitcorn :: nitcorn

The nitcorn Web server framework creates server-side Web apps in Nit
module proxy

nitcorn :: proxy

Provides the ProxyAction action, which redirects requests to another interface
module pthreads

nitcorn :: pthreads

Activate the use of pthreads with nitcorn
module reactor

nitcorn :: reactor

Core of the nitcorn project, provides HttpFactory and Action
module restful

nitcorn :: restful

Support module for the nitrestful tool and the restful annotation
module server_config

nitcorn :: server_config

Classes and services to configure the server
module sessions

nitcorn :: sessions

Automated session management
module signal_handler

nitcorn :: signal_handler

Handle SIGINT and SIGTERM to close the server after all active events
module token

nitcorn :: token

Simple generate_token service, independent of the rest of the nitcorn framework
module vararg_routes

nitcorn :: vararg_routes

Routes with parameters.
package_diagram nitcorn\> nitcorn ...nitcorn\> ... ...nitcorn\>->nitcorn\> github github github->nitcorn\> nitcorn\>examples\>src\> src nitcorn\>examples\>src\>->nitcorn\> popcorn popcorn popcorn->nitcorn\> popcorn->github popcorn... ... popcorn...->popcorn

Ancestors

group codecs

core > codecs

Group module for all codec-related manipulations
group collection

core > collection

This module define several collection classes.
group meta

meta

Simple user-defined meta-level to manipulate types of instances as object.
group parser_base

parser_base

Simple base for hand-made parsers of all kinds
group poset

poset

Pre order sets and partial order set (ie hierarchies)
group realtime

realtime

Services to keep time of the wall clock time
group text

core > text

All the classes and methods related to the manipulation of text entities

Parents

group base64

base64

Offers the base 64 encoding and decoding algorithms
group core

core

Nit common library of core classes and methods
group curl

curl

Data transfer powered by the native curl library
group json

json

read and write JSON formatted text
group libevent

libevent

Low-level wrapper around the libevent library to manage events on file descriptors
group md5

md5

Native MD5 digest implementation as Text::md5
group more_collections

more_collections

Highly specific, but useful, collections-related classes.
group performance_analysis

performance_analysis

Services to gather information on the performance of events by categories
group pthreads

pthreads

POSIX Threads support
group serialization

serialization

Abstract serialization services
group template

template

Basic template system

Children

group github

github

Nit wrapper for Github API
group popcorn

popcorn

Popcorn
group src

nitcorn > examples > src

Descendants