mirror of
https://github.com/falk-werner/webfused
synced 2026-03-02 04:09:19 +00:00
use builder to load config
This commit is contained in:
42
src/webfused/config/builder.c
Normal file
42
src/webfused/config/builder.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "webfused/config/builder.h"
|
||||
|
||||
void
|
||||
wfd_config_builder_set_server_vhostname(
|
||||
struct wfd_config_builder builder,
|
||||
char const * vhost_name)
|
||||
{
|
||||
builder.vtable->set_server_vhostname(builder.data, vhost_name);
|
||||
}
|
||||
|
||||
void
|
||||
wfd_config_builder_set_server_port(
|
||||
struct wfd_config_builder builder,
|
||||
int port)
|
||||
{
|
||||
builder.vtable->set_server_port(builder.data, port);
|
||||
}
|
||||
|
||||
void
|
||||
wfd_config_builder_set_server_key(
|
||||
struct wfd_config_builder builder,
|
||||
char const * key_path)
|
||||
{
|
||||
builder.vtable->set_server_key(builder.data, key_path);
|
||||
}
|
||||
|
||||
void
|
||||
wfd_config_builder_set_server_cert(
|
||||
struct wfd_config_builder builder,
|
||||
char const * cert_path)
|
||||
{
|
||||
builder.vtable->set_server_cert(builder.data, cert_path);
|
||||
}
|
||||
|
||||
void
|
||||
wfd_config_builder_set_server_document_root(
|
||||
struct wfd_config_builder builder,
|
||||
char const * document_root)
|
||||
{
|
||||
builder.vtable->set_server_document_root(builder.data, document_root);
|
||||
}
|
||||
|
||||
79
src/webfused/config/builder.h
Normal file
79
src/webfused/config/builder.h
Normal file
@@ -0,0 +1,79 @@
|
||||
#ifndef WFD_CONFIG_BUILDER_H
|
||||
#define WFD_CONFIG_BUILDER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef void
|
||||
wfd_config_builder_set_server_vhostname_fn(
|
||||
void * data,
|
||||
char const * vhost_name);
|
||||
|
||||
typedef void
|
||||
wfd_config_builder_set_server_port_fn(
|
||||
void * data,
|
||||
int port);
|
||||
|
||||
typedef void
|
||||
wfd_config_builder_set_server_key_fn(
|
||||
void * data,
|
||||
char const * key_path);
|
||||
|
||||
typedef void
|
||||
wfd_config_builder_set_server_cert_fn(
|
||||
void * data,
|
||||
char const * cert_path);
|
||||
|
||||
typedef void
|
||||
wfd_config_builder_set_server_document_root_fn(
|
||||
void * data,
|
||||
char const * document_root);
|
||||
|
||||
struct wfd_config_builder_vtable
|
||||
{
|
||||
wfd_config_builder_set_server_vhostname_fn * set_server_vhostname;
|
||||
wfd_config_builder_set_server_port_fn * set_server_port;
|
||||
wfd_config_builder_set_server_key_fn * set_server_key;
|
||||
wfd_config_builder_set_server_cert_fn * set_server_cert;
|
||||
wfd_config_builder_set_server_document_root_fn * set_server_document_root;
|
||||
};
|
||||
|
||||
struct wfd_config_builder
|
||||
{
|
||||
struct wfd_config_builder_vtable const * vtable;
|
||||
void * data;
|
||||
};
|
||||
|
||||
extern void
|
||||
wfd_config_builder_set_server_vhostname(
|
||||
struct wfd_config_builder builder,
|
||||
char const * vhost_name);
|
||||
|
||||
extern void
|
||||
wfd_config_builder_set_server_port(
|
||||
struct wfd_config_builder builder,
|
||||
int port);
|
||||
|
||||
extern void
|
||||
wfd_config_builder_set_server_key(
|
||||
struct wfd_config_builder builder,
|
||||
char const * key_path);
|
||||
|
||||
extern void
|
||||
wfd_config_builder_set_server_cert(
|
||||
struct wfd_config_builder builder,
|
||||
char const * cert_path);
|
||||
|
||||
extern void
|
||||
wfd_config_builder_set_server_document_root(
|
||||
struct wfd_config_builder builder,
|
||||
char const * document_root);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,6 +1,4 @@
|
||||
#include "webfused/config/factory.h"
|
||||
#include "webfused/config/config_intern.h"
|
||||
#include "webfused/config/config.h"
|
||||
#include "webfused/log/log.h"
|
||||
|
||||
#include <libconfig.h>
|
||||
@@ -59,76 +57,72 @@ wfd_config_check_version(
|
||||
static bool
|
||||
wfd_config_read_server(
|
||||
config_t * config,
|
||||
struct wfd_config * result)
|
||||
struct wfd_config_builder builder)
|
||||
{
|
||||
char const * vhost_name;
|
||||
int rc = config_lookup_string(config, "server.vhost_name", &vhost_name);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
wfd_config_set_server_vhostname(result, vhost_name);
|
||||
wfd_config_builder_set_server_vhostname(builder, vhost_name);
|
||||
}
|
||||
|
||||
int port;
|
||||
rc = config_lookup_int(config, "server.port", &port);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
wfd_config_set_server_port(result, port);
|
||||
wfd_config_builder_set_server_port(builder, port);
|
||||
}
|
||||
|
||||
char const * cert;
|
||||
rc = config_lookup_string(config, "server.tls.certificate", &cert);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
wfd_config_set_server_cert(result, cert);
|
||||
wfd_config_builder_set_server_cert(builder, cert);
|
||||
}
|
||||
|
||||
char const * key;
|
||||
rc = config_lookup_string(config, "server.tls.key", &key);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
wfd_config_set_server_key(result, key);
|
||||
wfd_config_builder_set_server_key(builder, key);
|
||||
}
|
||||
|
||||
char const * doc_root;
|
||||
rc = config_lookup_string(config, "server.document_root", &doc_root);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
wfd_config_set_server_document_root(result, doc_root);
|
||||
wfd_config_builder_set_server_document_root(builder, doc_root);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static struct wfd_config *
|
||||
wfd_config_load(config_t * config)
|
||||
static bool
|
||||
wfd_config_load(
|
||||
struct wfd_config_builder builder,
|
||||
config_t * config)
|
||||
{
|
||||
struct wfd_config * result = wfd_config_create();
|
||||
|
||||
bool success = wfd_config_check_version(config)
|
||||
&& wfd_config_read_server(config, result)
|
||||
bool result = wfd_config_check_version(config)
|
||||
&& wfd_config_read_server(config, builder)
|
||||
;
|
||||
|
||||
if (!success)
|
||||
{
|
||||
wfd_config_dispose(result);
|
||||
result = NULL;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct wfd_config *
|
||||
bool
|
||||
wfd_config_load_file(
|
||||
struct wfd_config_builder builder,
|
||||
char const * filename)
|
||||
{
|
||||
struct wfd_config * result = NULL;
|
||||
bool result = false;
|
||||
|
||||
config_t config;
|
||||
config_init(&config);
|
||||
int rc = config_read_file(&config, filename);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
result = wfd_config_load(&config);
|
||||
result = wfd_config_load(builder, &config);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -143,18 +137,19 @@ wfd_config_load_file(
|
||||
return result;
|
||||
}
|
||||
|
||||
struct wfd_config *
|
||||
bool
|
||||
wfd_config_load_string(
|
||||
struct wfd_config_builder builder,
|
||||
char const * contents)
|
||||
{
|
||||
struct wfd_config * result = NULL;
|
||||
bool result = false;
|
||||
|
||||
config_t config;
|
||||
config_init(&config);
|
||||
int rc = config_read_string(&config, contents);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
result = wfd_config_load(&config);
|
||||
result = wfd_config_load(builder, &config);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -1,17 +1,25 @@
|
||||
#ifndef WFD_CONFIG_FACTORY_H
|
||||
#define WFD_CONFIG_FACTORY_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include "webfused/config/builder.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern struct wfd_config *
|
||||
extern bool
|
||||
wfd_config_load_file(
|
||||
struct wfd_config_builder builder,
|
||||
char const * filename);
|
||||
|
||||
extern struct wfd_config *
|
||||
extern bool
|
||||
wfd_config_load_string(
|
||||
struct wfd_config_builder builder,
|
||||
char const * contents);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
Reference in New Issue
Block a user