From c220610422acb9e13d4c19ee72a2547d6345621d Mon Sep 17 00:00:00 2001 From: "Arthur K." Date: Tue, 21 Jan 2025 14:20:30 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D0=B2=D0=B8=D0=B3:=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D1=8C=D1=82=D0=B5=20=D0=BE=D1=81=D0=BE?= =?UTF-8?q?=D0=B1=D0=B5=D0=BD=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20=D1=87=D0=B5?= =?UTF-8?q?=D0=BA=20=D0=B7=D0=B4=D0=BE=D1=80=D0=BE=D0=B2=D1=8C=D1=8F=20?= =?UTF-8?q?=D0=B8=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82=D0=B5=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=20=D0=B4=D0=BE=D0=BA=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F,=20=D1=81=D1=82=D1=80=D0=BE=D1=8F=D1=82?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5=D1=81=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- puppy/Dockerfile | 18 +++++++++++------- puppy/app/healthcheck.cpp | 6 ++++++ puppy/app/main.cpp | 21 ++++++++++++--------- 3 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 puppy/app/healthcheck.cpp diff --git a/puppy/Dockerfile b/puppy/Dockerfile index dab8e62..c539092 100644 --- a/puppy/Dockerfile +++ b/puppy/Dockerfile @@ -1,11 +1,13 @@ #check=skip=JSONArgsRecommended FROM debian:12-slim AS builder 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/* WORKDIR /build COPY . . +RUN g++ -o healthcheck -I./include ./app/healthcheck.cpp + RUN --mount=type=cache,target=cmake-build cmake -B cmake-build \ -DCMAKE_CXX_COMPILER=i686-w64-mingw32-g++ && cmake --build cmake-build -j$(nproc) && \ 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 ENV WINEPREFIX=/wineprefix \ DEBIAN_FRONTEND=noninteracive -RUN sed -i 's/main/main contrib/' /etc/apt/sources.list.d/debian.sources && \ - dpkg --add-architecture i386 && apt-get update && \ +RUN dpkg --add-architecture i386 && apt-get update && \ 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 && \ - apt-get clean && rm -rf /var/lib/apt/lists/* + mkdir /tmpfs && ln -sf /tmpfs $WINEPREFIX/drive_c/tmpfs + COPY build/ $WINEPREFIX/drive_c RUN wine regedit $WINEPREFIX/drive_c/registry.reg && \ - mkdir /tmpfs && ln -sf /tmpfs $WINEPREFIX/drive_c/tmpfs && \ echo "Sleeping for 15 seconds" && sleep 15 WORKDIR /app -COPY --from=builder /build/promt-puppy.exe . +COPY --from=builder /build/promt-puppy.exe /build/healthcheck . + +HEALTHCHECK CMD ./healthcheck EXPOSE 80/tcp VOLUME /cache diff --git a/puppy/app/healthcheck.cpp b/puppy/app/healthcheck.cpp new file mode 100644 index 0000000..f5e023f --- /dev/null +++ b/puppy/app/healthcheck.cpp @@ -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); +} diff --git a/puppy/app/main.cpp b/puppy/app/main.cpp index b1652f0..1acb218 100644 --- a/puppy/app/main.cpp +++ b/puppy/app/main.cpp @@ -4,16 +4,10 @@ #include #include #include -#include #include "PromtCtlDocument.hpp" #include "PromtFTManager.hpp" -template -static inline void print(T... args) { - ((std::cout << args << ' '), ...) << std::endl; -} - static inline std::string random_filename(int len = 65) { static const char ASCII_PRINTABLE[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; std::random_device random_device; @@ -26,10 +20,15 @@ static inline std::string random_filename(int len = 65) { 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_OUT = TMP_FOLDER + random_filename(); +template +static inline void print(T... args) { + ((std::cout << args << ' '), ...) << std::endl; +} + static inline auto utf8_to_cp(const char *data) { int size = MultiByteToWideChar(CP_UTF8, 0, data, -1, 0, 0); auto wstr = new wchar_t[size]; @@ -86,7 +85,7 @@ class WebServer { ofs << utf8_to_cp(text.c_str()); } 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) { @@ -146,9 +145,13 @@ class WebServer { if (!std::filesystem::exists(TMP_FOLDER)) std::filesystem::create_directory(TMP_FOLDER); m_svr.Post("/translate", [this](const httplib::Request &req, httplib::Response &res) { - print("Got request!"); TranslateHandler(req, res); }); + + m_svr.Get("/health", [](const httplib::Request &req, httplib::Response &res) { + res.set_content("OK", "text/plain"); + res.status = 200; + }); } ~WebServer() {