1
0
mirror of https://github.com/falk-werner/webfused synced 2024-10-27 20:44:08 +00:00

refactor: use builder to create config

This commit is contained in:
Falk Werner 2020-03-15 22:35:57 +01:00
parent f3743a6e01
commit aaf8bcb688
5 changed files with 111 additions and 207 deletions

View File

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

View File

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

View File

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

View File

@ -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

20
test/test_config.cc Normal file
View File

@ -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);
}