|
| 1 | +# CRut, a ReST application framework |
| 2 | +CRut is web application framework for services written in C. |
| 3 | +It is realized so to save the more resources as possible and to be run on Internet-of-Things devices. |
| 4 | +It has been developed for the [PeerStreamer-ng](http://peerstreamer.eu) web application, a service for real-time P2P streaming. |
| 5 | + |
| 6 | +Developers are asked to define and couple HTTP request patterns (i.e., URI) and methods. |
| 7 | +CRut also serves the files placed in the _Public/_ folder. |
| 8 | + |
| 9 | +By default, CRut runs on port 3000. |
| 10 | + |
| 11 | +## Compile and run |
| 12 | +``` |
| 13 | +make |
| 14 | +./crut |
| 15 | +``` |
| 16 | +Point your web browser at _http://localhost:3000_. |
| 17 | + |
| 18 | +## Create your routes |
| 19 | +The HTTP request patters comprise of |
| 20 | + |
| 21 | + * an HTTP method; |
| 22 | + * a regex for the URL. |
| 23 | + |
| 24 | +HTTP methods are: GET, PUT, POST, UPDATE, DELETE, OPTION, HEAD. |
| 25 | +Regex allows the specification of a broad range of HTTP resource patters; for example, if you want to match the resource with URL _/channels/BBC_, you can write: "/channels/[A-z0-9]+$". |
| 26 | +The previous regex will match all the URLs of the form _/channels/<id>_ where _<id>_ is any string comprising of lower and upper case letters and numbers. |
| 27 | + |
| 28 | +A path handler is a C function pointer with type |
| 29 | +``` |
| 30 | +void (*path_handler)(struct mg_connection *nc, struct http_message *hm); |
| 31 | +``` |
| 32 | + |
| 33 | +A route is the triple __HTTP method, URL regex, path handler__. |
| 34 | +Routes are defined so to react to specific HTTP requests with some C code. |
| 35 | +Routes can be defined in the _src/routes.c_ file with the following line: |
| 36 | +``` |
| 37 | +res |= router_add_route(r, "POST", "^[/]+channels/[A-z0-9]+$", path_handler); |
| 38 | +``` |
| 39 | +where _res_ and _r_ are scope specific variable to be used as-is. |
| 40 | + |
| 41 | +## Define your handlers |
| 42 | +Defining a path handler is easy as opening _src/path_handlers.c_ and writing: |
| 43 | +``` |
| 44 | +void path_handler(struct mg_connection *nc, struct http_message *hm) |
| 45 | +{ |
| 46 | + mg_http_short_answer(nc, 200); |
| 47 | +} |
| 48 | +``` |
| 49 | +The example code above will answer to incoming HTTP requests with a polite "HTTP 200 OK" message. |
| 50 | + |
| 51 | +Handlers can take advantage from some helpers for getting request variable values; |
| 52 | + |
| 53 | + * mg_uri_field(hm, pos) returns a char array pointer with the value of the URI field specified by _pos_; for example, for the request _/channels/BBC_, with _pos=1_ it will return "BBC". |
| 54 | + * mg_get_http_var(&hm->body, "data", buff, BUFF_LENGTH) stores in the buffer pointed by _buff_ the value of the HTTP variable "data" encoded in the request. |
| 55 | + |
| 56 | +The helpers also include some functions for answering: |
| 57 | + |
| 58 | + * mg_http_short_answer(nc, code); will send the HTTP request with the status code _code_ and the brief corresponding message; |
| 59 | + * mg_http_text_answer(nc, code, body); adds the possibility to also specify the response message; |
| 60 | + * mg_http_json_answer(nc, code, body); adds the possibility to specify a JSON body (it automatically set the Content-type header). |
| 61 | + * mg_http_answer(nc, code, content_type, body); sends an HTTP requests setting the content type and the response body. |
| 62 | + |
| 63 | +## SSL |
| 64 | +CRut supports SSL, just enable it at compilation time: |
| 65 | +``` |
| 66 | +ENABLE_SSL=1 make |
| 67 | +``` |
| 68 | + |
| 69 | +## Tests |
| 70 | +CRut comes with a unit-test suite (in _/Test_). |
| 71 | +To run all of them at once type: |
| 72 | +``` |
| 73 | +make tests |
| 74 | +``` |
| 75 | +It requires [valgrind](http://valgrind.org/) to be installed and present in the PATH. |
| 76 | + |
| 77 | +CRut can also be compiled in debug mode for enabling extra output: |
| 78 | +``` |
| 79 | +DEBUG=1 make |
| 80 | +``` |
| 81 | + |
| 82 | +## Tutorials |
| 83 | +A [tutorial](https://ans.disi.unitn.it/users/baldesi//programming/c/rest/raspberry-pi/web/crut/2019/02/13/web-interface-for-raspberry-pi-gpio.html) about using CRut to create a web app turning Raspberry pi leds on and off is avaiable. |
0 commit comments