feat(config): use toml config file

This commit is contained in:
2026-02-25 19:06:52 +01:00
parent a276e644a7
commit 825c02b626
10 changed files with 73 additions and 36 deletions
+21 -8
View File
@@ -27,7 +27,19 @@ static void worker_close_client(int epfd, int client_fd) {
close(client_fd);
}
static cws_return worker_handle_client_data(int epfd, int client_fd) {
static cws_vhost_s *get_vhost(cws_config_s *config, char *host) {
for (unsigned i = 0; i < config->virtual_hosts_count; ++i) {
cws_vhost_s *vh = config->virtual_hosts;
if (!strcmp(vh[i].domain, host)) {
return vh;
}
}
/* ?? */
return NULL;
}
static cws_return worker_handle_client_data(int epfd, int client_fd, cws_config_s *config) {
string_s *data = string_new("", 4096);
/* Read data from socket */
@@ -55,14 +67,15 @@ static cws_return worker_handle_client_data(int epfd, int client_fd) {
}
/* Configure handler */
/* TODO: use vhosts */
cws_handler_config_s config = {
.root_dir = "www",
.index_file = "index.html",
char *host = cws_http_get_host(request);
cws_vhost_s *vh = get_vhost(config, host);
cws_handler_config_s conf = {
.domain = vh->domain,
.root = vh->root,
};
/* Handle request and generate response */
cws_response_s *response = cws_handler_static_file(request, &config);
cws_response_s *response = cws_handler_static_file(request, &conf);
/* Send response */
if (response) {
@@ -81,7 +94,7 @@ static cws_return worker_handle_client_data(int epfd, int client_fd) {
/* Worker thread: process events on its epoll instance */
static void *cws_worker_loop(void *arg) {
cws_worker_s *worker = arg;
cws_worker_s *worker = (cws_worker_s *)arg;
struct epoll_event events[64];
while (cws_server_run) {
@@ -94,7 +107,7 @@ static void *cws_worker_loop(void *arg) {
for (int i = 0; i < nfds; ++i) {
int client_fd = events[i].data.fd;
worker_handle_client_data(worker->epfd, client_fd);
worker_handle_client_data(worker->epfd, client_fd, worker->config);
}
}