chore: remove .clangd config, update CMakeLists, Dockerfile, and code

This commit is contained in:
Arthur K. 2025-01-19 14:20:26 +03:00
parent 87a758ecd8
commit d735a356b3
Signed by: wzray
GPG key ID: B97F30FDC4636357
15 changed files with 76 additions and 58 deletions

View file

@ -2,22 +2,26 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <mutex>
#include <string>
#include <windows.h>
#include <wingdi.h>
#include <random>
#include "PromtCtlDocument.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 const char ASCII_PRINTABLE[] =
"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static const char ASCII_PRINTABLE[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
std::random_device random_device;
std::mt19937 generator(random_device());
std::uniform_int_distribution distribution(0, static_cast<int>(strlen(ASCII_PRINTABLE) - 1));
std::string random_string;
for (std::size_t i = 0; i < len; ++i)
random_string.reserve(len);
for (int i = 0; i < len; ++i)
random_string += ASCII_PRINTABLE[distribution(generator)];
return random_string;
}
@ -26,6 +30,19 @@ static const char TMP_FOLDER[] = "C:\\tmpfs\\";
static const auto TMP_IN = TMP_FOLDER + random_filename();
static const auto TMP_OUT = TMP_FOLDER + random_filename();
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];
MultiByteToWideChar(CP_UTF8, 0, data, -1, wstr, size);
size = WideCharToMultiByte(1251, 0, wstr, -1, NULL, 0, 0, 0);
std::string out;
out.resize(size);
WideCharToMultiByte(1251, 0, wstr, -1, out.data(), size, 0, 0);
delete[] wstr;
return out;
}
static inline auto read_file(const std::string &filename) {
char *in_buf = nullptr;
{
@ -60,17 +77,16 @@ class WebServer {
std::mutex m_global_lock;
private:
int TranslateHtml(const std::string &text, httplib::Response &res) {
void TranslateHtml(const std::string &text, httplib::Response &res) {
const auto ft = PromtFTManager::FileType::kHTML;
auto direction = m_doc.direction();
auto translator = m_ftman.Translator(ft, direction);
{
std::ofstream ofs(TMP_IN);
ofs << text;
ofs << utf8_to_cp(text.c_str());
}
translator.Translate(TMP_IN, TMP_OUT);
res.set_content(read_file(TMP_OUT), "text/html");
}
void TranslateText(const std::string &text, httplib::Response &res) {
@ -87,8 +103,8 @@ class WebServer {
else if (target_dir == "ru-en")
m_doc.direction(PromtCtlDocument::Direction::kRusEng);
else {
res.set_content("X-Translation-Direction must be one of: [\"en-ru\", \"ru-en\"]",
"text/plain");
print("Wrong X-Translation-Direction header:", target_dir);
res.set_content("X-Translation-Direction must be one of: [\"en-ru\", \"ru-en\"]", "text/plain");
res.status = 400;
return false;
}
@ -101,11 +117,11 @@ class WebServer {
void TranslateHandler(const httplib::Request &req, httplib::Response &res) {
const std::lock_guard lock(m_global_lock);
if (!SetDirection(req.headers, res))
return;
if (!SetDirection(req.headers, res)) return;
const auto &body = req.body;
if (body.empty()) {
print("Request with empty body");
res.set_content("Request body must not be empty", "text/plain");
res.status = 400;
return;
@ -128,7 +144,9 @@ class WebServer {
public:
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);
});
}
@ -137,8 +155,8 @@ class WebServer {
stop();
}
void listen() {
m_svr.listen("0.0.0.0", 80);
void listen(const char *host = "0.0.0.0", unsigned short port = 80) {
m_svr.listen(host, port);
}
void stop() {
@ -149,9 +167,10 @@ class WebServer {
};
int main() {
print("Starting...");
CoInitializeEx(NULL, COINIT_MULTITHREADED);
CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_NONE,
RPC_C_IMP_LEVEL_IDENTIFY, nullptr, EOAC_NONE, nullptr);
CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, nullptr, EOAC_NONE, nullptr);
WebServer ws;
ws.listen();