added database

This commit is contained in:
wt committed 2025-08-22 17:46:38 +07:00
1 parent c4ea7a4293
commit 4f67cf11bd
30 files changed
+601 -267

No files matched your search

+20 -12
View File
@@ -1,10 +1,13 @@
#include "user.h"
#include <mongoose.h>
#include <sqlite3.h>
#include "database.h"
#include "route.h"
#include "sql.h"
#include "user.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);
struct mg_http_serve_opts opts = {.root_dir = "./web/"};
mg_http_serve_dir(c, hm, &opts);
}
static const struct route routes[] = {
@@ -18,28 +21,33 @@ static const struct route routes[] = {
{"POST", "/api/users/change", change_password},
{"POST", "/api/users/add", add_user},
{"POST", "/api/register", register_user},
{nullptr, nullptr, nullptr}
};
{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;
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 (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"));
mg_http_reply(c, 404, "", "{%m:%m}", MG_ESC("error"),
MG_ESC("URI not found"));
}
}
}
int main(void) {
init_db();
migrate_db();
db_get_users_from_file("users.json");
struct mg_mgr mgr;
mg_mgr_init(&mgr);
mg_http_listen(&mgr, "http://0.0.0.0:8222", ev_handler, nullptr);