add config parser
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Default config file of CWS
|
||||
host: "localhost"
|
||||
host: localhost
|
||||
port: 3030
|
||||
|
||||
cert: "cert.pem"
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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
|
||||
|
||||
35
src/main.c
35
src/main.c
@@ -1,17 +1,38 @@
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -1,5 +1,42 @@
|
||||
#include "utils/config.h"
|
||||
|
||||
int nothing() {
|
||||
return 0;
|
||||
#include <cyaml/cyaml.h>
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user