Refactor config parsing to use a switch statement for improved readability and maintainability
This commit is contained in:
@@ -5,6 +5,20 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include "server_config.h"
|
#include "server_config.h"
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
CONFIG_PORT,
|
||||||
|
CONFIG_USE_HTTPS,
|
||||||
|
CONFIG_LOG_FILE,
|
||||||
|
CONFIG_MAX_THREADS,
|
||||||
|
CONFIG_RUNNING,
|
||||||
|
CONFIG_SERVER_NAME,
|
||||||
|
CONFIG_VERBOSE,
|
||||||
|
CONFIG_ENABLE_HTTP2,
|
||||||
|
CONFIG_ENABLE_WEBSOCKET,
|
||||||
|
CONFIG_UNKNOWN
|
||||||
|
|
||||||
|
} ConfigKey;
|
||||||
|
|
||||||
// Trim whitespace from both ends of a string
|
// Trim whitespace from both ends of a string
|
||||||
static char* trim_whitespace(char *str) {
|
static char* trim_whitespace(char *str) {
|
||||||
char *end;
|
char *end;
|
||||||
@@ -32,6 +46,32 @@ static bool parse_bool(const char *value) {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// Map string to enum
|
||||||
|
static ConfigKey get_config_key(const char *key) {
|
||||||
|
static const struct
|
||||||
|
{
|
||||||
|
const char *name;
|
||||||
|
ConfigKey key;
|
||||||
|
} key_map[] = {
|
||||||
|
{"port", CONFIG_PORT},
|
||||||
|
{"use_https", CONFIG_USE_HTTPS},
|
||||||
|
{"log_file", CONFIG_LOG_FILE},
|
||||||
|
{"max_threads", CONFIG_MAX_THREADS},
|
||||||
|
{"running", CONFIG_RUNNING},
|
||||||
|
{"server_name", CONFIG_SERVER_NAME},
|
||||||
|
{"verbose", CONFIG_VERBOSE},
|
||||||
|
{"enable_http2", CONFIG_ENABLE_HTTP2},
|
||||||
|
{"enable_websocket",CONFIG_ENABLE_WEBSOCKET},
|
||||||
|
{NULL, CONFIG_UNKNOWN}
|
||||||
|
|
||||||
|
};
|
||||||
|
for (int i = 0;key_map[i].name != NULL; i++) {
|
||||||
|
if (strcasecmp(key, key_map[i].name) == 0) {
|
||||||
|
return key_map[i].key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return CONFIG_UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
int load_config(const char *filename, ServerConfig *config) {
|
int load_config(const char *filename, ServerConfig *config) {
|
||||||
FILE *fp = fopen(filename, "r");
|
FILE *fp = fopen(filename, "r");
|
||||||
@@ -80,54 +120,65 @@ int load_config(const char *filename, ServerConfig *config) {
|
|||||||
value[strlen(value) - 1] = '\0';
|
value[strlen(value) - 1] = '\0';
|
||||||
value++;
|
value++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse configuration options
|
// Parse configuration options
|
||||||
if (strcasecmp(key, "port") == 0) {
|
switch (get_config_key(key)) {
|
||||||
|
case CONFIG_PORT:
|
||||||
config->port = atoi(value);
|
config->port = atoi(value);
|
||||||
printf("load_config: port = %d\n", config->port);
|
printf("load_config: port = %d\n", config->port);
|
||||||
}
|
break;
|
||||||
else if (strcasecmp(key, "use_https") == 0) {
|
|
||||||
|
case CONFIG_USE_HTTPS:
|
||||||
config->use_https = parse_bool(value);
|
config->use_https = parse_bool(value);
|
||||||
printf("load_config: use_https = %d\n", config->use_https);
|
printf("load_config: use_https = %d\n", config->use_https);
|
||||||
}
|
break;
|
||||||
else if (strcasecmp(key, "log_file") == 0) {
|
|
||||||
|
case CONFIG_LOG_FILE:
|
||||||
strncpy(config->log_file, value, sizeof(config->log_file) - 1);
|
strncpy(config->log_file, value, sizeof(config->log_file) - 1);
|
||||||
config->log_file[sizeof(config->log_file) - 1] = '\0';
|
config->log_file[sizeof(config->log_file) - 1] = '\0';
|
||||||
printf("load_config: log_file = %s\n", config->log_file);
|
printf("load_config: log_file = %s\n", config->log_file);
|
||||||
}
|
break;
|
||||||
else if (strcasecmp(key, "max_threads") == 0) {
|
|
||||||
|
case CONFIG_MAX_THREADS:
|
||||||
config->max_threads = atoi(value);
|
config->max_threads = atoi(value);
|
||||||
printf("load_config: max_threads = %d\n", config->max_threads);
|
printf("load_config: max_threads = %d\n", config->max_threads);
|
||||||
}
|
break;
|
||||||
else if (strcasecmp(key, "running") == 0) {
|
|
||||||
|
case CONFIG_RUNNING:
|
||||||
config->running = parse_bool(value);
|
config->running = parse_bool(value);
|
||||||
printf("load_config: running = %d\n", config->running);
|
printf("load_config: running = %d\n", config->running);
|
||||||
}
|
break;
|
||||||
else if (strcasecmp(key, "server_name") == 0) {
|
|
||||||
|
case CONFIG_SERVER_NAME:
|
||||||
strncpy(config->server_name, value, sizeof(config->server_name) - 1);
|
strncpy(config->server_name, value, sizeof(config->server_name) - 1);
|
||||||
config->server_name[sizeof(config->server_name) - 1] = '\0';
|
config->server_name[sizeof(config->server_name) - 1] = '\0';
|
||||||
printf("load_config: server_name = %s\n", config->server_name);
|
printf("load_config: server_name = %s\n", config->server_name);
|
||||||
if (strcmp(config->server_name, "Your_domain/IP") == 0) {
|
if (strcmp(config->server_name, "Your_domain/IP") == 0) {
|
||||||
fprintf(stderr, "WARNING: server_name is set to default\nPlease set server_name in server.conf to the server's IP address or domain name for proper operation.\n");
|
fprintf(stderr, "WARNING: server_name is set to default\n"
|
||||||
|
"Please set server_name in server.conf to the server's IP address or domain name for proper operation.\n");
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
else if (strcasecmp(key, "verbose") == 0) {
|
|
||||||
|
case CONFIG_VERBOSE:
|
||||||
config->verbose = parse_bool(value);
|
config->verbose = parse_bool(value);
|
||||||
printf("load_config: verbose = %d\n", config->verbose);
|
printf("load_config: verbose = %d\n", config->verbose);
|
||||||
}
|
break;
|
||||||
else if (strcasecmp(key, "enable_http2") == 0) {
|
|
||||||
|
case CONFIG_ENABLE_HTTP2:
|
||||||
config->enable_http2 = parse_bool(value);
|
config->enable_http2 = parse_bool(value);
|
||||||
printf("load_config: enable_http2 = %d\n", config->enable_http2);
|
printf("load_config: enable_http2 = %d\n", config->enable_http2);
|
||||||
}
|
break;
|
||||||
else if (strcasecmp(key, "enable_websocket") == 0) {
|
|
||||||
|
case CONFIG_ENABLE_WEBSOCKET:
|
||||||
config->enable_websocket = parse_bool(value);
|
config->enable_websocket = parse_bool(value);
|
||||||
printf("load_config: enable_websocket = %d\n", config->enable_websocket);
|
printf("load_config: enable_websocket = %d\n", config->enable_websocket);
|
||||||
}
|
break;
|
||||||
else {
|
|
||||||
fprintf(stderr, "Warning: Unknown config option '%s' on line %d\n", key, line_number);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
case CONFIG_UNKNOWN:
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "Warning: Unknown config option '%s' on line %d\n", key, line_number);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user