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的完整文檔。