From 360b9351da957bdb65eb9c964d54e25d7ee7e3cc Mon Sep 17 00:00:00 2001 From: chatlanin Date: Sun, 4 Aug 2024 12:40:09 +0300 Subject: [PATCH] fix api error --- src/trs/trs.hpp | 15 ++++++++++----- src/trs/utils/var.hpp | 1 - tests/main.cpp | 3 +-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/trs/trs.hpp b/src/trs/trs.hpp index eadef58..fce049f 100644 --- a/src/trs/trs.hpp +++ b/src/trs/trs.hpp @@ -29,7 +29,7 @@ namespace trs ~server() = default; public: - void init(std::string service_name) + void init(std::string service_name, std::string url = "/") { if (!PGXX().ready()) { @@ -38,13 +38,15 @@ namespace trs return; } + m_service_name = service_name; + API_URL = url; + set_read_timeout(5, 0); set_write_timeout(5, 0); set_idle_interval(0, 1'000'000); set_payload_max_length(1024 * 1024 * 512); // 512MB set_CORS(); set_post(); - m_service_name = service_name; } void log_to_console(bool v) { m_log_to_console = v; } @@ -78,28 +80,31 @@ namespace trs bool m_log_to_console { true }; function_manager m_function_manager; inspector m_inspector; + std::string API_URL { "/" }; + private: void set_CORS() { Options(R"(\*)", [](const auto& req, auto& res) { res.set_header("Allow", "POST, HEAD, OPTIONS"); }); - Options(var::API_URL, [](const auto& req, auto& res) + Options(API_URL, [](const auto& req, auto& res) { res.set_header("Access-Control-Allow-Origin", "*"); res.set_header("Allow", "POST, HEAD, OPTIONS"); - res.set_header( "Access-Control-Allow-Headers", std::format("X-Requested-With, Content-Type, Accept, Origin, Authorization, {}, {}", var::HEADER_TOKEN, var::HEADER_FUNCTION).c_str()); + res.set_header("Access-Control-Allow-Headers", std::format("X-Requested-With, Content-Type, Accept, Origin, Authorization, {}, {}", var::HEADER_TOKEN, var::HEADER_FUNCTION).c_str()); res.set_header("Access-Control-Allow-Methods", "OPTIONS, HEAD, POST"); }); } void set_post() { - Post(var::API_URL, [&](const httplib::Request& req, httplib::Response& res) { + Post(API_URL, [&](const httplib::Request& req, httplib::Response& res) { res.set_header("Access-Control-Allow-Origin", "*"); res.set_header("Access-Control-Allow-Headers", "*"); res.set_header("Access-Control-Allow-Methods", "POST"); res.set_header("Access-Control-Allow-Credentials", "false"); + hack::log()(API_URL); transaction tr; try diff --git a/src/trs/utils/var.hpp b/src/trs/utils/var.hpp index d01c16a..0dbe388 100644 --- a/src/trs/utils/var.hpp +++ b/src/trs/utils/var.hpp @@ -6,7 +6,6 @@ namespace trs::var { inline const int PORT = 5000; inline const std::string HOST = "0.0.0.0"; - inline const std::string API_URL = "/"; inline const std::string ALIEN_SYSTEM_ERROR = "is alien system error it can be very dangerous!!! good luck my friend!"; inline const std::string NO_VALID_DATA = "no valid data"; diff --git a/tests/main.cpp b/tests/main.cpp index 619e91e..7ff5a76 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -52,8 +52,7 @@ auto main(int argc, char* args[]) -> int // trs::client::upload("https://file-examples.com/storage/feaf4c715066a139295358c/2017/11/file_example_MP3_700KB.mp3", "/mnt/raid/projects/trs/test.mp3"); trs::server srv; - srv.init("test service"); - srv.set_api_url("/api"); + srv.init("test service", "/api"); srv.registration("healthcheck", worckspaces::inspector, worckspaces::provider); srv.run();