这里记录在官方的例子中,加入跨域。Oat++ Example-CRUD
在官方的例子中,加入跨域。
Oat++ Example-CRUD
修改AppComponent.hpp文件中的代码,如下:
#include "AppComponent.hpp"
#include "controller/UserController.hpp"
#include "controller/StaticController.hpp"
#include "oatpp-swagger/Controller.hpp"
#include "oatpp/network/Server.hpp"
#include "oatpp/web/server/interceptor/RequestInterceptor.hpp"
#include "oatpp/web/server/interceptor/ResponseInterceptor.hpp"
#include "oatpp/web/protocol/http/outgoing/ResponseFactory.hpp"
#include <iostream>
class CORSInterceptor : public oatpp::web::server::interceptor::RequestInterceptor, public oatpp::web::server::interceptor::ResponseInterceptor {
public:
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> intercept(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request) override {
if (request->getStartingLine().method == "OPTIONS") {
auto response = oatpp::web::protocol::http::outgoing::ResponseFactory::createResponse(oatpp::web::protocol::http::Status::CODE_200, nullptr);
/* response->putHeader("Access-Control-Allow-Origin", "*");*/
/* response->putHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
response->putHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");*/
//response->putHeader("Access-Control-Max-Age", "3600"); // Cache preflight response
return response;
}
return nullptr;
}
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> intercept(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request, const std::shared_ptr<oatpp::web::protocol::http::outgoing::Response>& response) override {
response->putHeader("Access-Control-Allow-Origin", "*");
response->putHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
response->putHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
return response;
}
};
void run() {
AppComponent components; // Create scope Environment components
/* Get router component */
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
/* Get connection handler component */
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);
/* Create a CORSInterceptor instance */
auto corsInterceptor = std::make_shared<CORSInterceptor>();
/* Add CORSInterceptor to the connection handler */
auto httpConnectionHandler = std::static_pointer_cast<oatpp::web::server::HttpConnectionHandler>(connectionHandler);
httpConnectionHandler->addRequestInterceptor(corsInterceptor);
httpConnectionHandler->addResponseInterceptor(corsInterceptor);
oatpp::web::server::api::Endpoints docEndpoints;
docEndpoints.append(router->addController(UserController::createShared())->getEndpoints());
router->addController(oatpp::swagger::Controller::createShared(docEndpoints));
router->addController(StaticController::createShared());
/* Get connection provider component */
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
/* create server */
oatpp::network::Server server(connectionProvider, connectionHandler);
OATPP_LOGD("Server", "Running on port %s...", connectionProvider->getProperty("port").toString()->c_str());
server.run();
/* stop db connection pool */
OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider);
dbConnectionProvider->stop();
}
/**
* main
*/
int main(int argc, const char* argv[]) {
oatpp::base::Environment::init();
run();
/* Print how much objects were created during app running, and what have left-probably leaked */
/* Disable object counting for release builds using '-D OATPP_DISABLE_ENV_OBJECT_COUNTERS' flag for better performance */
std::cout << "\nEnvironment:\n";
std::cout << "objectsCount = " << oatpp::base::Environment::getObjectsCount() << "\n";
std::cout << "objectsCreated = " << oatpp::base::Environment::getObjectsCreated() << "\n\n";
oatpp::base::Environment::destroy();
return 0;
}