Lwan is a high-performance & scalable web server for glibc/Linux platforms. For more information about this project, visit their official website.
This example creates a very simple web service which greets you by your name.
This example is already in biicode, it’s simple to run, just open the block and build it like this:
$ bii init lwan
$ cd lwan
$ bii open examples/lwan
$ bii build
This example needs three files, first one that defines the server configuration:
lwan.conf
1 2 3 4 5 | listener *:8080 {
prefix /hello {
handler = hello_world
}
}
|
The actual soruce code:
lwan_example.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include "hithwen/lwan/common/lwan.h"
lwan_http_status_t
hello_world(lwan_request_t *request,
lwan_response_t *response,
void *data __attribute__((unused)))
{
static lwan_key_value_t headers[] = {
{ .key = "X-The-Answer-To-The-Universal-Question", .value = "42" },
{ NULL, NULL }
};
response->headers = headers;
response->mime_type = "text/plain";
const char *name = lwan_request_get_query_param(request, "name");
if (name)
strbuf_printf(response->buffer, "Hello, %s!", name);
else
strbuf_set_static(response->buffer, "Hello, world!", sizeof("Hello, world!") -1);
return HTTP_OK;
}
int
main(void)
{
lwan_t l;
lwan_init(&l);
lwan_main_loop(&l);
lwan_shutdown(&l);
return 0;
}
|
And the Cmake lists that copies config file to bin folder:
CMakeLists.txt
1 2 3 4 5 6 7 | include(${CMAKE_HOME_DIRECTORY}/biicode.cmake)
ADD_BII_TARGETS()
MESSAGE("Copying lwan.conf to ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
FILE(COPY ${CMAKE_CURRENT_SOURCE_DIR}/lwan.conf DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
FILE(RENAME ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/lwan.conf ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/examples_lwan_lwan_example.conf)
|
Now, run the hello example.
$ cd bin
$ ./examples_lwan_lwan_example
You can see the server running:
62528 lwan-job.c:76 lwan_job_thread_init() Initializing low priority job thread.
62528 lwan-response.c:75 lwan_response_init() Initializing default response.
62528 lwan-tables.c:44 lwan_tables_init() Uncompressing MIME type table.
62528 lwan.c:58 lwan_module_init() Initializing module registry.
62528 lwan.c:73 lwan_module_register() Registering module "serve_files".
62528 lwan.c:73 lwan_module_register() Registering module "redirect".
62528 lwan.c:360 setup_from_config() Loading configuration file: examples_lwan_lwan_example.conf.
62528 lwan.c:476 lwan_init() Initializing lwan web server.
62528 lwan.c:487 lwan_init() Using 2 threads, maximum 2048 sockets per thread.
62528 lwan-thread.c:393 lwan_thread_init() Initializing threads.
62528 lwan-socket.c:212 lwan_socket_init() Initializing sockets.
62528 lwan-socket.c:197 _setup_socket_normally() Listening on http://0.0.0.0:8080.
62528 lwan.c:561 lwan_main_loop() Ready to serve.
62531 lwan-thread.c:290 _thread_io_loop() Starting IO loop on thread #2.
62530 lwan-thread.c:290 _thread_io_loop() Starting IO loop on thread #1.
Now you can go to any browser and navigate to http://localhost:8080?name=fred
Any doubts? Do not hesitate to contact us visit our forum and feel free to ask any questions.