added config and tls
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Has been cancelled
This commit is contained in:
1 parent
28655e7b46
commit
4c7a156723
10 files changed
+274
-10
No files matched your search
+25
-8
@@ -1,15 +1,13 @@
|
||||
#include <colors.h>
|
||||
#include <mongoose.h>
|
||||
#include <sqlite3.h>
|
||||
#include "config.h"
|
||||
#include "cors.h"
|
||||
#include "database.h"
|
||||
#include "route.h"
|
||||
#include "static.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);
|
||||
}
|
||||
#include "file.h"
|
||||
|
||||
static const struct route routes[] = {
|
||||
{"GET", "/#", root_handler},
|
||||
@@ -26,6 +24,17 @@ static const struct route routes[] = {
|
||||
{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));
|
||||
}
|
||||
if (ev == MG_EV_ACCEPT && c->is_tls) {
|
||||
struct mg_tls_opts opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.cert = mg_str(read_file("/home/tola/certs/localhost/cert.pem"));
|
||||
opts.key = mg_str(read_file("/home/tola/certs/localhost/key.pem"));
|
||||
mg_tls_init(c, &opts);
|
||||
}
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
const auto hm = (struct mg_http_message *) ev_data;
|
||||
bool route_found = false;
|
||||
@@ -48,16 +57,24 @@ void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
init_config("config.json");
|
||||
init_db();
|
||||
migrate_db();
|
||||
db_get_users_from_file("users.json");
|
||||
const char *default_users_filename =
|
||||
cm_get_parameter_as_string(config, "default_users_file");
|
||||
if (default_users_filename) db_get_users_from_file("users.json");
|
||||
|
||||
struct mg_mgr mgr;
|
||||
mg_log_set(MG_LL_DEBUG);
|
||||
mg_mgr_init(&mgr);
|
||||
mg_http_listen(&mgr, "http://0.0.0.0:8222", ev_handler, nullptr);
|
||||
while (true) {
|
||||
const char *address = cm_get_parameter_as_string(config, "address");
|
||||
// address ? mg_http_listen(&mgr, cm_get_parameter_as_string(config, "address"),
|
||||
// ev_handler, nullptr)
|
||||
// : mg_http_listen(&mgr, "http://0.0.0.0:8222", ev_handler, nullptr);
|
||||
mg_http_listen(&mgr, "https://0.0.0.0:8222", ev_handler, nullptr);
|
||||
for (;;) {
|
||||
mg_mgr_poll(&mgr, 1000);
|
||||
}
|
||||
mg_mgr_free(&mgr);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user