fix api error

This commit is contained in:
chatlanin 2024-08-04 12:40:09 +03:00
parent 19b43a3bba
commit 360b9351da
3 changed files with 11 additions and 8 deletions

View File

@ -29,7 +29,7 @@ namespace trs
~server() = default; ~server() = default;
public: public:
void init(std::string service_name) void init(std::string service_name, std::string url = "/")
{ {
if (!PGXX().ready()) if (!PGXX().ready())
{ {
@ -38,13 +38,15 @@ namespace trs
return; return;
} }
m_service_name = service_name;
API_URL = url;
set_read_timeout(5, 0); set_read_timeout(5, 0);
set_write_timeout(5, 0); set_write_timeout(5, 0);
set_idle_interval(0, 1'000'000); set_idle_interval(0, 1'000'000);
set_payload_max_length(1024 * 1024 * 512); // 512MB set_payload_max_length(1024 * 1024 * 512); // 512MB
set_CORS(); set_CORS();
set_post(); set_post();
m_service_name = service_name;
} }
void log_to_console(bool v) { m_log_to_console = v; } void log_to_console(bool v) { m_log_to_console = v; }
@ -78,28 +80,31 @@ namespace trs
bool m_log_to_console { true }; bool m_log_to_console { true };
function_manager<transaction> m_function_manager; function_manager<transaction> m_function_manager;
inspector<transaction> m_inspector; inspector<transaction> m_inspector;
std::string API_URL { "/" };
private: private:
void set_CORS() void set_CORS()
{ {
Options(R"(\*)", [](const auto& req, auto& res) { res.set_header("Allow", "POST, HEAD, OPTIONS"); }); 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("Access-Control-Allow-Origin", "*");
res.set_header("Allow", "POST, HEAD, OPTIONS"); 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"); res.set_header("Access-Control-Allow-Methods", "OPTIONS, HEAD, POST");
}); });
} }
void set_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-Origin", "*");
res.set_header("Access-Control-Allow-Headers", "*"); res.set_header("Access-Control-Allow-Headers", "*");
res.set_header("Access-Control-Allow-Methods", "POST"); res.set_header("Access-Control-Allow-Methods", "POST");
res.set_header("Access-Control-Allow-Credentials", "false"); res.set_header("Access-Control-Allow-Credentials", "false");
hack::log()(API_URL);
transaction tr; transaction tr;
try try

View File

@ -6,7 +6,6 @@ namespace trs::var
{ {
inline const int PORT = 5000; inline const int PORT = 5000;
inline const std::string HOST = "0.0.0.0"; 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 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"; inline const std::string NO_VALID_DATA = "no valid data";

View File

@ -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::client::upload("https://file-examples.com/storage/feaf4c715066a139295358c/2017/11/file_example_MP3_700KB.mp3", "/mnt/raid/projects/trs/test.mp3");
trs::server srv; trs::server srv;
srv.init("test service"); srv.init("test service", "/api");
srv.set_api_url("/api");
srv.registration("healthcheck", worckspaces::inspector, worckspaces::provider); srv.registration("healthcheck", worckspaces::inspector, worckspaces::provider);
srv.run(); srv.run();