Skip to content

Commit

Permalink
Updated corsFilter files again.
Browse files Browse the repository at this point in the history
  • Loading branch information
tembolo1284 committed Dec 15, 2024
1 parent a7888af commit a681a22
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions api/include/filters/CorsFilter.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// CorsFilter.h
#ifndef QUANT_FIN_API_CORS_FILTER_H
#define QUANT_FIN_API_CORS_FILTER_H
#include <drogon/HttpFilter.h>
Expand Down
19 changes: 13 additions & 6 deletions api/src/filters/CorsFilter.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// CorsFilter.cpp
#include "filters/CorsFilter.h"
#include "utils/Logger.h"

Expand All @@ -7,15 +8,15 @@ namespace api {
void CorsFilter::doFilter(const drogon::HttpRequestPtr& req,
drogon::FilterCallback&& fcb,
drogon::FilterChainCallback&& fccb) {

API_LOG_DEBUG("Processing CORS request for path: {}", req->getPath());

// Create response for handling CORS
auto resp = drogon::HttpResponse::newHttpResponse();

// Add CORS headers
resp->addHeader("Access-Control-Allow-Origin", "*");
resp->addHeader("Access-Control-Allow-Methods", "OPTIONS,GET,POST,HEAD");
resp->addHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
// Add CORS headers matching your config
resp->addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
resp->addHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
resp->addHeader("Access-Control-Allow-Headers", "Content-Type,Authorization");
resp->addHeader("Access-Control-Max-Age", "86400");

// Handle preflight request
Expand All @@ -26,7 +27,13 @@ void CorsFilter::doFilter(const drogon::HttpRequestPtr& req,
return;
}

// For non-OPTIONS requests, continue with the chain but with CORS headers
// For non-OPTIONS requests, add CORS headers to the response
req->addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
req->addHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
req->addHeader("Access-Control-Allow-Headers", "Content-Type,Authorization");
req->addHeader("Access-Control-Max-Age", "86400");

// Continue with the request chain
fccb();
}

Expand Down
15 changes: 5 additions & 10 deletions api/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ int main() {
// Configure server
auto& app = drogon::app();

// Load config and setup
app.loadConfigFile("config.json");
app.addListener("0.0.0.0", 8080);
// Load config from the api directory
app.loadConfigFile("api/config.json");

// Register CORS filter for all paths using path patterns
drogon::app().registerPreHandlingAdvice(
[](const drogon::HttpRequestPtr& req, drogon::AdviceCallback&& acb, drogon::AdviceChainCallback&& accb) {
auto corsFilter = std::make_shared<quant_fin::api::CorsFilter>();
corsFilter->doFilter(req, std::move(acb), std::move(accb));
}
);
// Create the CORS filter
auto corsFilter = std::make_shared<quant_fin::api::CorsFilter>();
app.registerFilter(corsFilter);

API_LOG_INFO("Server configuration loaded successfully");
API_LOG_INFO("Starting server on port 8080");
Expand Down
5 changes: 5 additions & 0 deletions logs/api_2024-12-14.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[2024-12-14 20:28:37.636] [API] [info] [10111] Starting Quantitative Finance API server
[2024-12-14 20:28:37.637] [API] [critical] [10111] Fatal error during server startup: Config file config.json not found!
[2024-12-14 20:32:06.597] [API] [info] [10367] Starting Quantitative Finance API server
[2024-12-14 20:32:06.597] [API] [info] [10367] Server configuration loaded successfully
[2024-12-14 20:32:06.597] [API] [info] [10367] Starting server on port 8080

0 comments on commit a681a22

Please sign in to comment.