feat: add healthcheck feature and update Dockerfile for build process

This commit is contained in:
Arthur K. 2025-01-21 14:20:30 +03:00
parent e78ac289f7
commit ef2f477fcd
Signed by: wzray
GPG key ID: B97F30FDC4636357
3 changed files with 29 additions and 16 deletions

View file

@ -1,11 +1,13 @@
#check=skip=JSONArgsRecommended #check=skip=JSONArgsRecommended
FROM debian:12-slim AS builder FROM debian:12-slim AS builder
RUN apt-get update && DEBIAN_FRONTEND=noninteracive apt-get install -y \ RUN apt-get update && DEBIAN_FRONTEND=noninteracive apt-get install -y \
--no-install-recommends cmake g++-mingw-w64-i686-posix make && \ --no-install-recommends cmake g++-mingw-w64-i686-posix g++ make && \
apt-get clean && rm -rf /var/lib/apt/lists/* apt-get clean && rm -rf /var/lib/apt/lists/*
WORKDIR /build WORKDIR /build
COPY . . COPY . .
RUN g++ -o healthcheck -I./include ./app/healthcheck.cpp
RUN --mount=type=cache,target=cmake-build cmake -B cmake-build \ RUN --mount=type=cache,target=cmake-build cmake -B cmake-build \
-DCMAKE_CXX_COMPILER=i686-w64-mingw32-g++ && cmake --build cmake-build -j$(nproc) && \ -DCMAKE_CXX_COMPILER=i686-w64-mingw32-g++ && cmake --build cmake-build -j$(nproc) && \
cp cmake-build/promt-puppy.exe . cp cmake-build/promt-puppy.exe .
@ -13,21 +15,23 @@ RUN --mount=type=cache,target=cmake-build cmake -B cmake-build \
FROM debian:12-slim AS runner FROM debian:12-slim AS runner
ENV WINEPREFIX=/wineprefix \ ENV WINEPREFIX=/wineprefix \
DEBIAN_FRONTEND=noninteracive DEBIAN_FRONTEND=noninteracive
RUN sed -i 's/main/main contrib/' /etc/apt/sources.list.d/debian.sources && \ RUN dpkg --add-architecture i386 && apt-get update && \
dpkg --add-architecture i386 && apt-get update && \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
wine wine64 wine32:i386 ca-certificates && \ wine wine64 wine32:i386 && \
apt-get clean && rm -rf /var/lib/apt/lists/* && \
wineboot -i && echo "Sleeping for 15 seconds..." && sleep 15 && \ wineboot -i && echo "Sleeping for 15 seconds..." && sleep 15 && \
apt-get clean && rm -rf /var/lib/apt/lists/* mkdir /tmpfs && ln -sf /tmpfs $WINEPREFIX/drive_c/tmpfs
COPY build/ $WINEPREFIX/drive_c COPY build/ $WINEPREFIX/drive_c
RUN wine regedit $WINEPREFIX/drive_c/registry.reg && \ RUN wine regedit $WINEPREFIX/drive_c/registry.reg && \
mkdir /tmpfs && ln -sf /tmpfs $WINEPREFIX/drive_c/tmpfs && \
echo "Sleeping for 15 seconds" && sleep 15 echo "Sleeping for 15 seconds" && sleep 15
WORKDIR /app WORKDIR /app
COPY --from=builder /build/promt-puppy.exe . COPY --from=builder /build/promt-puppy.exe /build/healthcheck .
HEALTHCHECK CMD ./healthcheck
EXPOSE 80/tcp EXPOSE 80/tcp
VOLUME /cache VOLUME /cache

View file

@ -0,0 +1,6 @@
#include "httplib.hpp"
int main() {
auto res = httplib::Client("127.0.0.1", 80).Get("/health");
return !(res && res->status == 200);
}

View file

@ -4,16 +4,10 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <random>
#include "PromtCtlDocument.hpp" #include "PromtCtlDocument.hpp"
#include "PromtFTManager.hpp" #include "PromtFTManager.hpp"
template<typename... T>
static inline void print(T... args) {
((std::cout << args << ' '), ...) << std::endl;
}
static inline std::string random_filename(int len = 65) { static inline std::string random_filename(int len = 65) {
static const char ASCII_PRINTABLE[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; static const char ASCII_PRINTABLE[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
std::random_device random_device; std::random_device random_device;
@ -26,10 +20,15 @@ static inline std::string random_filename(int len = 65) {
return random_string; return random_string;
} }
static const char TMP_FOLDER[] = "C:\\tmpfs\\"; static constexpr std::string TMP_FOLDER = "C:\\tmpfs\\";
static const auto TMP_IN = TMP_FOLDER + random_filename(); static const auto TMP_IN = TMP_FOLDER + random_filename();
static const auto TMP_OUT = TMP_FOLDER + random_filename(); static const auto TMP_OUT = TMP_FOLDER + random_filename();
template<typename... T>
static inline void print(T... args) {
((std::cout << args << ' '), ...) << std::endl;
}
static inline auto utf8_to_cp(const char *data) { static inline auto utf8_to_cp(const char *data) {
int size = MultiByteToWideChar(CP_UTF8, 0, data, -1, 0, 0); int size = MultiByteToWideChar(CP_UTF8, 0, data, -1, 0, 0);
auto wstr = new wchar_t[size]; auto wstr = new wchar_t[size];
@ -86,7 +85,7 @@ class WebServer {
ofs << utf8_to_cp(text.c_str()); ofs << utf8_to_cp(text.c_str());
} }
translator.Translate(TMP_IN, TMP_OUT); translator.Translate(TMP_IN, TMP_OUT);
res.set_content(read_file(TMP_OUT), "text/html"); res.set_content(std::move(read_file(TMP_OUT)), "text/html");
} }
void TranslateText(const std::string &text, httplib::Response &res) { void TranslateText(const std::string &text, httplib::Response &res) {
@ -146,9 +145,13 @@ class WebServer {
if (!std::filesystem::exists(TMP_FOLDER)) std::filesystem::create_directory(TMP_FOLDER); if (!std::filesystem::exists(TMP_FOLDER)) std::filesystem::create_directory(TMP_FOLDER);
m_svr.Post("/translate", [this](const httplib::Request &req, httplib::Response &res) { m_svr.Post("/translate", [this](const httplib::Request &req, httplib::Response &res) {
print("Got request!");
TranslateHandler(req, res); TranslateHandler(req, res);
}); });
m_svr.Get("/health", [](const httplib::Request &req, httplib::Response &res) {
res.set_content("OK", "text/plain");
res.status = 200;
});
} }
~WebServer() { ~WebServer() {