router.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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& http) {
  8. // preprocessor => Handler => postprocessor
  9. http.preprocessor = Handler::preprocessor;
  10. http.postprocessor = Handler::postprocessor;
  11. // curl -v http://ip:port/ping
  12. http.GET("/ping", [](HttpRequest* req, HttpResponse* res) {
  13. res->body = "PONG";
  14. return 200;
  15. });
  16. // curl -v http://ip:port/echo -d "hello,world!"
  17. http.POST("/echo", [](HttpRequest* req, HttpResponse* res) {
  18. res->content_type = req->content_type;
  19. res->body = req->body;
  20. return 200;
  21. });
  22. // curl -v http://ip:port/query?page_no=1\&page_size=10
  23. http.GET("/query", Handler::query);
  24. // Content-Type: application/x-www-form-urlencoded
  25. // curl -v http://ip:port/kv -H "content-type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
  26. http.POST("/kv", Handler::kv);
  27. // Content-Type: application/json
  28. // curl -v http://ip:port/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
  29. http.POST("/json", Handler::json);
  30. // Content-Type: multipart/form-data
  31. // bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
  32. http.POST("/form", Handler::form);
  33. // curl -v http://ip:port/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
  34. // curl -v http://ip:port/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
  35. // bin/curl -v http://ip:port/test -F 'bool=1 int=123 float=3.14 string=hello'
  36. http.POST("/test", Handler::test);
  37. // Content-Type: application/grpc
  38. // bin/curl -v --http2 http://ip:port/grpc -H "content-type:application/grpc" -d 'protobuf'
  39. http.POST("/grpc", Handler::grpc);
  40. // RESTful API: /group/:group_name/user/:user_id
  41. // curl -v -X DELETE http://ip:port/group/test/user/123
  42. http.DELETE("/group/:group_name/user/:user_id", Handler::restful);
  43. // bin/curl -v localhost:8080/upload -F "file=@LICENSE"
  44. http.POST("/upload", Handler::upload);
  45. // curl -v http://ip:port/login -H "Content-Type:application/json" -d '{"username":"admin","password":"123456"}'
  46. http.POST("/login", Handler::login);
  47. }
  48. };
  49. #endif // HV_HTTPD_ROUTER_H