216 lines
5.6 KiB
C
216 lines
5.6 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <config.h>
|
|
#include <constants.h>
|
|
#include <logger.h>
|
|
|
|
config_t *global_config;
|
|
|
|
|
|
#define CONFINI_IS_KEY(SECTION, KEY) \
|
|
(ini_array_match(SECTION, disp->append_to, '.', disp->format) && \
|
|
ini_string_match_ii(KEY, disp->data, disp->format))
|
|
|
|
static int
|
|
config_load_log_level(IniDispatch *disp, config_t *config)
|
|
{
|
|
if(strcasecmp(disp->value, "debug") == 0)
|
|
{
|
|
setlogmask(LOG_UPTO(LOG_DEBUG));
|
|
config->log_level = LOG_DEBUG;
|
|
return 0;
|
|
}
|
|
if(strcasecmp(disp->value, "info") == 0)
|
|
{
|
|
setlogmask(LOG_UPTO(LOG_INFO));
|
|
config->log_level = LOG_INFO;
|
|
return 0;
|
|
}
|
|
if(strcasecmp(disp->value, "notice") == 0)
|
|
{
|
|
setlogmask(LOG_UPTO(LOG_NOTICE));
|
|
config->log_level = LOG_NOTICE;
|
|
return 0;
|
|
}
|
|
if(strcasecmp(disp->value, "warning") == 0)
|
|
{
|
|
setlogmask(LOG_UPTO(LOG_WARNING));
|
|
config->log_level = LOG_WARNING;
|
|
return 0;
|
|
}
|
|
if(strcasecmp(disp->value, "err") == 0)
|
|
{
|
|
setlogmask(LOG_UPTO(LOG_ERR));
|
|
config->log_level = LOG_ERR;
|
|
return 0;
|
|
}
|
|
if(strcasecmp(disp->value, "crit") == 0)
|
|
{
|
|
setlogmask(LOG_UPTO(LOG_CRIT));
|
|
config->log_level = LOG_CRIT;
|
|
return 0;
|
|
}
|
|
if(strcasecmp(disp->value, "emerg") == 0)
|
|
{
|
|
setlogmask(LOG_UPTO(LOG_EMERG));
|
|
config->log_level = LOG_EMERG;
|
|
return 0;
|
|
}
|
|
LOGGER_WARNING("invalid log-level '%s'\n", disp->value);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
config_load_log_file(IniDispatch *disp, config_t *config)
|
|
{
|
|
if(strcasecmp(disp->value, "stdout") == 0)
|
|
{
|
|
config->log_file = stdout;
|
|
return 0;
|
|
}
|
|
if(strcasecmp(disp->value, "stderr") == 0)
|
|
{
|
|
config->log_file = stderr;
|
|
return 0;
|
|
}
|
|
config->log_file = fopen(disp->value, "a+");
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
config_init()
|
|
{
|
|
global_config = calloc(1, sizeof(config_t));
|
|
|
|
config_load_string(&global_config->file, DEFAULT_CONFIG_PATH);
|
|
|
|
global_config->discovery_port = 4421;
|
|
global_config->mqtt_port = 1885;
|
|
global_config->server_port = 5000;
|
|
|
|
global_config->log_level = LOG_DEBUG;
|
|
global_config->log_file = stdout;
|
|
|
|
global_config->user = NULL;
|
|
global_config->group = NULL;
|
|
|
|
config_load_string(&global_config->content_dir, ".");
|
|
config_load_string(&global_config->not_found_file, "404.html");
|
|
config_load_string(&global_config->not_found_file_type, "text/html");
|
|
config_load_string(&global_config->not_found_content, "404 - NOT FOUND");
|
|
config_load_string(&global_config->not_found_content_type, "text/plain");
|
|
}
|
|
|
|
void
|
|
config_free()
|
|
{
|
|
free(global_config->file);
|
|
free(global_config->include);
|
|
free(global_config->database);
|
|
free(global_config->user);
|
|
free(global_config->group);
|
|
free(global_config->content_dir);
|
|
free(global_config->not_found_file);
|
|
free(global_config->not_found_file_type);
|
|
free(global_config->not_found_content);
|
|
free(global_config->not_found_content_type);
|
|
|
|
free(global_config);
|
|
}
|
|
|
|
void
|
|
config_load_string(char **holder, const char *value)
|
|
{
|
|
if(*holder)
|
|
{
|
|
free(*holder);
|
|
}
|
|
size_t value_len = strlen(value);
|
|
|
|
char *new_holder = malloc(sizeof(char) * (value_len + 1));
|
|
strcpy(new_holder, value);
|
|
new_holder[value_len] = '\0';
|
|
|
|
*holder = new_holder;
|
|
}
|
|
|
|
int
|
|
config_load(IniDispatch *disp, void *config_void)
|
|
{
|
|
config_t *config = (config_t*)config_void;
|
|
|
|
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;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|