router.cpp 5.5 KB

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