Update Docker support
This commit is contained in:
parent
da0c6579fa
commit
ba638ff38e
@ -1,11 +1,12 @@
|
|||||||
FROM ubuntu AS builder
|
FROM ubuntu AS builder
|
||||||
WORKDIR /app
|
WORKDIR /build
|
||||||
COPY httplib.h .
|
COPY httplib.h .
|
||||||
COPY docker/main.cc .
|
COPY docker/main.cc .
|
||||||
RUN apt update && apt install g++ -y
|
RUN apt update && apt install g++ -y
|
||||||
RUN g++ -std=c++14 -static -o server -O3 -I. -DCPPHTTPLIB_USE_POLL main.cc
|
RUN g++ -std=c++23 -static -o server -O2 -I. -DCPPHTTPLIB_USE_POLL main.cc && strip server
|
||||||
|
|
||||||
FROM scratch
|
FROM scratch
|
||||||
COPY --from=builder /app/server /server
|
COPY --from=builder /build/server /server
|
||||||
COPY docker/index.html /html/index.html
|
COPY docker/html/index.html /html/index.html
|
||||||
|
EXPOSE 80
|
||||||
CMD ["/server"]
|
CMD ["/server"]
|
||||||
|
7
docker-compose.yml
Normal file
7
docker-compose.yml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
services:
|
||||||
|
http:
|
||||||
|
build: .
|
||||||
|
ports:
|
||||||
|
- "8080:80"
|
||||||
|
volumes:
|
||||||
|
- ./docker/html:/html
|
@ -5,35 +5,73 @@
|
|||||||
// MIT License
|
// MIT License
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <cstdio>
|
#include <chrono>
|
||||||
#include <httplib.h>
|
#include <ctime>
|
||||||
|
#include <format>
|
||||||
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
using namespace httplib;
|
#include <httplib.h>
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
auto error_html = R"(<html>
|
constexpr auto error_html = R"(<html>
|
||||||
<head><title>%d %s</title></head>
|
<head><title>{} {}</title></head>
|
||||||
<body>
|
<body>
|
||||||
<center><h1>404 Not Found</h1></center>
|
<center><h1>404 Not Found</h1></center>
|
||||||
<hr><center>cpp-httplib/%s</center>
|
<hr><center>cpp-httplib/{}</center>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
)";
|
)";
|
||||||
|
|
||||||
int main(int argc, const char **argv) {
|
std::string time_local() {
|
||||||
Server svr;
|
auto p = std::chrono::system_clock::now();
|
||||||
|
auto t = std::chrono::system_clock::to_time_t(p);
|
||||||
|
|
||||||
svr.set_error_handler([](const Request & /*req*/, Response &res) {
|
std::stringstream ss;
|
||||||
char buf[BUFSIZ];
|
ss << std::put_time(std::localtime(&t), "%d/%b/%Y:%H:%M:%S %z");
|
||||||
snprintf(buf, sizeof(buf), error_html, res.status,
|
return ss.str();
|
||||||
status_message(res.status), CPPHTTPLIB_VERSION);
|
}
|
||||||
res.set_content(buf, "text/html");
|
|
||||||
|
std::string log(auto &req, auto &res) {
|
||||||
|
auto remote_user = "-"; // TODO:
|
||||||
|
auto request = std::format("{} {} {}", req.method, req.path, req.version);
|
||||||
|
auto body_bytes_sent = res.get_header_value("Content-Length");
|
||||||
|
auto http_referer = "-"; // TODO:
|
||||||
|
auto http_user_agent = req.get_header_value("User-Agent", "-");
|
||||||
|
|
||||||
|
// NOTE: From NGINX defualt access log format
|
||||||
|
// log_format combined '$remote_addr - $remote_user [$time_local] '
|
||||||
|
// '"$request" $status $body_bytes_sent '
|
||||||
|
// '"$http_referer" "$http_user_agent"';
|
||||||
|
return std::format(R"({} - {} [{}] "{}" {} {} "{}" "{}")", req.remote_addr,
|
||||||
|
remote_user, time_local(), request, res.status,
|
||||||
|
body_bytes_sent, http_referer, http_user_agent);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, const char **argv) {
|
||||||
|
auto base_dir = "./html";
|
||||||
|
auto host = "0.0.0.0";
|
||||||
|
auto port = 80;
|
||||||
|
|
||||||
|
httplib::Server svr;
|
||||||
|
|
||||||
|
svr.set_error_handler([](auto & /*req*/, auto &res) {
|
||||||
|
auto body =
|
||||||
|
std::format(error_html, res.status, httplib::status_message(res.status),
|
||||||
|
CPPHTTPLIB_VERSION);
|
||||||
|
|
||||||
|
res.set_content(body, "text/html");
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.set_mount_point("/", "./html");
|
svr.set_logger(
|
||||||
|
[](auto &req, auto &res) { std::cout << log(req, res) << std::endl; });
|
||||||
|
|
||||||
svr.listen("0.0.0.0", 80);
|
svr.set_mount_point("/", base_dir);
|
||||||
|
|
||||||
return 0;
|
std::cout << std::format("Serving HTTP on {0} port {1} ...", host, port)
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
auto ret = svr.listen(host, port);
|
||||||
|
|
||||||
|
return ret ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user