router.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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->resp->headers["X-Request-tid"] = hv::to_string(hv_gettid());
  45. std::async([req, writer](){
  46. writer->Begin();
  47. std::string resp_tid = hv::to_string(hv_gettid());
  48. writer->resp->headers["X-Response-tid"] = hv::to_string(hv_gettid());
  49. writer->WriteHeader("Content-Type", "text/plain");
  50. writer->WriteBody("This is an async response.\n");
  51. writer->End();
  52. });
  53. });
  54. // curl -v http://ip:port/www/*
  55. // curl -v http://ip:port/www/example.com
  56. // curl -v http://ip:port/www/example/com
  57. router.GET("/www/*", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
  58. HttpRequestPtr req2(new HttpRequest);
  59. req2->url = replace(req->path.substr(1), "/", ".");
  60. // printf("url=%s\n", req2->url.c_str());
  61. http_client_send_async(req2, [writer](const HttpResponsePtr& resp2){
  62. writer->Begin();
  63. writer->resp = resp2;
  64. writer->End();
  65. });
  66. });
  67. // curl -v http://ip:port/sleep?t=1000
  68. router.GET("/sleep", Handler::sleep);
  69. // curl -v http://ip:port/setTimeout?t=1000
  70. router.GET("/setTimeout", Handler::setTimeout);
  71. // curl -v http://ip:port/query?page_no=1\&page_size=10
  72. router.GET("/query", Handler::query);
  73. // Content-Type: application/x-www-form-urlencoded
  74. // curl -v http://ip:port/kv -H "content-type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
  75. router.POST("/kv", Handler::kv);
  76. // Content-Type: application/json
  77. // curl -v http://ip:port/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
  78. router.POST("/json", Handler::json);
  79. // Content-Type: multipart/form-data
  80. // bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
  81. router.POST("/form", Handler::form);
  82. // curl -v http://ip:port/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
  83. // curl -v http://ip:port/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
  84. // bin/curl -v http://ip:port/test -F 'bool=1 int=123 float=3.14 string=hello'
  85. router.POST("/test", Handler::test);
  86. // Content-Type: application/grpc
  87. // bin/curl -v --http2 http://ip:port/grpc -H "content-type:application/grpc" -d 'protobuf'
  88. router.POST("/grpc", Handler::grpc);
  89. // RESTful API: /group/:group_name/user/:user_id
  90. // curl -v -X DELETE http://ip:port/group/test/user/123
  91. router.Delete("/group/:group_name/user/:user_id", Handler::restful);
  92. // bin/curl -v localhost:8080/upload -F "file=@LICENSE"
  93. router.POST("/upload", Handler::upload);
  94. // curl -v http://ip:port/login -H "Content-Type:application/json" -d '{"username":"admin","password":"123456"}'
  95. router.POST("/login", Handler::login);
  96. }
  97. };
  98. #endif // HV_HTTPD_ROUTER_H