This is not really a library, but a little project for my education. But if you really want to use it, you can.
make static
# or
make sharedCopy the directory in the include/ folder to your project.
arena arena = {0};
http_server serv = {0};
if (init_server(&arena, &serv, "127.0.0.1", "6969") != 0) {
return 1;
}http_handler *hello_world_handler = http_handle_path(&serv, "GET", "/", hello_world);
void hello_world(http_request req) {
req.resp->code = OK;
req.resp->body = CS("Hello world!\n");
http_send(req);
}http_register_global_middleware(&serv, logging_func);
// or
http_register_handler_middleware(&arena, hello_world_handler, logging_func);
void logging_func(http_middleware *self, http_request req) {
// you have to run http_run_next to run the next middleware
http_run_next(self, req);
// or you can just send the response
// http_send(req);
http_log(HTTP_INFO, CS_FMT" "CS_FMT": %ld\n", CS_ARG(req.method), CS_ARG(req.path), req.resp->code);
}And to wrap things up, we need to run
listen_and_serve(&serv);#include <chttp/http.h>
#include <chttp/const_strings.h>
void logging_func(http_middleware *self, http_request req) {
http_run_next(self, req);
http_log(HTTP_INFO, CS_FMT" "CS_FMT": %ld\n", CS_ARG(req.method), CS_ARG(req.path), req.resp->code);
}
void hello_world(http_request req) {
req.resp->code = OK;
req.resp->body = CS("Hello world!\n");
http_send(req);
}
int main() {
arena arena = {0};
http_server serv = {0};
if (init_server(&arena, &serv, "127.0.0.1", "6969") != 0) {
return 1;
}
http_handler *hello_world_handler = http_handle_path(&serv, "GET", "/", hello_world);
http_register_handler_middleware(&arena, hello_world_handler, logging_func);
http_register_global_middleware(&serv, logging_func);
listen_and_serve(&serv);
return 0;
}