Files
auth_service_c/src/main.c
T
tola 4c930e3b82
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 11s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Successful in 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Successful in 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Successful in 12s
more refactor
2025-10-27 18:57:17 +07:00

97 lines
3.2 KiB
C

#include <colors.h>
#include <mongoose.h>
#ifdef USE_SQLITE3
#include <sqlite3.h>
#endif
#include "config/config.h"
#include "security/cors.h"
#include "database/database.h"
#include "utils/file.h"
#include "api/route.h"
#include "api/static.h"
#include "api/user.h"
static const struct route routes[] = {
{"GET", "/#", root_handler},
{"OPTIONS", "/#", set_cors_headers},
{"POST", "/api/token", get_token},
{"POST", "/api/token/verify", verify_token},
{"POST", "/api/users", get_all_users},
{"POST", "/api/users/user", get_user},
{"DELETE", "/api/users", delete_user},
{"POST", "/api/users/password", change_password},
{"PUT", "/api/users", change_user},
{"POST", "/api/users/add", add_user},
{"POST", "/api/register", register_user},
{nullptr, nullptr, nullptr}};
void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_ERROR) {
MG_LOG(MG_LL_ERROR, ("Ошибка соединения: %s", (char *)ev_data));
}
const bool tls_enabled = cm_get_parameter_as_bool(config, "tls_enabled");
if (tls_enabled) {
if (ev == MG_EV_ACCEPT && c->is_tls) {
struct mg_tls_opts opts;
memset(&opts, 0, sizeof(opts));
opts.cert =
mg_str(read_file(cm_get_parameter_as_string(config, "tls_cert")));
opts.key =
mg_str(read_file(cm_get_parameter_as_string(config, "tls_key")));
mg_tls_init(c, &opts);
}
}
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)) {
mg_log("%s%.*s %s%.*s%s", GREEN, hm->uri.len, hm->uri.buf, BWHITE,
hm->method.len, hm->method.buf, RESET);
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) {
init_config("config.json");
init_db();
migrate_db();
const char *default_users_filename =
cm_get_parameter_as_string(config, "default_users_file");
if (default_users_filename) db_get_users_from_file(default_users_filename);
struct mg_mgr mgr;
mg_log_set(MG_LL_DEBUG);
mg_mgr_init(&mgr);
const char *address = cm_get_parameter_as_string(config, "address");
char http_address[64];
snprintf(http_address, sizeof(http_address), "http://%s", address);
address ? mg_http_listen(&mgr, http_address, ev_handler, nullptr)
: mg_http_listen(&mgr, "http://0.0.0.0:8222", ev_handler, nullptr);
if (cm_get_parameter_as_bool(config, "tls_enabled")) {
const char *tls_address = cm_get_parameter_as_string(config, "tls_address");
char https_address[64];
snprintf(https_address, sizeof(https_address), "https://%s", tls_address);
tls_address
? mg_http_listen(&mgr, https_address, ev_handler, nullptr)
: mg_http_listen(&mgr, "https://0.0.0.0:8223", ev_handler, nullptr);
}
for (;;) {
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}