Replace ini with toml

This commit is contained in:
Tobias Reisinger 2020-11-17 23:13:01 +01:00
parent 97a19135ce
commit 5796f88e05
11 changed files with 242 additions and 5726 deletions

View file

@ -1,5 +1,8 @@
#include <bsd/string.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <config.h>
#include <constants.h>
@ -13,72 +16,185 @@ config_t *global_config;
ini_string_match_ii(KEY, disp->data, disp->format))
static int
config_load_log_level(IniDispatch *disp, config_t *config)
config_load_log_level(config_t *config, char *value)
{
if(strcmp(disp->value, "debug") == 0)
if(strcmp(value, "debug") == 0)
{
setlogmask(LOG_UPTO(LOG_DEBUG));
config->log_level = LOG_DEBUG;
config->logging.level = LOG_DEBUG;
return 0;
}
if(strcmp(disp->value, "info") == 0)
if(strcmp(value, "info") == 0)
{
setlogmask(LOG_UPTO(LOG_INFO));
config->log_level = LOG_INFO;
config->logging.level = LOG_INFO;
return 0;
}
if(strcmp(disp->value, "notice") == 0)
if(strcmp(value, "notice") == 0)
{
setlogmask(LOG_UPTO(LOG_NOTICE));
config->log_level = LOG_NOTICE;
config->logging.level = LOG_NOTICE;
return 0;
}
if(strcmp(disp->value, "warning") == 0)
if(strcmp(value, "warning") == 0)
{
setlogmask(LOG_UPTO(LOG_WARNING));
config->log_level = LOG_WARNING;
config->logging.level = LOG_WARNING;
return 0;
}
if(strcmp(disp->value, "err") == 0)
if(strcmp(value, "err") == 0)
{
setlogmask(LOG_UPTO(LOG_ERR));
config->log_level = LOG_ERR;
config->logging.level = LOG_ERR;
return 0;
}
if(strcmp(disp->value, "crit") == 0)
if(strcmp(value, "crit") == 0)
{
setlogmask(LOG_UPTO(LOG_CRIT));
config->log_level = LOG_CRIT;
config->logging.level = LOG_CRIT;
return 0;
}
if(strcmp(disp->value, "emerg") == 0)
if(strcmp(value, "emerg") == 0)
{
setlogmask(LOG_UPTO(LOG_EMERG));
config->log_level = LOG_EMERG;
config->logging.level = LOG_EMERG;
return 0;
}
LOGGER_WARNING("invalid log-level '%s'\n", disp->value);
LOGGER_WARNING("invalid log-level '%s'\n", value);
return 0;
}
static int
config_load_log_file(IniDispatch *disp, config_t *config)
config_load_log_file(config_t *config, char *value)
{
if(strcmp(disp->value, "stdout") == 0)
if(strcmp(value, "stdout") == 0)
{
config->log_file = stdout;
config->logging.file = stdout;
return 0;
}
if(strcmp(disp->value, "stderr") == 0)
if(strcmp(value, "stderr") == 0)
{
config->log_file = stderr;
config->logging.file = stderr;
return 0;
}
config->log_file = fopen(disp->value, "a+");
config->logging.file = fopen(value, "a+");
return 0;
}
static void
config_load_section_core(config_t *config, toml_table_t* core)
{
toml_datum_t config_entry;
config_entry = toml_string_in(core, "database");
if(config_entry.ok)
{
config_load_string(&config->database, config_entry.u.s);
free(config_entry.u.s);
}
config_entry = toml_string_in(core, "user");
if(config_entry.ok)
{
config_load_string(&config->user, config_entry.u.s);
free(config_entry.u.s);
}
config_entry = toml_string_in(core, "group");
if(config_entry.ok)
{
config_load_string(&config->group, config_entry.u.s);
free(config_entry.u.s);
}
config_entry = toml_string_in(core, "content-dir");
if(config_entry.ok)
{
config_load_string(&config->content_dir, config_entry.u.s);
free(config_entry.u.s);
}
config_entry = toml_string_in(core, "not-found-file");
if(config_entry.ok)
{
config_load_string(&config->not_found_file, config_entry.u.s);
free(config_entry.u.s);
}
config_entry = toml_string_in(core, "not-found-file-type");
if(config_entry.ok)
{
config_load_string(&config->not_found_file_type, config_entry.u.s);
free(config_entry.u.s);
}
config_entry = toml_string_in(core, "not-found-content");
if(config_entry.ok)
{
config_load_string(&config->not_found_content, config_entry.u.s);
free(config_entry.u.s);
}
config_entry = toml_string_in(core, "not-found-content-type");
if(config_entry.ok)
{
config_load_string(&config->not_found_content_type, config_entry.u.s);
free(config_entry.u.s);
}
config_entry = toml_string_in(core, "include");
if(config_entry.ok)
{
config_load_string(&config->include, config_entry.u.s);
free(config_entry.u.s);
}
}
static void
config_load_section_logging(config_t *config, toml_table_t* logging)
{
toml_datum_t config_entry;
config_entry = toml_string_in(logging, "level");
if(config_entry.ok)
{
config_load_log_level(config, config_entry.u.s);
free(config_entry.u.s);
}
config_entry = toml_string_in(logging, "file");
if(config_entry.ok)
{
config_load_log_file(config, config_entry.u.s);
free(config_entry.u.s);
}
}
static void
config_load_section_ports(config_t *config, toml_table_t* ports)
{
toml_datum_t config_entry;
config_entry = toml_int_in(ports, "server");
if(config_entry.ok)
{
config->ports.server = config_entry.u.i;
}
config_entry = toml_int_in(ports, "discovery");
if(config_entry.ok)
{
config->ports.discovery = config_entry.u.i;
}
config_entry = toml_int_in(ports, "mqtt");
if(config_entry.ok)
{
config->ports.mqtt = config_entry.u.i;
}
}
void
config_init()
{
@ -86,12 +202,12 @@ config_init()
config_load_string(&global_config->file, DEFAULT_CONFIG_PATH);
global_config->discovery_port = DEFAULT_DISCOVERY_PORT;
global_config->mqtt_port = DEFAULT_MQTT_PORT;
global_config->server_port = DEFAULT_SERVER_PORT;
global_config->ports.discovery = DEFAULT_DISCOVERY_PORT;
global_config->ports.mqtt = DEFAULT_MQTT_PORT;
global_config->ports.server = DEFAULT_SERVER_PORT;
global_config->log_level = LOG_DEBUG;
global_config->log_file = stdout;
global_config->logging.level = LOG_DEBUG;
global_config->logging.file = stdout;
global_config->user = NULL;
global_config->group = NULL;
@ -136,80 +252,71 @@ config_load_string(char **holder, const char *value)
}
int
config_load(IniDispatch *disp, void *config_void)
config_load(config_t *config)
{
config_t *config = (config_t*)config_void;
FILE *fp;
toml_table_t* config_toml;
char errbuf[256];
if(disp->type == INI_KEY)
{
if(CONFINI_IS_KEY("core", "include"))
{
config_load_string(&config->include, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "database"))
{
config_load_string(&config->database, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "user"))
{
config_load_string(&config->user, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "group"))
{
config_load_string(&config->group, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "content-dir"))
{
config_load_string(&config->content_dir, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "not-found-file"))
{
config_load_string(&config->not_found_file, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "not-found-file-type"))
{
config_load_string(&config->not_found_file_type, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "not-found-content"))
{
config_load_string(&config->not_found_content, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "not-found-content-type"))
{
config_load_string(&config->not_found_content_type, disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "log-level"))
{
return config_load_log_level(disp, config);
}
if(CONFINI_IS_KEY("core", "log-file"))
{
return config_load_log_file(disp, config);
}
if(CONFINI_IS_KEY("core", "server-port"))
{
config->server_port = atoi(disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "discovery-port"))
{
config->discovery_port = atoi(disp->value);
return 0;
}
if(CONFINI_IS_KEY("core", "mqtt-port"))
{
config->mqtt_port = atoi(disp->value);
return 0;
}
/* Open the file and parse content */
fp = fopen(global_config->file, "r");
if(fp == NULL) {
LOGGER_CRIT("unable to open config file '%s'\n", global_config->file);
exit(1);
}
config_toml = toml_parse_file(fp, errbuf, sizeof(errbuf));
fclose(fp);
if(config_toml == NULL) {
LOGGER_CRIT("unable to parse config file '%s': %s\n", global_config->file, errbuf);
exit(1);
}
toml_table_t* core = toml_table_in(config_toml, "core");
if(core)
{
config_load_section_core(config, core);
}
toml_table_t* logging = toml_table_in(config_toml, "logging");
if(logging)
{
config_load_section_logging(config, logging);
}
toml_table_t* ports = toml_table_in(config_toml, "ports");
if(ports)
{
config_load_section_ports(config, ports);
}
toml_free(config_toml);
return 0;
}
void
config_load_directory(config_t *config, char *directory_name)
{
struct dirent *pDirent;
DIR *pDir;
(void)config;
pDir = opendir (directory_name);
if(pDir == NULL)
{
LOGGER_CRIT("cannot open directory '%s': %s\n", directory_name, strerror(errno));
exit(1);
}
// Process each entry.
while((pDirent = readdir(pDir)) != NULL)
{
LOGGER_DEBUG("including: %s\n", pDirent->d_name);
}
// Close directory and exit.
closedir(pDir);
}

View file

@ -125,7 +125,7 @@ api_v1_controllers_discover_PUT(struct mg_connection *nc, struct http_message *h
payload[0] = discover_server_port;
LOGGER_DEBUG("sending udp broadcast\n");
if(send_udp_broadcast("255.255.255.255", global_config->discovery_port, payload, sizeof(payload)) < 0)
if(send_udp_broadcast("255.255.255.255", global_config->ports.discovery, payload, sizeof(payload)) < 0)
{
M_RESPONSE_MSG(LOGGER_ERR, response, 500, "the server failed to send discovery broadcast");
return;

View file

@ -17,7 +17,7 @@ const char *COLOR_EMERG = COLOR_MAGENTA;
void
logger_log(int level, const char *filename, int line, const char *func, const char *msg, ...)
{
if(global_config->log_level < level || level == LOG_NONE )
if(global_config->logging.level < level || level == LOG_NONE )
{
return;
}
@ -82,8 +82,8 @@ logger_log(int level, const char *filename, int line, const char *func, const ch
syslog(level, "%s", log_line + timestamp_len + 1);
fprintf(global_config->log_file, "%s", log_line);
fflush(global_config->log_file);
fprintf(global_config->logging.file, "%s", log_line);
fflush(global_config->logging.file);
free(buffer);
free(log_line);

View file

@ -3,7 +3,6 @@
#include <string.h>
#include <syslog.h>
#include <confini.h>
#include <mongoose.h>
#include <cache.h>
@ -62,23 +61,16 @@ main(int argc, const char** argv)
cli_parse(argc, argv, global_config);
FILE * const ini_file = fopen(global_config->file, "rb");
if(ini_file == NULL)
config_load(global_config);
if(global_config->logging.file == NULL)
{
LOGGER_CRIT("config file '%s' was not found\n", global_config->file);
exit(1);
}
if(load_ini_file(ini_file, INI_DEFAULT_FORMAT, NULL, config_load, global_config))
{
LOGGER_CRIT("unable to parse ini file\n");
exit(1);
global_config->logging.file = stdout;
}
fclose(ini_file);
if(global_config->log_file == NULL)
if(global_config->include)
{
global_config->log_file = stdout;
config_load_directory(global_config, global_config->include);
}
LOGGER_DEBUG("Loaded config from %s\n", global_config->file);
@ -91,20 +83,20 @@ main(int argc, const char** argv)
mg_mgr_init(&mgr, NULL);
char address[100];
sprintf(address, "tcp://0.0.0.0:%u", global_config->server_port);
sprintf(address, "tcp://0.0.0.0:%u", global_config->ports.server);
struct mg_connection *c_http = mg_bind(&mgr, address, handler_http);
if(c_http == NULL)
{
LOGGER_CRIT("failed to bind http server to port %u\n", global_config->server_port);
LOGGER_CRIT("failed to bind http server to port %u\n", global_config->ports.server);
exit(1);
}
mg_set_protocol_http_websocket(c_http);
sprintf(address, "tcp://0.0.0.0:%u", global_config->mqtt_port);
sprintf(address, "tcp://0.0.0.0:%u", global_config->ports.mqtt);
struct mg_connection *c_mqtt = mg_bind(&mgr, address, handler_mqtt);
if(c_mqtt == NULL)
{
LOGGER_CRIT("failed to bind mqtt server to port %u\n", global_config->mqtt_port);
LOGGER_CRIT("failed to bind mqtt server to port %u\n", global_config->ports.mqtt);
exit(1);
}
mg_mqtt_broker_init(&brk, NULL);