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;
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,12 +80,14 @@ namespace trs
bool m_log_to_console { true };
function_manager<transaction> m_function_manager;
inspector<transaction> 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");
@ -94,12 +98,13 @@ namespace trs
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

View File

@ -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";

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::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();