router.h 2.6 KB

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