router.h 5.0 KB

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