router.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef HV_HTTPD_ROUTER_H
  2. #define HV_HTTPD_ROUTER_H
  3. #include "HttpService.h"
  4. #include "handler.h"
  5. class Router {
  6. public:
  7. static void Register(HttpService& router) {
  8. // preprocessor => Handler => postprocessor
  9. router.preprocessor = Handler::preprocessor;
  10. router.postprocessor = Handler::postprocessor;
  11. // curl -v http://ip:port/ping
  12. router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
  13. return resp->String("pong");
  14. });
  15. // curl -v http://ip:port/data
  16. router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
  17. static char data[] = "0123456789";
  18. return resp->Data(data, 10);
  19. });
  20. // curl -v http://ip:port/html/index.html
  21. router.GET("/html/index.html", [](HttpRequest* req, HttpResponse* resp) {
  22. return resp->File("html/index.html");
  23. });
  24. // curl -v http://ip:port/paths
  25. router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
  26. return resp->Json(router.Paths());
  27. });
  28. // curl -v http://ip:port/echo -d "hello,world!"
  29. router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
  30. resp->content_type = req->content_type;
  31. resp->body = req->body;
  32. return 200;
  33. });
  34. // wildcard *
  35. // curl -v http://ip:port/wildcard/any
  36. router.GET("/wildcard*", [](HttpRequest* req, HttpResponse* resp){
  37. std::string str = req->path + " match /wildcard*";
  38. return resp->String(str);
  39. });
  40. // curl -v http://ip:port/sleep?t=3
  41. router.GET("/sleep", Handler::sleep);
  42. // curl -v http://ip:port/query?page_no=1\&page_size=10
  43. router.GET("/query", Handler::query);
  44. // Content-Type: application/x-www-form-urlencoded
  45. // curl -v http://ip:port/kv -H "content-type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
  46. router.POST("/kv", Handler::kv);
  47. // Content-Type: application/json
  48. // curl -v http://ip:port/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
  49. router.POST("/json", Handler::json);
  50. // Content-Type: multipart/form-data
  51. // bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
  52. router.POST("/form", Handler::form);
  53. // curl -v http://ip:port/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
  54. // curl -v http://ip:port/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
  55. // bin/curl -v http://ip:port/test -F 'bool=1 int=123 float=3.14 string=hello'
  56. router.POST("/test", Handler::test);
  57. // Content-Type: application/grpc
  58. // bin/curl -v --http2 http://ip:port/grpc -H "content-type:application/grpc" -d 'protobuf'
  59. router.POST("/grpc", Handler::grpc);
  60. // RESTful API: /group/:group_name/user/:user_id
  61. // curl -v -X DELETE http://ip:port/group/test/user/123
  62. router.Delete("/group/:group_name/user/:user_id", Handler::restful);
  63. // bin/curl -v localhost:8080/upload -F "file=@LICENSE"
  64. router.POST("/upload", Handler::upload);
  65. // curl -v http://ip:port/login -H "Content-Type:application/json" -d '{"username":"admin","password":"123456"}'
  66. router.POST("/login", Handler::login);
  67. }
  68. };
  69. #endif // HV_HTTPD_ROUTER_H