Skip to content

keep the server running without using std::getline or boost::asio #1841

@yyjpro

Description

@yyjpro

How can I keep the server running without using std::getline or boost::asio? Does cpprest provide any interfaces for this?

using boost::asio can achieve this, but does cpprest provide any other useful method? If so, the advantages compared to using boost::asio directly?

Here is my code:

int main(int argc, char* argv[]) {
    utility::string_t port = U("8080");
    utility::string_t address = U("http://localhost:");
    address.append(port);

    // init server
    uri_builder uri(address);
    uri.append_path(U("/api/predict/")).append(U("model"));
    auto addr = uri.to_uri().to_string();

    http_listener m_listener(addr);
    m_listener.support(methods::GET, std::bind(&handle_get, std::placeholders::_1));
    m_listener.support(methods::POST, std::bind(&handle_post, std::placeholders::_1));
    m_listener.open().wait();

    std::cout << utility::string_t(U("Listening for requests at: ")) << addr << std::endl;

    boost::asio::io_context handler_context;
    boost::asio::signal_set signals(handler_context, SIGINT );

    signals.async_wait( [&](boost::system::error_code, int signum) {
        handler_context.stop();
    });
    handler_context.run();

//        std::cout << "Press ENTER to exit." << std::endl;
//        std::string line;
//        std::getline(std::cin, line);

    m_listener.close().wait();

    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions