Skip to content

Commit

Permalink
move webserver files into the binary, so the promise of a single bina…
Browse files Browse the repository at this point in the history
…ry is true again
  • Loading branch information
berthubert committed Apr 14, 2024
1 parent 0b9df35 commit a37d347
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/meson.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ jobs:
- uses: actions/checkout@v3

- name: Install various apt dependencies
run: sudo apt-get install libsqlite3-dev python3-pip libnghttp2-dev pkg-config libssl-dev liblua5.3-dev
run: sudo apt-get install libsqlite3-dev python3-pip libnghttp2-dev pkg-config libssl-dev liblua5.3-dev xxd

- name: Install meson
run: sudo pip3 install meson ninja


- name: Configure Meson
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
Expand Down
1 change: 1 addition & 0 deletions Dockerfile.full-build
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN apt-get update -yqq && apt-get install -yqq \
openssl \
libssl-dev \
zlib1g-dev \
xxd \
libsqlite3-dev
COPY . /src
WORKDIR /src
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ check the log with `journalctl -u simplomon.service`
On Debian derived systems the following works:

```
apt install python3-pip pkg-config libnghttp2-dev libssl-dev liblua5.3-dev
apt install python3-pip pkg-config libnghttp2-dev libssl-dev liblua5.3-dev xxd
```
In addition, the project requires a recent version of meson, which you can
get with 'pip3 install meson ninja' or perhaps 'pip install
Expand Down
31 changes: 31 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,38 @@ doctest_dep=dependency('doctest')
simplesockets_dep = dependency('simplesockets', static: true)
# argparse_dep = dependency('argparse', version: '>=3')

prog_xxd = find_program('xxd')

index_html_h = custom_target(
'index_html.h', output : 'index_html.h', input : 'html/index.html',
command : [prog_xxd, '-i', '@INPUT@', '@OUTPUT@'],
)

style_css_h = custom_target(
'style_css.h', output : 'style_css.h', input : 'html/style.css',
command : [prog_xxd, '-i', '@INPUT@', '@OUTPUT@'],
)

simplomon_ico_h = custom_target(
'simplomon_ico.h', output : 'simplomon_ico.h', input : 'html/simplomon.ico',
command : [prog_xxd, '-i', '@INPUT@', '@OUTPUT@'],
)

alpine_min_js_h = custom_target(
'alpine_min_js.h', output : 'alpine_min_js.h', input : 'html/alpine.min.js',
command : [prog_xxd, '-i', '@INPUT@', '@OUTPUT@'],
)

logic_js_h = custom_target(
'logic_js.h', output : 'logic_js.h', input : 'html/logic.js',
command : [prog_xxd, '-i', '@INPUT@', '@OUTPUT@'],
)


webpages = [logic_js_h, alpine_min_js_h, simplomon_ico_h, style_css_h, index_html_h]

executable('simplomon', 'simplomon.cc', 'notifiers.cc', 'minicurl.cc', 'dnsmon.cc', 'record-types.cc', 'dnsmessages.cc', 'dns-storage.cc', 'netmon.cc', 'luabridge.cc', 'webservice.cc', 'support.cc', 'promon.cc', 'mailmon.cc', 'nonblocker.cc',
webpages,
dependencies: [json_dep, fmt_dep, cpphttplib,
simplesockets_dep, lua_dep, curl_dep, sqlite_dep, sqlitewriter_dep])

Expand Down
31 changes: 25 additions & 6 deletions webservice.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <openssl/bio.h>
#include <openssl/evp.h>

#include "index_html.h"
#include "style_css.h"
#include "logic_js.h"
#include "alpine_min_js.h"
#include "simplomon_ico.h"
using namespace std;

static std::mutex s_lock;
Expand Down Expand Up @@ -78,9 +83,8 @@ void updateWebService()
}
}

static void webserverThread(std::unique_ptr<httplib::Server> svr, string addr)
static void webserverThread(std::unique_ptr<httplib::Server> svr, ComboAddress ca)
{
ComboAddress ca(addr, 8080);
if(svr->listen(ca.toString(), ntohs(ca.sin4.sin_port))) {
cout<<"Error launching server: "<<strerror(errno)<<endl;
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -134,7 +138,6 @@ static bool checkAuth(const httplib::Request& req, httplib::Response &res)

if(!g_webpassword || g_webpassword->empty())
goto fail;


if(B64Decode(auth.substr(6), dec))
goto fail;
Expand Down Expand Up @@ -204,10 +207,26 @@ void startWebService(sol::table data)
res.set_content(s_checkerstates.dump(), "application/json");
});

svr->set_mount_point("/", "./html");
svr->Get("/simplomon.ico", [](const auto& req, auto& res) {
res.set_content(string((const char*)___html_simplomon_ico, ___html_simplomon_ico_len), "image/x-icon");
});
svr->Get("/alpine.min.js", [](const auto& req, auto& res) {
res.set_content(string((const char*)___html_alpine_min_js, ___html_alpine_min_js_len), "application/javascript");
});
svr->Get("/logic.js", [](const auto& req, auto& res) {
res.set_content(string((const char*)___html_logic_js, ___html_logic_js_len), "application/javascript");
});
svr->Get("/style.css", [](const auto& req, auto& res) {
res.set_content(string((const char*)___html_style_css, ___html_style_css_len), "text/css");
});
svr->Get("/", [](const auto& req, auto& res) {
res.set_content(string((const char*)___html_index_html, ___html_index_html_len), "text/html");
});


std::thread t(webserverThread, std::move(svr), data.get_or("address", string("0.0.0.0:8080")));
string addr = data.get_or("address", string("0.0.0.0:8080"));
ComboAddress ca(addr, 8080);
fmt::print("Going to launch webserver on {}\n", ca.toStringWithPort());
std::thread t(webserverThread, std::move(svr), ca);
t.detach();
}

0 comments on commit a37d347

Please sign in to comment.