diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-09-16 01:46:07 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-09-16 01:46:07 +0200 |
| commit | b2037fea902cca2dd0238a1116b05480ef29b3d6 (patch) | |
| tree | 505bf26c6daa798b6e8c467763a06b60ba2b5b5c | |
| parent | bec528a979ccadbd6687ee6679cf4b43771db586 (diff) | |
| download | slcl-b2037fea902cca2dd0238a1116b05480ef29b3d6.tar.gz | |
main.c: Refactor calls to handler_add
| -rw-r--r-- | main.c | 52 |
1 files changed, 39 insertions, 13 deletions
@@ -1911,6 +1911,44 @@ end: return ret; } +static int add_urls(struct handler *const h, void *const user) +{ + static const struct url + { + const char *url; + enum http_op op; + handler_fn f; + } urls[] = + { + {.url = "/", .op = HTTP_OP_GET, .f = serve_index}, + {.url = "/index.html", .op = HTTP_OP_GET, .f = serve_index}, + {.url = "/style.css", .op = HTTP_OP_GET, .f = serve_style}, + {.url = "/user/*", .op = HTTP_OP_GET, .f = getnode}, + {.url = "/login", .op = HTTP_OP_POST, .f = login}, + {.url = "/logout", .op = HTTP_OP_POST, .f = logout}, + {.url = "/public/*", .op = HTTP_OP_GET, .f = getpublic}, + {.url = "/search", .op = HTTP_OP_POST, .f = search}, + {.url = "/share", .op = HTTP_OP_POST, .f = share}, + {.url = "/upload", .op = HTTP_OP_POST, .f = upload}, + {.url = "/mkdir", .op = HTTP_OP_POST, .f = createdir}, + {.url = "/confirm/rm", .op = HTTP_OP_POST, .f = confirm_rm}, + {.url = "/rm", .op = HTTP_OP_POST, .f = rm} + }; + + for (size_t i = 0; i < sizeof urls / sizeof *urls; i++) + { + const struct url *const u = &urls[i]; + + if (handler_add(h, u->url, u->op, u->f, user)) + { + fprintf(stderr, "%s: handler_add %s failed\n", __func__, u->url); + return -1; + } + } + + return 0; +} + int main(int argc, char *argv[]) { int ret = EXIT_FAILURE; @@ -1933,19 +1971,7 @@ int main(int argc, char *argv[]) }; if (!(h = handler_alloc(&cfg)) - || handler_add(h, "/", HTTP_OP_GET, serve_index, a) - || handler_add(h, "/index.html", HTTP_OP_GET, serve_index, a) - || handler_add(h, "/style.css", HTTP_OP_GET, serve_style, a) - || handler_add(h, "/user/*", HTTP_OP_GET, getnode, a) - || handler_add(h, "/login", HTTP_OP_POST, login, a) - || handler_add(h, "/logout", HTTP_OP_POST, logout, a) - || handler_add(h, "/public/*", HTTP_OP_GET, getpublic, a) - || handler_add(h, "/search", HTTP_OP_POST, search, a) - || handler_add(h, "/share", HTTP_OP_POST, share, a) - || handler_add(h, "/upload", HTTP_OP_POST, upload, a) - || handler_add(h, "/mkdir", HTTP_OP_POST, createdir, a) - || handler_add(h, "/confirm/rm", HTTP_OP_POST, confirm_rm, a) - || handler_add(h, "/rm", HTTP_OP_POST, rm, a) + || add_urls(h, a) || handler_listen(h, port)) goto end; |
