refactor: use builder to create config

pull/1/head
Falk Werner 4 years ago
parent f3743a6e01
commit aaf8bcb688

@ -109,6 +109,7 @@ add_executable(alltests
test/mock_config_builder.cc
test/mock_logger.cc
test/test_config_factory.cc
test/test_config.cc
test/test_log.cc
)
@ -123,6 +124,8 @@ target_compile_options(alltests PRIVATE ${GMOCK_CFLAGS} ${GTEST_CFLAGS} "-pthrea
target_link_libraries(alltests PRIVATE
webfused-static
${LIBCONFIG_LIBRARIES}
${WEBFUSE_LIBRARIES}
${UUID_LIBRARIES}
${GMOCK_LIBRARIES}
${GTEST_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}

@ -1,156 +1,107 @@
#include "webfused/config/config.h"
#include "webfused/config/config_intern.h"
#include "webfuse/adapter/server_config.h"
#include <stdlib.h>
#include <string.h>
#define WFD_CONFIG_DEFAULT_PORT (8080)
#define WFD_CONFIG_DEFAULT_VHOSTNAME ("localhost")
struct wfd_config
{
char * vhost_name;
char * server_cert;
char * server_key;
char * server_doc_root;
int port;
struct wf_server_config * server;
};
extern struct wfd_config *
wfd_config_create(void)
{
struct wfd_config * config = malloc(sizeof(struct wfd_config));
config->port = WFD_CONFIG_DEFAULT_PORT;
config->vhost_name = strdup(WFD_CONFIG_DEFAULT_VHOSTNAME);
config->server_key = NULL;
config->server_cert = NULL;
config->server_doc_root = NULL;
return config;
}
void wfd_config_dispose(
struct wfd_config * config)
{
free(config->vhost_name);
free(config->server_cert);
free(config->server_key);
free(config->server_doc_root);
free(config);
}
int
wfd_config_get_server_port(
struct wfd_config * config)
static void
wfd_config_set_server_vhostname(
void * data,
char const * vhost_name)
{
return config->port;
struct wfd_config * config = data;
wf_server_config_set_vhostname(config->server, vhost_name);
}
void
static void
wfd_config_set_server_port(
struct wfd_config * config,
void * data,
int port)
{
config->port = port;
struct wfd_config * config = data;
wf_server_config_set_port(config->server, port);
}
char const *
wfd_config_get_server_vhostname(
struct wfd_config * config)
static void
wfd_config_set_server_key(
void * data,
char const * key_path)
{
return config->vhost_name;
struct wfd_config * config = data;
wf_server_config_set_keypath(config->server, key_path);
}
void
wfd_config_set_server_vhostname(
struct wfd_config * config,
char const * vhost_name)
static void
wfd_config_set_server_cert(
void * data,
char const * cert_path)
{
free(config->vhost_name);
config->vhost_name = strdup(vhost_name);
struct wfd_config * config = data;
wf_server_config_set_certpath(config->server, cert_path);
}
bool
wfd_config_is_server_tls_enabled(
struct wfd_config * config)
static void
wfd_config_set_server_document_root(
void * data,
char const * document_root)
{
return ((NULL != config->server_key)
&& (NULL != config->server_cert));
struct wfd_config * config = data;
wf_server_config_set_documentroot(config->server, document_root);
}
char const *
wfd_config_get_server_cert(
struct wfd_config * config)
static const struct wfd_config_builder_vtable
wfd_config_vtable_config_builder =
{
return config->server_cert;
}
.set_server_vhostname = &wfd_config_set_server_vhostname,
.set_server_port = &wfd_config_set_server_port,
.set_server_key = &wfd_config_set_server_key,
.set_server_cert = &wfd_config_set_server_cert,
.set_server_document_root = &wfd_config_set_server_document_root
};
void
wfd_config_set_server_cert(
struct wfd_config * config,
char const * cert)
struct wfd_config *
wfd_config_create(void)
{
free(config->server_cert);
config->server_cert = strdup(cert);
}
struct wfd_config * config = malloc(sizeof(struct wfd_config));
config->server = wf_server_config_create();
wf_server_config_set_vhostname(config->server, WFD_CONFIG_DEFAULT_VHOSTNAME);
wf_server_config_set_port(config->server, WFD_CONFIG_DEFAULT_PORT);
char const *
wfd_config_get_server_key(
struct wfd_config * config)
{
return config->server_key;
return config;
}
void
wfd_config_set_server_key(
struct wfd_config * config,
char const * key)
{
free(config->server_key);
config->server_key = strdup(key);
}
char const *
wfd_config_get_server_document_root(
wfd_config_dispose(
struct wfd_config * config)
{
return config->server_doc_root;
}
void
wfd_config_set_server_document_root(
struct wfd_config * config,
char const * document_root)
{
free(config->server_doc_root);
config->server_doc_root = strdup(document_root);
wf_server_config_dispose(config->server);
free(config);
}
char const *
wfd_config_get_auth_provider(
struct wfd_config_builder
wfd_config_get_builder(
struct wfd_config * config)
{
return NULL;
}
struct wfd_config_builder builder =
{
&wfd_config_vtable_config_builder,
config
};
struct wfd_auth_settings *
wfd_config_get_auth_settings(
struct wfd_config * config)
{
return NULL;
return builder;
}
size_t
wfd_config_get_filesystem_count(
struct wf_server_config *
wfd_config_get_server_config(
struct wfd_config * config)
{
return 0;
return config->server;
}
struct wfd_filesystem_info *
wfd_confi_get_filesystem(
struct wfd_config * config,
size_t fs_index)
{
return NULL;
}

@ -1,6 +1,8 @@
#ifndef WFD_CONFIG_H
#define WFD_CONFIG_H
#include "webfused/config/builder.h"
#ifndef __cplusplus
#include <stdbool.h>
#include <stddef.h>
@ -14,54 +16,23 @@ extern "C"
#endif
struct wfd_config;
struct wfd_auth_settins;
struct wfd_filesystem_info;
extern void wfd_config_dispose(
struct wfd_config * config);
extern int
wfd_config_get_server_port(
struct wfd_config * config);
extern char const *
wfd_config_get_server_vhostname(
struct wfd_config * config);
extern char const *
wfd_config_get_server_cert(
struct wfd_config * config);
extern bool
wfd_config_is_server_tls_enabled(
struct wfd_config * config);
extern char const *
wfd_config_get_server_key(
struct wfd_config * config);
struct wf_server_config;
extern char const *
wfd_config_get_server_document_root(
struct wfd_config * config);
extern struct wfd_config *
wfd_config_create(void);
extern char const *
wfd_config_get_auth_provider(
extern void
wfd_config_dispose(
struct wfd_config * config);
extern struct wfd_auth_settings *
wfd_config_get_auth_settings(
extern struct wfd_config_builder
wfd_config_get_builder(
struct wfd_config * config);
extern size_t
wfd_config_get_filesystem_count(
extern struct wf_server_config *
wfd_config_get_server_config(
struct wfd_config * config);
extern struct wfd_filesystem_info *
wfd_confi_get_filesystem(
struct wfd_config * config,
size_t fs_index);
#ifdef __cplusplus
}
#endif

@ -1,41 +0,0 @@
#ifndef WFD_CONFIG_INTERN_H
#define WFD_CONFIG_INTERN_H
#ifdef _cplusplus
extern "C"
{
#endif
extern struct wfd_config *
wfd_config_create(void);
extern void
wfd_config_set_server_vhostname(
struct wfd_config * config,
char const * vhost_name);
extern void
wfd_config_set_server_port(
struct wfd_config * config,
int port);
extern void
wfd_config_set_server_cert(
struct wfd_config * config,
char const * cert);
extern void
wfd_config_set_server_key(
struct wfd_config * config,
char const * key);
extern void
wfd_config_set_server_document_root(
struct wfd_config * config,
char const * document_root);
#ifdef _cplusplus
}
#endif
#endif

@ -0,0 +1,20 @@
#include <gtest/gtest.h>
#include "webfused/config/config.h"
TEST(config, server_config)
{
wfd_config * config = wfd_config_create();
ASSERT_NE(nullptr, config);
wfd_config_builder builder = wfd_config_get_builder(config);
wfd_config_builder_set_server_vhostname(builder, "localhost");
wfd_config_builder_set_server_port(builder, 8443);
wfd_config_builder_set_server_key(builder, "/path/to/key.pem");
wfd_config_builder_set_server_cert(builder, "/path/to/cert.pem");
wfd_config_builder_set_server_document_root(builder, "/var/www");
wf_server_config * server_config = wfd_config_get_server_config(config);
ASSERT_NE(nullptr, server_config);
wfd_config_dispose(config);
}
Loading…
Cancel
Save