aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 0ba64c9acb11fe86b70806b363a74a9bd3f6b8b2 (plain)
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
#include "lib/const_strings.h"
#include "lib/http.h"

void success(struct request req) {
  req.resp->code = OK;
  req.resp->body = CS("Hello world!\n");
  http_send(req);
}

void logging_func(http_middleware *self, struct request req) {
  (self->next->func)(self->next, req);
  http_log(HTTP_INFO, "[1]"CS_FMT" "CS_FMT": %ld\n", CS_ARG(req.method), CS_ARG(req.path), req.resp->code);
}

int main() {
  arena arena = {0};
  struct server serv = {0};
  if (init_server(&arena, &serv, "127.0.0.1", "6969") != 0) {
	return 1;
  }

  handler *success_handler = handle_path(&serv, CS("GET"), CS("/"), success);
  http_register_handler_middleware(&arena, success_handler, logging_func);
  
  listen_and_serve(&serv);

  return 0;
}