From d8c3333e7a7330c10bb96e426482e2b158011251 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Fri, 3 Apr 2020 13:54:08 +0200 Subject: Added configuration file (WIP) --- Makefile | 8 ++++++++ config.cpp | 35 +++++++++++++++++++++++++++++++++++ config.h | 21 +++++++++++++++++++++ webserver.conf | 40 ++++++++++++++++++++++++++++++++++++++++ webserver.cpp | 36 +++++++++++++++++++++++++++++++++++- 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 config.cpp create mode 100644 config.h create mode 100644 webserver.conf diff --git a/Makefile b/Makefile index 7794af6..bb93b0d 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ LIBS+= \ endif PROGSRC=\ + config.cpp \ http.cpp \ http_debian10.cpp @@ -69,6 +70,7 @@ SRC=$(PROGSRC) webserver.cpp all: test-$(PROJECTNAME) $(PROJECTNAME) ./test-$(PROJECTNAME) + ./webserver -c webserver.conf # testsuite ---------------------------------------------- test-$(PROJECTNAME): $(TESTSRC:.cpp=.o) @@ -96,6 +98,12 @@ install: mkdir -p $(DESTDIR)/usr/bin cp webserver $(DESTDIR)/usr/bin + mkdir -p $(DESTDIR)/usr/lib/webserver/plugins + mkdir -p $(DESTDIR)/usr/local/lib/webserver/plugins + + mkdir -p $(DESTDIR)/etc + cp webserver.conf $(DESTDIR)/etc/webserver.conf + # misc --------------------------------------------------- deb: # build binary deb package diff --git a/config.cpp b/config.cpp new file mode 100644 index 0000000..edbe3c4 --- /dev/null +++ b/config.cpp @@ -0,0 +1,35 @@ +#include "config.h" + +#include +#include + +namespace pt = boost::property_tree; + +void Config::readConfigfile(std::string filename) +{ + if (filename == "") { + filename = default_filename; + } + + pt::ptree tree; + + pt::read_xml(filename, tree); + + m_user = tree.get("webserver.user"); + m_group = tree.get("webserver.group1"); +} + +Config::Config(const std::string& filename) +{ + readConfigfile(filename); +} + +std::string Config::User() const +{ + return m_user; +} + +std::string Config::Group() const +{ + return m_group; +} diff --git a/config.h b/config.h new file mode 100644 index 0000000..b1f17a3 --- /dev/null +++ b/config.h @@ -0,0 +1,21 @@ +#pragma once + +#include + +class Config +{ + const std::string default_filename{"/etc/webserver.conf"}; + + void readConfigfile(std::string filename); + + std::string m_user; + std::string m_group; + + public: + Config(const std::string& filename); + + // Data getters + std::string User() const; + std::string Group() const; +}; + diff --git a/webserver.conf b/webserver.conf new file mode 100644 index 0000000..c3580c8 --- /dev/null +++ b/webserver.conf @@ -0,0 +1,40 @@ + + www-data + www-data + /usr/lib/webserver/plugins + /usr/local/lib/webserver/plugins + + + antcom.de + antcom.de + /var/www/antcom.de + webbox + + + + + +
127.0.0.1
+ 80 + http + +
+ +
127.0.0.1
+ 443 + https +
+
+
+ diff --git a/webserver.cpp b/webserver.cpp index 4b6a89c..b079707 100644 --- a/webserver.cpp +++ b/webserver.cpp @@ -1,6 +1,40 @@ +#include "config.h" #include "http.h" +#include +#include +#include + +using namespace std::string_literals; + +void usage() +{ + std::cout << "usage: webserver [-c ]" << std::endl; +} + int main(int argc, char* argv[]) { - return http_server(argc, argv); + std::string config_filename; + + if (!(argc == 1 || argc == 3)) { + usage(); + return 1; + } + + if (argc == 3) { + if (argv[1] != "-c"s) { + usage(); + return 1; + } + + config_filename = argv[2]; + } + + try { + Config config{config_filename}; + return http_server(argc, argv); + } catch (const std::exception& ex) { + std::cout << "Error: " << ex.what() << std::endl; + return 1; + } } -- cgit v1.2.3