지돌이의 블로그 입니다!

C++ & libuv & uvw (C++ libuv wrapper library) 으로 http server을 구현했습니다.

 

Spring-boot의 RequestMapping과 같은 기능을 구현해놓아서 URL에 대한 접근이 용이합니다.

 

#include <iostream>

#include <uvw/loop.hpp>
#include <jshttpserver/server.hpp>
#include <jshttpserver/http_request.hpp>

#pragma comment(lib, "libuv.lib")

int main() {
    auto loop = uvw::Loop::getDefault();

    std::shared_ptr<jshttpserver::Server> server(jshttpserver::Server::create(loop));

    server->requestMapping(jshttpserver::METHOD_ALL, "/api/test", [](jshttpserver::HttpRequest &req, jshttpserver::HttpResponse &res) {
      std::cout << "requestMapping FOUND :: /api/test" << std::endl;
      std::cout << "\tsearch[ " << req.url_search << " ]" << std::endl;
    });

    server->requestMapping(jshttpserver::METHOD_ALL, "/api/test2/{v1}/{v2}", [](jshttpserver::HttpRequest &req, jshttpserver::HttpResponse &res) {
      std::cout << "requestMapping FOUND :: /api/test2/{v1}/{v2}" << std::endl;
      std::cout << "\tsearch[ " << req.url_search << " ]" << std::endl;
    });

    server->requestMapping(jshttpserver::METHOD_ALL, "/api/test3/{v3:.+}", [](jshttpserver::HttpRequest &req, jshttpserver::HttpResponse &res) {
      std::cout << "requestMapping FOUND :: /api/test3/{v3:.+}" << std::endl;
      std::cout << "\tsearch[ " << req.url_search << " ]" << std::endl;
    });

    server->documentRootMapping("/docs/", [](jshttpserver::HttpRequest &req, jshttpserver::HttpResponse &res) {
      std::cout << "documentRootMapping FOUND :: /docs/ :: path = " << req.doc_path_name << std::endl;
      std::cout << "\tpath_name[ " << req.doc_path_name << " ], search[ " << req.url_search << " ]" << std::endl;
    });

    server->documentRootMapping("/doxs", [](jshttpserver::HttpRequest &req, jshttpserver::HttpResponse &res) {
      std::cout << "documentRootMapping FOUND :: /doxs :: path = " << req.doc_path_name << std::endl;
      std::cout << "\tpath_name[ " << req.doc_path_name << " ], search[ " << req.url_search << " ]" << std::endl;
    });

    server->addListen(8887);

    loop->run();
    return 0;
}

 

https://github.com/jc-lab/jshttpserver-uvw.git

 

jc-lab/jshttpserver-uvw

Contribute to jc-lab/jshttpserver-uvw development by creating an account on GitHub.

github.com

 

Comment +0