restinio
v.0.7.3
Restinio是一个C ++ 17库,可为您提供嵌入式HTTP/WebSocket服务器。它基于ASIO的独立版本,主要针对HTTP-要求的异步处理。 BOOST :: ASIO也得到了支持。
Restinio本身是一个仅标题库,但它取决于不仅标题的Nodejs/llhttp。
目前, Restinio处于相当稳定的状态,我们认为自V.0.4.0以来,它已准备好生产使用。
考虑编写必须支持某些REST API的C ++应用程序的任务,Restinio代表我们解决该任务的解决方案。
让我们看看最简单的情况下的感觉:
# include < restinio/core.hpp >
int main ()
{
restinio::run (
restinio::on_this_thread ()
. port ( 8080 )
. address ( " localhost " )
. request_handler ([]( auto req) {
return req-> create_response (). set_body ( " Hello, World! " ). done ();
}));
return 0 ;
}服务器在主线程上运行,并通过Hello-World消息响应所有请求。当然,您可以访问给定的HTTP请求的结构,因此您可以应用复杂的逻辑来处理请求。
# include < restinio/core.hpp >
using namespace restinio ;
template < typename T>
std::ostream & operator <<(std::ostream & to, const optional_t <T> & v) {
if (v) to << *v;
return to;
}
int main () {
// Create express router for our service.
auto router = std::make_unique<router:: express_router_t <>>();
router-> http_get (
R"( /data/meter/:meter_id(d+) )" ,
[]( auto req, auto params) {
const auto qp = parse_query (req-> header (). query ());
return req-> create_response ()
. set_body (
fmt::format ( " meter_id={} (year={}/mon={}/day={}) " ,
cast_to< int >(params[ " meter_id " ]),
opt_value< int >(qp, " year " ),
opt_value< int >(qp, " mon " ),
opt_value< int >(qp, " day " )))
. done ();
});
router-> non_matched_request_handler (
[]( auto req){
return req-> create_response ( restinio::status_not_found ()). connection_close (). done ();
});
// Launching a server with custom traits.
struct my_server_traits : public default_single_thread_traits_t {
using request_handler_t = restinio::router:: express_router_t <>;
};
restinio::run (
restinio::on_this_thread<my_server_traits>()
. address ( " localhost " )
. request_handler ( std::move (router)));
return 0 ;
}Restinio是根据BSD-3-CAREASE许可证分配的。
可以在此处找到Restinio的完整文档。