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_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}

@ -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_create(void) wfd_config_set_server_vhostname(
{ void * data,
struct wfd_config * config = malloc(sizeof(struct wfd_config)); char const * vhost_name)
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)
{ {
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( wfd_config_set_server_port(
struct wfd_config * config, void * data,
int port) int port)
{ {
config->port = port; struct wfd_config * config = data;
wf_server_config_set_port(config->server, port);
} }
char const * static void
wfd_config_get_server_vhostname( wfd_config_set_server_key(
struct wfd_config * config) 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 static void
wfd_config_set_server_vhostname( wfd_config_set_server_cert(
struct wfd_config * config, void * data,
char const * vhost_name) char const * cert_path)
{ {
free(config->vhost_name); struct wfd_config * config = data;
config->vhost_name = strdup(vhost_name); wf_server_config_set_certpath(config->server, cert_path);
} }
bool static void
wfd_config_is_server_tls_enabled( wfd_config_set_server_document_root(
struct wfd_config * config) void * data,
char const * document_root)
{ {
return ((NULL != config->server_key) struct wfd_config * config = data;
&& (NULL != config->server_cert)); wf_server_config_set_documentroot(config->server, document_root);
} }
char const * static const struct wfd_config_builder_vtable
wfd_config_get_server_cert( wfd_config_vtable_config_builder =
struct wfd_config * config)
{ {
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 struct wfd_config *
wfd_config_set_server_cert( wfd_config_create(void)
struct wfd_config * config,
char const * cert)
{ {
free(config->server_cert); struct wfd_config * config = malloc(sizeof(struct wfd_config));
config->server_cert = strdup(cert); 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 * return config;
wfd_config_get_server_key(
struct wfd_config * config)
{
return config->server_key;
} }
void void
wfd_config_set_server_key( wfd_config_dispose(
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) struct wfd_config * config)
{ {
return config->server_doc_root; wf_server_config_dispose(config->server);
} free(config);
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 * struct wfd_config_builder
wfd_config_get_auth_provider( wfd_config_get_builder(
struct wfd_config * config) struct wfd_config * config)
{ {
return NULL; struct wfd_config_builder builder =
} {
&wfd_config_vtable_config_builder,
config
};
struct wfd_auth_settings * return builder;
wfd_config_get_auth_settings(
struct wfd_config * config)
{
return NULL;
} }
size_t struct wf_server_config *
wfd_config_get_filesystem_count( wfd_config_get_server_config(
struct wfd_config * 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 #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(
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);
extern char const * extern struct wfd_config *
wfd_config_get_server_document_root( wfd_config_create(void);
struct wfd_config * config);
extern char const * extern void
wfd_config_get_auth_provider( wfd_config_dispose(
struct wfd_config * config); struct wfd_config * config);
extern struct wfd_auth_settings * extern struct wfd_config_builder
wfd_config_get_auth_settings( wfd_config_get_builder(
struct wfd_config * config); struct wfd_config * config);
extern size_t extern struct wf_server_config *
wfd_config_get_filesystem_count( wfd_config_get_server_config(
struct wfd_config * config); 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

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