Skip to content

Commit ba638ff

Browse files
committed
Update Docker support
1 parent da0c657 commit ba638ff

File tree

4 files changed

+66
-20
lines changed

4 files changed

+66
-20
lines changed

Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
FROM ubuntu AS builder
2-
WORKDIR /app
2+
WORKDIR /build
33
COPY httplib.h .
44
COPY docker/main.cc .
55
RUN apt update && apt install g++ -y
6-
RUN g++ -std=c++14 -static -o server -O3 -I. -DCPPHTTPLIB_USE_POLL main.cc
6+
RUN g++ -std=c++23 -static -o server -O2 -I. -DCPPHTTPLIB_USE_POLL main.cc && strip server
77

88
FROM scratch
9-
COPY --from=builder /app/server /server
10-
COPY docker/index.html /html/index.html
9+
COPY --from=builder /build/server /server
10+
COPY docker/html/index.html /html/index.html
11+
EXPOSE 80
1112
CMD ["/server"]

docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
http:
3+
build: .
4+
ports:
5+
- "8080:80"
6+
volumes:
7+
- ./docker/html:/html
File renamed without changes.

docker/main.cc

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,73 @@
55
// MIT License
66
//
77

8-
#include <cstdio>
9-
#include <httplib.h>
8+
#include <chrono>
9+
#include <ctime>
10+
#include <format>
11+
#include <iomanip>
1012
#include <iostream>
13+
#include <sstream>
1114

12-
using namespace httplib;
13-
using namespace std;
15+
#include <httplib.h>
1416

15-
auto error_html = R"(<html>
16-
<head><title>%d %s</title></head>
17+
constexpr auto error_html = R"(<html>
18+
<head><title>{} {}</title></head>
1719
<body>
1820
<center><h1>404 Not Found</h1></center>
19-
<hr><center>cpp-httplib/%s</center>
21+
<hr><center>cpp-httplib/{}</center>
2022
</body>
2123
</html>
2224
)";
2325

26+
std::string time_local() {
27+
auto p = std::chrono::system_clock::now();
28+
auto t = std::chrono::system_clock::to_time_t(p);
29+
30+
std::stringstream ss;
31+
ss << std::put_time(std::localtime(&t), "%d/%b/%Y:%H:%M:%S %z");
32+
return ss.str();
33+
}
34+
35+
std::string log(auto &req, auto &res) {
36+
auto remote_user = "-"; // TODO:
37+
auto request = std::format("{} {} {}", req.method, req.path, req.version);
38+
auto body_bytes_sent = res.get_header_value("Content-Length");
39+
auto http_referer = "-"; // TODO:
40+
auto http_user_agent = req.get_header_value("User-Agent", "-");
41+
42+
// NOTE: From NGINX defualt access log format
43+
// log_format combined '$remote_addr - $remote_user [$time_local] '
44+
// '"$request" $status $body_bytes_sent '
45+
// '"$http_referer" "$http_user_agent"';
46+
return std::format(R"({} - {} [{}] "{}" {} {} "{}" "{}")", req.remote_addr,
47+
remote_user, time_local(), request, res.status,
48+
body_bytes_sent, http_referer, http_user_agent);
49+
}
50+
2451
int main(int argc, const char **argv) {
25-
Server svr;
52+
auto base_dir = "./html";
53+
auto host = "0.0.0.0";
54+
auto port = 80;
55+
56+
httplib::Server svr;
2657

27-
svr.set_error_handler([](const Request & /*req*/, Response &res) {
28-
char buf[BUFSIZ];
29-
snprintf(buf, sizeof(buf), error_html, res.status,
30-
status_message(res.status), CPPHTTPLIB_VERSION);
31-
res.set_content(buf, "text/html");
58+
svr.set_error_handler([](auto & /*req*/, auto &res) {
59+
auto body =
60+
std::format(error_html, res.status, httplib::status_message(res.status),
61+
CPPHTTPLIB_VERSION);
62+
63+
res.set_content(body, "text/html");
3264
});
3365

34-
svr.set_mount_point("/", "./html");
66+
svr.set_logger(
67+
[](auto &req, auto &res) { std::cout << log(req, res) << std::endl; });
68+
69+
svr.set_mount_point("/", base_dir);
70+
71+
std::cout << std::format("Serving HTTP on {0} port {1} ...", host, port)
72+
<< std::endl;
3573

36-
svr.listen("0.0.0.0", 80);
74+
auto ret = svr.listen(host, port);
3775

38-
return 0;
76+
return ret ? 0 : 1;
3977
}

0 commit comments

Comments
 (0)