diff options
| -rw-r--r-- | example-config.ini | 3 | ||||
| -rw-r--r-- | src/Config.c | 67 | ||||
| -rw-r--r-- | src/Config.h | 13 | ||||
| -rw-r--r-- | src/Main.c | 12 |
4 files changed, 90 insertions, 5 deletions
diff --git a/example-config.ini b/example-config.ini new file mode 100644 index 0000000..8905049 --- /dev/null +++ b/example-config.ini @@ -0,0 +1,3 @@ +[server] +host = 0.0.0.0 +port = 8000
\ No newline at end of file diff --git a/src/Config.c b/src/Config.c new file mode 100644 index 0000000..047bd00 --- /dev/null +++ b/src/Config.c @@ -0,0 +1,67 @@ +#include "Config.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int load_config(const char *filename, Config *config) { + FILE *file = fopen(filename, "r"); + if (!file) { + return -1; + } + + char line[512]; + char section[64] = ""; + + while (fgets(line, sizeof(line), file)) { + + line[strcspn(line, "\r\n")] = 0; + + if (line[0] == '\0' || line[0] == '#' || line[0] == ';') { + continue; + } + + if (line[0] == '[') { + char *end = strchr(line, ']'); + if (end) { + *end = '\0'; + snprintf(section, sizeof(section), "%s", line + 1); + section[sizeof(section) - 1] = '\0'; + } + continue; + } + + char *delimiter = strchr(line, '='); + if (delimiter) { + *delimiter = '\0'; + char *key = line; + char *value = delimiter + 1; + + while (*key == ' ' || *key == '\t') key++; + while (*value == ' ' || *value == '\t') value++; + + char *key_end = key + strlen(key) - 1; + while (key_end > key && (*key_end == ' ' || *key_end == '\t')) { + *key_end = '\0'; + key_end--; + } + + char *value_end = value + strlen(value) - 1; + while (value_end > value && (*value_end == ' ' || *value_end == '\t')) { + *value_end = '\0'; + value_end--; + } + + if (strcmp(section, "server") == 0) { + if (strcmp(key, "host") == 0) { + strncpy(config->host, value, sizeof(config->host) - 1); + config->host[sizeof(config->host) - 1] = '\0'; + } else if (strcmp(key, "port") == 0) { + config->port = atoi(value); + } + } + } + } + + fclose(file); + return 0; +}
\ No newline at end of file diff --git a/src/Config.h b/src/Config.h index b5695b7..384ed94 100644 --- a/src/Config.h +++ b/src/Config.h @@ -1,2 +1,11 @@ -static int port = 5000; -static char host[] = "0.0.0.0";
\ No newline at end of file +#ifndef CONFIG_H +#define CONFIG_H + +typedef struct { + char host[256]; + int port; +} Config; + +int load_config(const char *filename, Config *config); + +#endif
\ No newline at end of file @@ -15,13 +15,19 @@ int main() { curl_global_init(CURL_GLOBAL_DEFAULT); + Config config = {.host = "0.0.0.0", .port = 5000}; + + if (load_config("config.ini", &config) != 0) { + fprintf(stderr, "Warning: Could not load config file, using defaults\n"); + } + set_handler("/", home_handler); set_handler("/search", results_handler); set_handler("/images", images_handler); - fprintf(stderr, "Starting Omnisearch on %s:%d\n", host, port); + fprintf(stderr, "Starting Omnisearch on %s:%d\n", config.host, config.port); - int result = beaker_run(host, port); + int result = beaker_run(config.host, config.port); if (result != 0) { fprintf(stderr, "Error: Beaker server failed to start.\n"); @@ -33,4 +39,4 @@ int main() { curl_global_cleanup(); xmlCleanupParser(); return EXIT_SUCCESS; -} +}
\ No newline at end of file |
