router.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/service
  41. router.GET("/service", [](const HttpContextPtr& ctx) {
  42. ctx->setContentType("application/json");
  43. ctx->set("base_url", ctx->service->base_url);
  44. ctx->set("document_root", ctx->service->document_root);
  45. ctx->set("home_page", ctx->service->home_page);
  46. ctx->set("error_page", ctx->service->error_page);
  47. ctx->set("index_of", ctx->service->index_of);
  48. return 200;
  49. });
  50. // curl -v http://ip:port/echo -d "hello,world!"
  51. router.POST("/echo", [](const HttpContextPtr& ctx) {
  52. return ctx->send(ctx->body(), ctx->type());
  53. });
  54. // wildcard *
  55. // curl -v http://ip:port/wildcard/any
  56. router.GET("/wildcard*", [](HttpRequest* req, HttpResponse* resp) {
  57. std::string str = req->path + " match /wildcard*";
  58. return resp->String(str);
  59. });
  60. // curl -v http://ip:port/async
  61. router.GET("/async", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
  62. writer->WriteHeader("X-Request-tid", hv_gettid());
  63. std::async([req, writer](){
  64. writer->WriteHeader("X-Response-tid", hv_gettid());
  65. writer->WriteHeader("Content-Type", "text/plain");
  66. writer->WriteBody("This is an async response.\n");
  67. writer->End();
  68. });
  69. });
  70. // curl -v http://ip:port/www.*
  71. // curl -v http://ip:port/www.example.com
  72. router.GET("/www.*", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
  73. HttpRequestPtr req2(new HttpRequest);
  74. req2->url = req->path.substr(1);
  75. requests::async(req2, [writer](const HttpResponsePtr& resp2){
  76. writer->Begin();
  77. if (resp2 == NULL) {
  78. writer->WriteStatus(HTTP_STATUS_NOT_FOUND);
  79. writer->WriteHeader("Content-Type", "text/html");
  80. writer->WriteBody("<center><h1>404 Not Found</h1></center>");
  81. } else {
  82. writer->WriteResponse(resp2.get());
  83. }
  84. writer->End();
  85. });
  86. });
  87. // curl -v http://ip:port/sleep?t=1000
  88. router.GET("/sleep", Handler::sleep);
  89. // curl -v http://ip:port/setTimeout?t=1000
  90. router.GET("/setTimeout", Handler::setTimeout);
  91. // curl -v http://ip:port/query?page_no=1\&page_size=10
  92. router.GET("/query", Handler::query);
  93. // Content-Type: application/x-www-form-urlencoded
  94. // curl -v http://ip:port/kv -H "content-type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
  95. router.POST("/kv", Handler::kv);
  96. // Content-Type: application/json
  97. // curl -v http://ip:port/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
  98. router.POST("/json", Handler::json);
  99. // Content-Type: multipart/form-data
  100. // bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
  101. router.POST("/form", Handler::form);
  102. // curl -v http://ip:port/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
  103. // curl -v http://ip:port/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
  104. // bin/curl -v http://ip:port/test -F 'bool=1 int=123 float=3.14 string=hello'
  105. router.POST("/test", Handler::test);
  106. // Content-Type: application/grpc
  107. // bin/curl -v --http2 http://ip:port/grpc -H "content-type:application/grpc" -d 'protobuf'
  108. router.POST("/grpc", Handler::grpc);
  109. // RESTful API: /group/:group_name/user/:user_id
  110. // curl -v -X DELETE http://ip:port/group/test/user/123
  111. router.Delete("/group/:group_name/user/:user_id", Handler::restful);
  112. // router.Delete("/group/{group_name}/user/{user_id}", Handler::restful);
  113. // bin/curl -v localhost:8080/upload -F "file=@LICENSE"
  114. router.POST("/upload", Handler::upload);
  115. // curl -v http://ip:port/login -H "Content-Type:application/json" -d '{"username":"admin","password":"123456"}'
  116. router.POST("/login", Handler::login);
  117. }
  118. };
  119. #endif // HV_HTTPD_ROUTER_H