1
0

router.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef HV_HTTPD_ROUTER_H
  2. #define HV_HTTPD_ROUTER_H
  3. #include <future> // import std::async
  4. #include "HttpService.h"
  5. #include "requests.h"
  6. #include "handler.h"
  7. class Router {
  8. public:
  9. static void Register(HttpService& router) {
  10. // preprocessor => Handler => postprocessor
  11. router.preprocessor = Handler::preprocessor;
  12. router.postprocessor = Handler::postprocessor;
  13. router.largeFileHandler = Handler::largeFileHandler;
  14. // router.errorHandler = Handler::errorHandler;
  15. // curl -v http://ip:port/ping
  16. router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
  17. return resp->String("pong");
  18. });
  19. // curl -v http://ip:port/data
  20. router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
  21. static char data[] = "0123456789";
  22. return resp->Data(data, 10 /*, false */);
  23. });
  24. // curl -v http://ip:port/html/index.html
  25. router.GET("/html/index.html", [](HttpRequest* req, HttpResponse* resp) {
  26. return resp->File("html/index.html");
  27. });
  28. // curl -v http://ip:port/paths
  29. router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
  30. return resp->Json(router.Paths());
  31. });
  32. // curl -v http://ip:port/get?env=1
  33. router.GET("/get", [](HttpRequest* req, HttpResponse* resp) {
  34. resp->json["origin"] = req->client_addr.ip;
  35. resp->json["url"] = req->url;
  36. resp->json["args"] = req->query_params;
  37. resp->json["headers"] = req->headers;
  38. return 200;
  39. });
  40. // curl -v http://ip:port/echo -d "hello,world!"
  41. router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
  42. resp->content_type = req->content_type;
  43. resp->body = req->body;
  44. return 200;
  45. });
  46. // wildcard *
  47. // curl -v http://ip:port/wildcard/any
  48. router.GET("/wildcard*", [](HttpRequest* req, HttpResponse* resp) {
  49. std::string str = req->path + " match /wildcard*";
  50. return resp->String(str);
  51. });
  52. // curl -v http://ip:port/async
  53. router.GET("/async", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
  54. writer->response->headers["X-Request-tid"] = hv::to_string(hv_gettid());
  55. std::async([req, writer](){
  56. writer->Begin();
  57. writer->response->headers["X-Response-tid"] = hv::to_string(hv_gettid());
  58. writer->WriteHeader("Content-Type", "text/plain");
  59. writer->WriteBody("This is an async response.\n");
  60. writer->End();
  61. });
  62. });
  63. // curl -v http://ip:port/www.*
  64. // curl -v http://ip:port/www.example.com
  65. router.GET("/www.*", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
  66. HttpRequestPtr req2(new HttpRequest);
  67. req2->url = req->path.substr(1);
  68. requests::async(req2, [writer](const HttpResponsePtr& resp2){
  69. writer->Begin();
  70. if (resp2 == NULL) {
  71. writer->WriteStatus(HTTP_STATUS_NOT_FOUND);
  72. writer->WriteHeader("Content-Type", "text/html");
  73. writer->WriteBody("<center><h1>404 Not Found</h1></center>");
  74. } else {
  75. writer->WriteResponse(resp2.get());
  76. }
  77. writer->End();
  78. });
  79. });
  80. // curl -v http://ip:port/sleep?t=1000
  81. router.GET("/sleep", Handler::sleep);
  82. // curl -v http://ip:port/setTimeout?t=1000
  83. router.GET("/setTimeout", Handler::setTimeout);
  84. // curl -v http://ip:port/query?page_no=1\&page_size=10
  85. router.GET("/query", Handler::query);
  86. // Content-Type: application/x-www-form-urlencoded
  87. // curl -v http://ip:port/kv -H "content-type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
  88. router.POST("/kv", Handler::kv);
  89. // Content-Type: application/json
  90. // curl -v http://ip:port/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
  91. router.POST("/json", Handler::json);
  92. // Content-Type: multipart/form-data
  93. // bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
  94. router.POST("/form", Handler::form);
  95. // curl -v http://ip:port/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
  96. // curl -v http://ip:port/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
  97. // bin/curl -v http://ip:port/test -F 'bool=1 int=123 float=3.14 string=hello'
  98. router.POST("/test", Handler::test);
  99. // Content-Type: application/grpc
  100. // bin/curl -v --http2 http://ip:port/grpc -H "content-type:application/grpc" -d 'protobuf'
  101. router.POST("/grpc", Handler::grpc);
  102. // RESTful API: /group/:group_name/user/:user_id
  103. // curl -v -X DELETE http://ip:port/group/test/user/123
  104. router.Delete("/group/:group_name/user/:user_id", Handler::restful);
  105. // router.Delete("/group/{group_name}/user/{user_id}", Handler::restful);
  106. // bin/curl -v localhost:8080/upload -F "file=@LICENSE"
  107. router.POST("/upload", Handler::upload);
  108. // curl -v http://ip:port/login -H "Content-Type:application/json" -d '{"username":"admin","password":"123456"}'
  109. router.POST("/login", Handler::login);
  110. }
  111. };
  112. #endif // HV_HTTPD_ROUTER_H