From b9dd1c40070c48cf5e95f758f07cb1d5d19be9e1 Mon Sep 17 00:00:00 2001 From: Francesco Date: Fri, 25 Apr 2025 12:19:07 +0200 Subject: [PATCH] add config parser --- README.md | 2 +- config.yaml | 2 +- include/server/server.h | 6 ++++++ include/utils/config.h | 8 ++++++++ src/main.c | 35 ++++++++++++++++++++++++++------- src/meson.build | 2 +- src/utils/config.c | 43 ++++++++++++++++++++++++++++++++++++++--- 7 files changed, 85 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 0aa732d..5b91413 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ will it ever be. Use it at your own risk. - [doxygen](https://www.doxygen.nl/) - Optional, just to build the docs. - libcyaml -- libyaml-0.1 +- libyaml ## How to build diff --git a/config.yaml b/config.yaml index c53c471..6733aff 100644 --- a/config.yaml +++ b/config.yaml @@ -1,5 +1,5 @@ # Default config file of CWS -host: "localhost" +host: localhost port: 3030 cert: "cert.pem" diff --git a/include/server/server.h b/include/server/server.h index 96aadb8..913021d 100644 --- a/include/server/server.h +++ b/include/server/server.h @@ -40,6 +40,12 @@ void cws_server_setup_hints(struct addrinfo *hints, size_t len, const char *host */ 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 * diff --git a/include/utils/config.h b/include/utils/config.h index 05d950d..e4c9475 100644 --- a/include/utils/config.h +++ b/include/utils/config.h @@ -2,6 +2,14 @@ #define CWS_CONFIG_H typedef struct cws_config_t { + char *host; + char *port; + + char *cert; + char *key; } cws_config; +cws_config *cws_config_init(void); +void cws_config_free(cws_config *config); + #endif diff --git a/src/main.c b/src/main.c index 90b7e45..fe481eb 100644 --- a/src/main.c +++ b/src/main.c @@ -1,17 +1,38 @@ +#include #include +#include #include "server/server.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) { - const char *default_port = "3030"; - - fprintf(stdout, BOLD GREEN "[server] Running cws on http://localhost:%s...\n" RESET, default_port); - - int ret = cws_server_start(NULL, default_port); - if (ret < 0) { - fprintf(stderr, BOLD RED "Unable to start web server\n"); + cws_config *config = cws_config_init(); + if (config == NULL) { + fprintf(stderr, RED BOLD "[server] Unable to read config file\n" RESET); + return 1; } + 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; } diff --git a/src/meson.build b/src/meson.build index 856b031..59f2b66 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,3 +1,3 @@ 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') diff --git a/src/utils/config.c b/src/utils/config.c index 9a3e926..4c13593 100644 --- a/src/utils/config.c +++ b/src/utils/config.c @@ -1,5 +1,42 @@ #include "utils/config.h" -int nothing() { - return 0; -} \ No newline at end of file +#include + +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 +*/