C++ 如何保持服务器运行(cpprestsdk-卡萨布兰卡)
我正在使用微软的cpprestsdk(又名卡萨布兰卡)开发一个RESTAPI,在执行代码时,我很难保持服务器运行 请参见此处的my main.cpp:C++ 如何保持服务器运行(cpprestsdk-卡萨布兰卡),c++,casablanca,cpprest-sdk,C++,Casablanca,Cpprest Sdk,我正在使用微软的cpprestsdk(又名卡萨布兰卡)开发一个RESTAPI,在执行代码时,我很难保持服务器运行 请参见此处的my main.cpp: int main() { cout << "Starting Server" << endl; TransactionController server; server.setEndpoint("http://0.0.0.0:4200/api"); server.initHandlers(
int main() {
cout << "Starting Server" << endl;
TransactionController server;
server.setEndpoint("http://0.0.0.0:4200/api");
server.initHandlers();
try {
server.openServer();
cout << "Server listening at: " << server.getEndpoint() << endl;
// figure out how to keep server running without this?
while (true);
}
catch(exception &e) {
cout << "--- ERROR DETECTED ---" << endl;
cout << e.what() << endl;
}
// this doesn't get reached bc of the while(true)
server.closeServer();
return 0;
}
在手动停止执行之前保持服务器运行
不过,我希望以更优雅的方式实现此功能。我在网上浏览了文档,但没有找到正确的方法
任何正确方向的建议都将不胜感激,因为我以前从未与卡萨布兰卡合作过。谢谢你抽出时间 所以我使用这里提供的代码设法解决了这个问题: 下面是我的新main.cpp:
pplx::task<void> TransactionController::openServer() {
return listener.open();
}
pplx::task<void> TransactionController::closeServer() {
return listener.close();
}
std::string TransactionController::getEndpoint() const{
return listener.uri().to_string();
}
void TransactionController::initHandlers() {
listener.support(methods::GET, bind(&TransactionController::handleGet, this, std::placeholders::_1));
listener.support(methods::POST, bind(&TransactionController::handlePost, this, placeholders::_1));
}
void TransactionController::setEndpoint(const string& value) {
listener = http_listener(value);
}
int main() {
cout << "Starting Server" << endl;
InterruptHandler::hookSIGINT();
TransactionController server;
server.setEndpoint("http://0.0.0.0:4200/api");
server.initHandlers();
try {
server.openServer().wait();
cout << "Server listening at: " << server.getEndpoint() << endl;
InterruptHandler::waitForUserInterrupt();
server.closeServer().wait();
cout << "Shutting Down Server" << endl;
}
catch(exception &e) {
cout << "--- ERROR DETECTED ---" << endl;
cout << e.what() << endl;
}
return 0;
}
intmain(){
cout所以我使用这里提供的代码设法解决了这个问题:
下面是我的新main.cpp:
pplx::task<void> TransactionController::openServer() {
return listener.open();
}
pplx::task<void> TransactionController::closeServer() {
return listener.close();
}
std::string TransactionController::getEndpoint() const{
return listener.uri().to_string();
}
void TransactionController::initHandlers() {
listener.support(methods::GET, bind(&TransactionController::handleGet, this, std::placeholders::_1));
listener.support(methods::POST, bind(&TransactionController::handlePost, this, placeholders::_1));
}
void TransactionController::setEndpoint(const string& value) {
listener = http_listener(value);
}
int main() {
cout << "Starting Server" << endl;
InterruptHandler::hookSIGINT();
TransactionController server;
server.setEndpoint("http://0.0.0.0:4200/api");
server.initHandlers();
try {
server.openServer().wait();
cout << "Server listening at: " << server.getEndpoint() << endl;
InterruptHandler::waitForUserInterrupt();
server.closeServer().wait();
cout << "Shutting Down Server" << endl;
}
catch(exception &e) {
cout << "--- ERROR DETECTED ---" << endl;
cout << e.what() << endl;
}
return 0;
}
intmain(){
cout我这样做的方式是在do while循环中使用std::getline,该循环将仅通过正确的命令终止服务器。我发现这是一个快速修复方法,不需要太多资源
http_listener listener(L"http://localhost:80");
try {
listener
.open()
.then([&listener]() {std::wcout << L"\nstarting to listen\n"; })
.wait();
} catch(exception const& e){
std::wcout << e.what() << std::endl;
}
std::string choice= "";
do {
std::getline(cin, choice);
} while (!(choice == "N" || choice == "n"));
return 0;
http_侦听器(L)http://localhost:80");
试一试{
听众
.open()
。然后([&listener](){std::wcout我这样做的方式是在do-while循环中使用std::getline,该循环将仅通过正确的命令终止服务器。我发现这是一个快速修复方法,不需要太多资源
http_listener listener(L"http://localhost:80");
try {
listener
.open()
.then([&listener]() {std::wcout << L"\nstarting to listen\n"; })
.wait();
} catch(exception const& e){
std::wcout << e.what() << std::endl;
}
std::string choice= "";
do {
std::getline(cin, choice);
} while (!(choice == "N" || choice == "n"));
return 0;
http_侦听器(L)http://localhost:80");
试一试{
听众
.open()
。然后([&listener](){std::wcout如果您的http_侦听器是指针,则可以避免无限循环。创建http_侦听器的对象并等待,您应该能够删除无限循环,然后程序继续
下面的代码可以工作
void initialize(){
http_监听器*监听器;
侦听器=新的http_侦听器(L)http://127.0.0.1:1444/vessel");
侦听器->支持(方法::GET、GET\u容器\u访问位置);
尝试
{
侦听器->
开()
。然后([&listener](){})
.wait();
//虽然(正确);
}
捕获(异常常量和e)
{
wcout如果您的http_侦听器是指针,则可以避免无限循环。创建http_侦听器的对象并等待,您应该能够删除无限循环,然后程序继续
下面的代码可以工作
void initialize(){
http_监听器*监听器;
侦听器=新的http_侦听器(L)http://127.0.0.1:1444/vessel");
侦听器->支持(方法::GET、GET\u容器\u访问位置);
尝试
{
侦听器->
开()
。然后([&listener](){})
.wait();
//虽然(正确);
}
捕获(异常常量和e)
{
WCUT使用<代码> STD::信号< /代码>,在接受的ANWSER中用信号处理程序表示,这里不是真正的选择,因为根据C++引用:
信号处理程序应具有C链接,并且通常只有
使用C和C++的公共子集的特征。
如果C++链接的函数可以用作
信号处理器
然而,notify{one,all}
不是信号安全的,因此不能在信号处理程序中使用
或者,由于cpprestsdk依赖于boost asio,因此可以利用io_上下文进行信号处理。下面的代码片段演示了这一概念:
void handle_get(web::http::http_request request) {
//...
}
void interrupt_handler( const boost::system::error_code& error , int signal_number ) {
//...
}
int main() {
using web::http::experimental::listener::http_listener;
auto url = web::http::uri("http://127.0.0.1:8051");
http_listener listener(url);
listener.support(web::http::methods::GET, handle_get);
listener.open().then([]() { std::cout << "listening ..." << std::endl; }).wait();
boost::asio::io_context handler_context;
boost::asio::signal_set signals(handler_context, SIGINT );
signals.async_wait( interrupt_handler );
handler_context.run();
return 0;
}
void handle\u get(web::http::http\u请求){
//...
}
无效中断处理程序(常量boost::系统::错误代码和错误,整数信号编号){
//...
}
int main(){
使用web::http::experimental::listener::http\U listener;
自动url=web::http::uri(“http://127.0.0.1:8051");
http_侦听器(url);
support(web::http::methods::GET,handle\u GET);
然后,([])[{](ST{::CUT使用<代码> STD::信号< /C> >在接受的ANWSER中用信号处理程序表示,这里不是真正的选择,因为根据C++引用:
信号处理程序应具有C链接,并且通常只有
使用C和C++的公共子集的特征。
如果C++链接的函数可以用作
信号处理器
然而,notify{one,all}
不是信号安全的,因此不能在信号处理程序中使用
或者,由于cpprestsdk依赖于boost asio,因此可以利用io_上下文进行信号处理。下面的代码片段演示了这一概念:
void handle_get(web::http::http_request request) {
//...
}
void interrupt_handler( const boost::system::error_code& error , int signal_number ) {
//...
}
int main() {
using web::http::experimental::listener::http_listener;
auto url = web::http::uri("http://127.0.0.1:8051");
http_listener listener(url);
listener.support(web::http::methods::GET, handle_get);
listener.open().then([]() { std::cout << "listening ..." << std::endl; }).wait();
boost::asio::io_context handler_context;
boost::asio::signal_set signals(handler_context, SIGINT );
signals.async_wait( interrupt_handler );
handler_context.run();
return 0;
}
void handle\u get(web::http::http\u请求){
//...
}
无效中断处理程序(常量boost::系统::错误代码和错误,整数信号编号){
//...
}
int main(){
使用web::http::experimental::listener::http\U listener;
自动url=web::http::uri(“http://127.0.0.1:8051");
http_侦听器(url);
support(web::http::methods::GET,handle\u GET);
listener.open()。然后([](){std::cout这是不理想的,因为它不可避免地使资源泄漏。这不是一个可扩展的解决方案,也不是一个最佳实践。这是不理想的,因为它不可避免地使资源泄漏。不是一个可扩展的解决方案,也不是一个最佳实践。请注意,信号处理程序应该具有C链接,并且通常只使用来自公共子集的功能C和C++的T。如果C++链接的函数可以用作信号处理程序,则定义为实现。注意信号处理程序期望具有C连接,并且通常只使用C和C++的公共子集的特征。如果C++链接的函数可以用作信号处理程序,则是实现。