math/src/main.c

142 lines
4.2 KiB
C

#include "server/auth.h"
#include "server/config.h"
#include "server/types.h"
#include "jwt/jwt.h"
#include "db/auth.h"
#include "db/db.h"
#include <civetweb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
leveldb_t* db = NULL;
void signal_handler(int signal)
{
fprintf(stderr, "Signal %d received, cleaning up...\n", signal);
close_db(db);
exit(1);
}
int main(int argc, char** argv)
{
open_user_db();
// while (1) {
// printf("> ");
// char op[16];
// scanf("%15s", op);
// if (strcmp(op, "exit") == 0) break;
// if (strcmp(op, "login") == 0) {
// char user_id[64];
// char password[64];
// scanf("%63s %63s", user_id, password);
// int result;
// int flag = login(user_id, password, &result);
// if (!flag) {
// printf("Failed to login\n");
// } else {
// printf("Result: %s\n", result ? "login success" : "login failed");
// }
// }else if(strcmp(op, "logout") == 0){
// char user_id[64];
// scanf("%63s", user_id);
// int flag = logout(user_id);
// if (!flag) {
// printf("Failed to logout\n");
// } else {
// printf("Logout success\n");
// }
// } else if (strcmp(op, "register") == 0) {
// char user_id[64];
// char password[64];
// scanf("%63s %63s", user_id, password);
// int result;
// int flag = check_user_exists(user_id, &result);
// if(!flag){
// printf("Failed to check user existence\n");
// continue;
// }else if (result) {
// printf("User already exists\n");
// continue;
// }
// flag = set_user_password(user_id, password);
// if (!flag) {
// printf("Failed to register\n");
// } else {
// printf("Registration success\n");
// }
// } else if (strcmp(op, "check") == 0) {
// char user_id[64];
// scanf("%63s", user_id);
// int result;
// int flag = check_user_exists(user_id, &result);
// if (!flag) {
// printf("Failed to check user existence\n");
// } else {
// printf("Result: %s\n", result ? "user exists" : "user does not exist");
// }
// }
// }
// getchar();
// char* jwt = create_token("Hello", "world");
// printf("JWT: %s\n", jwt);
// int flag = verify_token(jwt, "world");
// printf("Flag: %d\n", flag);
// char* id = get_payload(jwt);
// printf("ID: %s\n", id);
// free(id);
// free(jwt);
// if (!(db = open_db("db/main"))) return 1;
// signal(SIGINT, signal_handler);
// signal(SIGTERM, signal_handler);
// signal(SIGSEGV, signal_handler);
config_t config;
int stat = config_read(&config, "configs/config.json");
if (!stat) {
printf("config_read() returned %d\n", stat);
config_dtor(&config);
return 1;
}
if (config.server_port > 65535 || config.server_port <= 0) {
printf("Invalid server_port: %d\n", config.server_port);
config_dtor(&config);
return 1;
}
secret = config.secret;
char server_port_str[6];
snprintf(server_port_str, sizeof(server_port_str), "%d", config.server_port);
const char* options[] =
{"document_root", "./www", "listening_ports", server_port_str, "error_log_file", "logs/civetweb.log", NULL};
mg_callbacks callbacks;
mg_context* ctx;
memset(&callbacks, 0, sizeof(callbacks));
ctx = mg_start(&callbacks, NULL, options);
mg_set_request_handler(ctx, "/api/auth/login", login_handler, NULL);
mg_set_request_handler(ctx, "/api/auth/register", register_handler, NULL);
mg_set_request_handler(ctx, "/api/auth/delete", delete_handler, NULL);
printf("Server started on port(s) %s\n", mg_get_option(ctx, "listening_ports"));
getchar();
mg_stop(ctx);
close_user_db();
// close_db(db);
return 0;
}