Skip to content

Commit a681a22

Browse files
committed
Updated corsFilter files again.
1 parent a7888af commit a681a22

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

api/include/filters/CorsFilter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// CorsFilter.h
12
#ifndef QUANT_FIN_API_CORS_FILTER_H
23
#define QUANT_FIN_API_CORS_FILTER_H
34
#include <drogon/HttpFilter.h>

api/src/filters/CorsFilter.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// CorsFilter.cpp
12
#include "filters/CorsFilter.h"
23
#include "utils/Logger.h"
34

@@ -7,15 +8,15 @@ namespace api {
78
void CorsFilter::doFilter(const drogon::HttpRequestPtr& req,
89
drogon::FilterCallback&& fcb,
910
drogon::FilterChainCallback&& fccb) {
10-
1111
API_LOG_DEBUG("Processing CORS request for path: {}", req->getPath());
1212

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

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

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

29-
// For non-OPTIONS requests, continue with the chain but with CORS headers
30+
// For non-OPTIONS requests, add CORS headers to the response
31+
req->addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
32+
req->addHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
33+
req->addHeader("Access-Control-Allow-Headers", "Content-Type,Authorization");
34+
req->addHeader("Access-Control-Max-Age", "86400");
35+
36+
// Continue with the request chain
3037
fccb();
3138
}
3239

api/src/main.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,12 @@ int main() {
1818
// Configure server
1919
auto& app = drogon::app();
2020

21-
// Load config and setup
22-
app.loadConfigFile("config.json");
23-
app.addListener("0.0.0.0", 8080);
21+
// Load config from the api directory
22+
app.loadConfigFile("api/config.json");
2423

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

3328
API_LOG_INFO("Server configuration loaded successfully");
3429
API_LOG_INFO("Starting server on port 8080");

logs/api_2024-12-14.log

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[2024-12-14 20:28:37.636] [API] [info] [10111] Starting Quantitative Finance API server
2+
[2024-12-14 20:28:37.637] [API] [critical] [10111] Fatal error during server startup: Config file config.json not found!
3+
[2024-12-14 20:32:06.597] [API] [info] [10367] Starting Quantitative Finance API server
4+
[2024-12-14 20:32:06.597] [API] [info] [10367] Server configuration loaded successfully
5+
[2024-12-14 20:32:06.597] [API] [info] [10367] Starting server on port 8080

0 commit comments

Comments
 (0)