initial commit
This commit is contained in:
commit
22dc118616
17 files changed
+33251
No files matched your search
+44
@@ -0,0 +1,44 @@
|
||||
#include "user.h"
|
||||
#include <mongoose.h>
|
||||
#include "route.h"
|
||||
|
||||
static void root_handler(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
struct mg_http_serve_opts opts = {.root_dir = "./web/"};
|
||||
mg_http_serve_dir(c, hm, &opts);
|
||||
}
|
||||
|
||||
static const struct route routes[] = {
|
||||
{"GET", "/", root_handler},
|
||||
{"POST", "/api/token", get_token},
|
||||
{"POST", "/api/token/verify", verify_token},
|
||||
{"POST", "/api/users", get_all_users},
|
||||
{nullptr, nullptr, nullptr}
|
||||
};
|
||||
|
||||
void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
const auto hm = (struct mg_http_message *)ev_data;
|
||||
bool route_found = false;
|
||||
|
||||
for (int i = 0; routes[i].path != nullptr; i++) {
|
||||
if (mg_match(hm->uri, mg_str(routes[i].path), nullptr) && mg_match(hm->method, mg_str(routes[i].method), nullptr)) {
|
||||
routes[i].handler(c, hm);
|
||||
route_found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!route_found) {
|
||||
mg_http_reply(c, 404, "", "{%m:%m}", MG_ESC("error"), MG_ESC("URI not found"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_mgr mgr;
|
||||
mg_mgr_init(&mgr);
|
||||
mg_http_listen(&mgr, "http://0.0.0.0:8222", ev_handler, nullptr);
|
||||
while (true) {
|
||||
mg_mgr_poll(&mgr, 1000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user