add config parser

This commit is contained in:
2025-04-25 12:19:07 +02:00
parent dd9e2f557b
commit b9dd1c4007
7 changed files with 85 additions and 13 deletions

View File

@@ -9,7 +9,7 @@ will it ever be. Use it at your own risk.
- [doxygen](https://www.doxygen.nl/) - [doxygen](https://www.doxygen.nl/)
- Optional, just to build the docs. - Optional, just to build the docs.
- libcyaml - libcyaml
- libyaml-0.1 - libyaml
## How to build ## How to build

View File

@@ -1,5 +1,5 @@
# Default config file of CWS # Default config file of CWS
host: "localhost" host: localhost
port: 3030 port: 3030
cert: "cert.pem" cert: "cert.pem"

View File

@@ -40,6 +40,12 @@ void cws_server_setup_hints(struct addrinfo *hints, size_t len, const char *host
*/ */
void cws_server_loop(int sockfd); void cws_server_loop(int sockfd);
// @TODO
/**
* @brief Cleanup server's resources
*/
void cws_server_cleanup();
/** /**
* @brief Adds a file descriptor to the interest list * @brief Adds a file descriptor to the interest list
* *

View File

@@ -2,6 +2,14 @@
#define CWS_CONFIG_H #define CWS_CONFIG_H
typedef struct cws_config_t { typedef struct cws_config_t {
char *host;
char *port;
char *cert;
char *key;
} cws_config; } cws_config;
cws_config *cws_config_init(void);
void cws_config_free(cws_config *config);
#endif #endif

View File

@@ -1,17 +1,38 @@
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include "server/server.h" #include "server/server.h"
#include "utils/colors.h" #include "utils/colors.h"
#include "utils/config.h"
void cws_signal_handler(int signo) {
/* TODO */
fprintf(stdout, BLUE "[server] Cleaning up resources...\n" RESET);
fprintf(stdout, BLUE "[server] Closing...\n" RESET);
_exit(1);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
const char *default_port = "3030"; cws_config *config = cws_config_init();
if (config == NULL) {
fprintf(stdout, BOLD GREEN "[server] Running cws on http://localhost:%s...\n" RESET, default_port); fprintf(stderr, RED BOLD "[server] Unable to read config file\n" RESET);
return 1;
int ret = cws_server_start(NULL, default_port);
if (ret < 0) {
fprintf(stderr, BOLD RED "Unable to start web server\n");
} }
if (signal(SIGINT, cws_signal_handler) == SIG_ERR) {
fprintf(stderr, BOLD RED "[server] Unable to setup signal()\n" RESET);
return 1;
}
fprintf(stdout, BOLD GREEN "[server] Running cws on http://%s:%s...\n" RESET, config->host, config->port);
int ret = cws_server_start(config->host, config->port);
if (ret < 0) {
fprintf(stderr, BOLD RED "[server] Unable to start web server\n" RESET);
}
cws_config_free(config);
return 0; return 0;
} }

View File

@@ -1,3 +1,3 @@
server = files('main.c', 'server/server.c') server = files('main.c', 'server/server.c')
server += files('utils/utils.c', 'utils/hashmap.c') server += files('utils/utils.c', 'utils/hashmap.c', 'utils/config.c')
server += files('http/http.c') server += files('http/http.c')

View File

@@ -1,5 +1,42 @@
#include "utils/config.h" #include "utils/config.h"
int nothing() { #include <cyaml/cyaml.h>
return 0;
} static const cyaml_config_t cyaml_config = {
.log_fn = cyaml_log,
.mem_fn = cyaml_mem,
.log_level = CYAML_LOG_WARNING,
};
static const cyaml_schema_field_t top_mapping_schema[] = {
CYAML_FIELD_STRING_PTR("host", CYAML_FLAG_POINTER, cws_config, host, 0, CYAML_UNLIMITED),
CYAML_FIELD_STRING_PTR("port", CYAML_FLAG_POINTER, cws_config, port, 0, CYAML_UNLIMITED),
CYAML_FIELD_STRING_PTR("cert", CYAML_FLAG_POINTER, cws_config, cert, 0, CYAML_UNLIMITED),
CYAML_FIELD_STRING_PTR("key", CYAML_FLAG_POINTER, cws_config, key, 0, CYAML_UNLIMITED), CYAML_FIELD_END};
static const cyaml_schema_value_t top_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER, cws_config, top_mapping_schema),
};
cws_config *cws_config_init(void) {
char *path = "config.yaml";
cws_config *config;
cyaml_err_t err = cyaml_load_file(path, &cyaml_config, &top_schema, (cyaml_data_t **)&config, NULL);
if (err != CYAML_OK) {
return NULL;
}
return config;
}
void cws_config_free(cws_config *config) {
cyaml_err_t err = cyaml_free(&cyaml_config, &top_schema, config, 0);
if (err != CYAML_OK) {
/* Handle */
}
}
/*
https://github.com/tlsa/libcyaml/blob/main/docs/guide.md
*/