6bc8b36c1d8727ea92caed0ee6751db6a6d20403
[nit.git] / lib / nitcorn / README.md
1 Lightweight framework for Web applications development
2
3 # Features
4
5 Dynamic content is served by subclassing `Action` and implementing `answer`.
6 This method receives an `HttpRequest` and must return an `HttpResponse`.
7 _nitcorn_ provides `FileServer`, a simple `Action` to serve static files.
8
9 `HttpRequest` contains the GET and POST arguments as well as session data it one exists.
10 The produced `HttpResponse` should contain the HTTP status code, the body,
11 session data to preserve or create a session, and optionally list files to append.
12
13 Each `Action` may be associated to many instances of `Route`.
14 These routes can simply identify the root of a service,
15 but also define parameters within the URI.
16
17 _nitcorn_ instances are configured dynamically in Nit code with the interfaces and routes created as needed.
18
19 _nitcorn_ plays well with other Nit services and tools such as `serialization`, `mongodb`, `sqlite` and `nitiwiki`.
20 It also benefits from the full power of the Nit language:
21 class refinement can be used to customize default services and merge many applications in a single server,
22 and the FFI enables calling services in different languages.
23
24 # Examples
25
26 A minimal example follows with a custom `Action` and using `FileServer`.
27
28 More general examples are available at `lib/nitcorn/examples/`.
29 It includes the configuration of `http://xymus.net/` which merges many other _nitcorn_ applications.
30
31 Larger projects using _nitcorn_ can be found in the `contrib/` folder:
32 * _opportunity_ is a meetup planner heavily based on _nitcorn_.
33 * _tnitter_ is a micro-blogging platform with a simple Web and RESTful interface.
34 * _benitlux_ uses a custom `Action` to subscribe people to a mailing list and define a RESTful interface.
35
36 ## Simple hello world server
37
38 ~~~
39 import nitcorn
40
41 # Simple Action to display the Hello World page and the get arguments
42 class HelloWorldAction
43         super Action
44
45         redef fun answer(http_request, turi)
46         do
47                 var title = "Hello World!"
48                 var args = http_request.get_args.join(",", ":")
49
50                 var response = new HttpResponse(200)
51                 response.body = """
52 <!DOCTYPE html>
53 <html>
54 <head>
55         <meta charset="utf-8">
56         <title>{{{title}}}</title>
57 </head>
58 <body>
59         <h1>{{{title}}}</h1>
60         <p>GET args: {{{args}}}</p>
61 </body>
62 </html>"""
63                 return response
64         end
65 end
66
67 # Listen on `localhost:8080`
68 var vh = new VirtualHost("localhost:8080")
69
70 # Serve `http://localhost:8080/hello.html` with our custom action
71 vh.routes.add new Route("/hello.html", new HelloWorldAction)
72
73 # Serve everything else under `http://localhost:8080/` using a `FileServer` with a root at "/var/www/"
74 vh.routes.add new Route(null, new FileServer("/var/www/"))
75
76 # Launch server
77 var factory = new HttpFactory.and_libevent
78 factory.config.virtual_hosts.add vh
79 factory.run
80 ~~~
81
82 # Credits
83
84 This nitcorn library is a fork from an independent project originally created in 2013 by
85 Jean-Philippe Caissy, Guillaume Auger, Frederic Sevillano, Justin Michaud-Ouellette,
86 Stephan Michaud and Maxime Bélanger.
87
88 It has been adapted to a library, and is currently maintained, by Alexis Laferrière.