restinio
v.0.7.3
Restinio는 C ++ 17 라이브러리로 임베디드 HTTP/WebSocket 서버를 제공합니다. 그것은 독립형 버전의 ASIO를 기반으로하며 주로 HTTP 요청의 비동기 처리를 대상으로합니다. 부스트 :: ASIO도 지원됩니다.
Restinio 자체는 헤더 전용 라이브러리이지만 헤더 전용이 아닌 Nodejs/LLHTTP에 따라 다릅니다.
현재 Restinio는 다소 안정적인 상태이며 V.0.4.0 이후 생산 사용이 준비되어 있다고 생각합니다.
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-Clause 라이센스에 따라 배포됩니다.
Restinio에 대한 전체 문서는 여기에서 찾을 수 있습니다.