From c7743beac82c81e6e46871f1922ba3478445ca9a Mon Sep 17 00:00:00 2001 From: alistair Date: Sun, 4 Apr 2021 19:59:13 +1000 Subject: [PATCH] bundle darkhttp for testing --- CMakeLists.txt | 2 +- main.cpp | 6 ++++++ util.cpp | 41 ++++++++++++++++++++++++++++++++++++++--- util.h | 21 +++++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7825580..81e4d42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,7 +42,7 @@ if(NOT TARGET spdlog) endif() -ADD_EXECUTABLE(stgen3 util.cpp markdown.cpp main.cpp) +ADD_EXECUTABLE(stgen3 util.cpp darkhttpd.c markdown.cpp main.cpp) target_include_directories(stgen3 PRIVATE include) diff --git a/main.cpp b/main.cpp index 6c5d935..187892a 100644 --- a/main.cpp +++ b/main.cpp @@ -735,5 +735,11 @@ int main(int argc, char **argv) { stgen::builder b (cmd_options); b.build(); + server s {}; + s.serve_now("./llll", "8080", "127.0.0.1"); + + unsigned int microsecond = 1000000; + usleep(100000 * microsecond);//sleeps for 3 second + return 0; } diff --git a/util.cpp b/util.cpp index fcd503b..17aa003 100644 --- a/util.cpp +++ b/util.cpp @@ -1,5 +1,7 @@ #include "util.h" +#include +#include std::string file_ext(std::string path) @@ -20,9 +22,6 @@ std::string read_file(std::string const &fpath) { return sstr.str(); } - - - std::string reformat_date(const std::string& date_time, const std::map &properties) { @@ -90,3 +89,39 @@ compute_url(fs::path path, std::map properties) return url.string(); } }; + + +void server::serve_now(std::string wroot, std::string port, std::string addr) { + + if (spid) { + // already serving + return; + } + + int pid; + if (!(pid = fork())) { + + const char *argv[] = {"stgen3", wroot.c_str(), "--port", port.c_str(), + "--addr", addr.c_str()}; + int argc = 6; + + darkhttpd::darkhttpd_main(argc, argv); + + } else { + this->spid = pid; + return; + } + +} + +void server::stop_serving() { + kill(spid, SIGTERM); + waitpid(spid, NULL, 0); + spid = 0; +} + +server::~server() { + if (spid) { + stop_serving(); + } +} diff --git a/util.h b/util.h index fa564e9..383edd5 100644 --- a/util.h +++ b/util.h @@ -51,5 +51,26 @@ compute_url(fs::path path, std::map properties); }; +namespace darkhttpd { + +extern "C" { +int darkhttpd_main(int argc, const char **argv); +} + + +}; + + +class server { + int spid = 0; + public: + void serve_now(std::string wroot,std::string port,std::string addr); + void stop_serving(); + + ~server(); +}; + + + #endif