forked from MechanisM/Tofu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-errors.c
48 lines (32 loc) · 1.02 KB
/
example-errors.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <tofu.h>
tofu_rep_t *handler_root(tofu_req_t *req) {
tofu_rep_t *rep = tofu_rep_init();
tofu_head(rep, "Content-Type", "text/html");
/* some random error occurred here */
tofu_status(rep, 500);
return rep;
}
tofu_rep_t *handler_404(tofu_req_t *req) {
tofu_rep_t *rep = tofu_rep_init();
tofu_head(rep, "Content-Type", "text/html");
tofu_write(rep, "<!DOCTYPE html>\n<head><title>404</title></head>\n");
tofu_write(rep, "<body>Not found</body>\n");
return rep;
}
tofu_rep_t *handler_500(tofu_req_t *req) {
tofu_rep_t *rep = tofu_rep_init();
tofu_head(rep, "Content-Type", "text/html");
tofu_write(rep, "<!DOCTYPE html>\n<head><title>500</title></head>\n");
tofu_write(rep, "<body>Error!!!</body>\n");
return rep;
}
int main() {
char *opts[] = { "0.0.0.0", "2000" };
tofu_ctx_t *ctx = tofu_ctx_init(TOFU_EVHTTP, opts);
tofu_handle_with(ctx, GET, "/", handler_root);
tofu_rescue_with(ctx, 404, handler_404);
tofu_rescue_with(ctx, 500, handler_500);
tofu_loop(ctx);
tofu_ctx_free(ctx);
return 0;
}