From 52a18c78a52b1bcffc10506fe5fdc76568b3c646 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 27 Aug 2024 00:23:31 -0400 Subject: [PATCH] Add docker related files --- Dockerfile | 11 +++++++++++ docker/index.html | 21 +++++++++++++++++++++ docker/main.cc | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 Dockerfile create mode 100644 docker/index.html create mode 100644 docker/main.cc diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ec82596 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM ubuntu AS builder +WORKDIR /app +COPY httplib.h . +COPY docker/main.cc . +RUN apt update && apt install g++ -y +RUN g++ -std=c++14 -static -o server -O3 -I. -DCPPHTTPLIB_USE_POLL main.cc + +FROM scratch +COPY --from=builder /app/server /server +COPY docker/index.html /html/index.html +CMD ["/server"] diff --git a/docker/index.html b/docker/index.html new file mode 100644 index 0000000..1abe978 --- /dev/null +++ b/docker/index.html @@ -0,0 +1,21 @@ + + + +Welcome to cpp-httplib! + + + +

Welcome to cpp-httplib!

+

If you see this page, the cpp-httplib web server is successfully installed and +working. Further configuration is required.

+ +

For online documentation and support please refer to +github.com/yhirose/cpp-httplib.
+ +

Thank you for using cpp-httplib.

+ + diff --git a/docker/main.cc b/docker/main.cc new file mode 100644 index 0000000..1f65cb6 --- /dev/null +++ b/docker/main.cc @@ -0,0 +1,39 @@ +// +// main.cc +// +// Copyright (c) 2024 Yuji Hirose. All rights reserved. +// MIT License +// + +#include +#include +#include + +using namespace httplib; +using namespace std; + +auto error_html = R"( +%d %s + +

404 Not Found

+
cpp-httplib/%s
+ + +)"; + +int main(int argc, const char **argv) { + Server svr; + + svr.set_error_handler([](const Request & /*req*/, Response &res) { + char buf[BUFSIZ]; + snprintf(buf, sizeof(buf), error_html, res.status, + status_message(res.status), CPPHTTPLIB_VERSION); + res.set_content(buf, "text/html"); + }); + + svr.set_mount_point("/", "./html"); + + svr.listen("0.0.0.0", 80); + + return 0; +}