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:
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 *
|
||||
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)
|
||||
{
|
||||
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;
|
||||
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);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
void wfd_config_dispose(
|
||||
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);
|
||||
wf_server_config_dispose(config->server);
|
||||
free(config);
|
||||
}
|
||||
|
||||
int
|
||||
wfd_config_get_server_port(
|
||||
struct wfd_config_builder
|
||||
wfd_config_get_builder(
|
||||
struct wfd_config * config)
|
||||
{
|
||||
return config->port;
|
||||
struct wfd_config_builder builder =
|
||||
{
|
||||
&wfd_config_vtable_config_builder,
|
||||
config
|
||||
};
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
void
|
||||
wfd_config_set_server_port(
|
||||
struct wfd_config * config,
|
||||
int port)
|
||||
{
|
||||
config->port = port;
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_config_get_server_vhostname(
|
||||
struct wf_server_config *
|
||||
wfd_config_get_server_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;
|
||||
}
|
||||
|
@ -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;
|
||||
struct wf_server_config;
|
||||
|
||||
extern void wfd_config_dispose(
|
||||
extern struct wfd_config *
|
||||
wfd_config_create(void);
|
||||
|
||||
extern void
|
||||
wfd_config_dispose(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern int
|
||||
wfd_config_get_server_port(
|
||||
extern struct wfd_config_builder
|
||||
wfd_config_get_builder(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern char const *
|
||||
wfd_config_get_server_vhostname(
|
||||
extern struct wf_server_config *
|
||||
wfd_config_get_server_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
|
||||
}
|
||||
#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
|
20
test/test_config.cc
Normal file
20
test/test_config.cc
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user