1
0

router.h 5.4 KB

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