router.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "router.h"
  2. #include "handler.h"
  3. #include "hthread.h" // import hv_gettid
  4. #include "hasync.h" // import hv::async
  5. #include "requests.h" // import requests::async
  6. void Router::Register(hv::HttpService& router) {
  7. /* handler chain */
  8. // preprocessor -> middleware -> processor -> postprocessor
  9. // processor: pathHandlers -> staticHandler -> errorHandler
  10. router.preprocessor = Handler::preprocessor;
  11. router.postprocessor = Handler::postprocessor;
  12. // router.errorHandler = Handler::errorHandler;
  13. // router.largeFileHandler = Handler::sendLargeFile;
  14. // middleware
  15. // router.Use(Handler::Authorization);
  16. // curl -v http://ip:port/ping
  17. router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
  18. return resp->String("pong");
  19. });
  20. // curl -v http://ip:port/data
  21. router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
  22. static char data[] = "0123456789";
  23. return resp->Data(data, 10 /*, false */);
  24. });
  25. // curl -v http://ip:port/html/index.html
  26. router.GET("/html/index.html", [](HttpRequest* req, HttpResponse* resp) {
  27. return resp->File("html/index.html");
  28. });
  29. // curl -v http://ip:port/paths
  30. router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
  31. return resp->Json(router.Paths());
  32. });
  33. // curl -v http://ip:port/service
  34. router.GET("/service", [](const HttpContextPtr& ctx) {
  35. ctx->setContentType("application/json");
  36. ctx->set("base_url", ctx->service->base_url);
  37. ctx->set("document_root", ctx->service->document_root);
  38. ctx->set("home_page", ctx->service->home_page);
  39. ctx->set("error_page", ctx->service->error_page);
  40. ctx->set("index_of", ctx->service->index_of);
  41. return 200;
  42. });
  43. // curl -v http://ip:port/get?env=1
  44. router.GET("/get", [](const HttpContextPtr& ctx) {
  45. hv::Json resp;
  46. resp["origin"] = ctx->ip();
  47. resp["url"] = ctx->url();
  48. resp["args"] = ctx->params();
  49. resp["headers"] = ctx->headers();
  50. return ctx->send(resp.dump(2));
  51. });
  52. // curl -v http://ip:port/echo -d "hello,world!"
  53. router.POST("/echo", [](const HttpContextPtr& ctx) {
  54. return ctx->send(ctx->body(), ctx->type());
  55. });
  56. // wildcard *
  57. // curl -v http://ip:port/wildcard/any
  58. router.GET("/wildcard*", [](HttpRequest* req, HttpResponse* resp) {
  59. std::string str = req->path + " match /wildcard*";
  60. return resp->String(str);
  61. });
  62. // curl -v http://ip:port/async
  63. router.GET("/async", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
  64. writer->Begin();
  65. writer->WriteHeader("X-Response-tid", hv_gettid());
  66. writer->WriteHeader("Content-Type", "text/plain");
  67. writer->WriteBody("This is an async response.\n");
  68. writer->End();
  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. auto req2 = std::make_shared<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 http://ip:port/form -F 'user=admin' -F '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' -F 'int=123' -F 'float=3.14' -F '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. // curl -v http://ip:port/login -H "Content-Type:application/json" -d '{"username":"admin","password":"123456"}'
  114. router.POST("/login", Handler::login);
  115. // curl -v http://ip:port/upload?filename=LICENSE -d '@LICENSE'
  116. // curl -v http://ip:port/upload -F 'file=@LICENSE'
  117. router.POST("/upload", Handler::upload);
  118. // curl -v http://ip:port/upload/README.md -d '@README.md'
  119. router.POST("/upload/{filename}", Handler::recvLargeFile);
  120. // SSE: Server Send Events
  121. // @test html/EventSource.html EventSource.onmessage
  122. router.GET("/sse", Handler::sse);
  123. }