|
@@ -7,60 +7,82 @@
|
|
|
|
|
|
|
|
class Router {
|
|
class Router {
|
|
|
public:
|
|
public:
|
|
|
- static void Register(HttpService& http) {
|
|
|
|
|
|
|
+ static void Register(HttpService& router) {
|
|
|
// preprocessor => Handler => postprocessor
|
|
// preprocessor => Handler => postprocessor
|
|
|
- http.preprocessor = Handler::preprocessor;
|
|
|
|
|
- http.postprocessor = Handler::postprocessor;
|
|
|
|
|
|
|
+ router.preprocessor = Handler::preprocessor;
|
|
|
|
|
+ router.postprocessor = Handler::postprocessor;
|
|
|
|
|
|
|
|
// curl -v http://ip:port/ping
|
|
// curl -v http://ip:port/ping
|
|
|
- http.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
|
|
|
|
|
- resp->body = "pong";
|
|
|
|
|
- return 200;
|
|
|
|
|
|
|
+ router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
|
|
|
|
|
+ return resp->String("pong");
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // curl -v http://ip:port/data
|
|
|
|
|
+ router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
|
|
|
|
|
+ static char data[] = "0123456789";
|
|
|
|
|
+ return resp->Data(data, 10);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // curl -v http://ip:port/html/index.html
|
|
|
|
|
+ router.GET("/html/index.html", [&router](HttpRequest* req, HttpResponse* resp) {
|
|
|
|
|
+ return resp->File("html/index.html");
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // curl -v http://ip:port/paths
|
|
|
|
|
+ router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
|
|
|
|
|
+ return resp->Json(router.Paths());
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
// curl -v http://ip:port/echo -d "hello,world!"
|
|
// curl -v http://ip:port/echo -d "hello,world!"
|
|
|
- http.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
|
|
|
|
|
|
|
+ router.POST("/echo", [](HttpRequest* req, HttpResponse* resp) {
|
|
|
resp->content_type = req->content_type;
|
|
resp->content_type = req->content_type;
|
|
|
resp->body = req->body;
|
|
resp->body = req->body;
|
|
|
return 200;
|
|
return 200;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ // wildcard *
|
|
|
|
|
+ // curl -v http://ip:port/wildcard/any
|
|
|
|
|
+ router.GET("/wildcard*", [](HttpRequest* req, HttpResponse* resp){
|
|
|
|
|
+ std::string str = req->path + " match /wildcard*";
|
|
|
|
|
+ return resp->String(str);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
// curl -v http://ip:port/sleep?t=3
|
|
// curl -v http://ip:port/sleep?t=3
|
|
|
- http.GET("/sleep", Handler::sleep);
|
|
|
|
|
|
|
+ router.GET("/sleep", Handler::sleep);
|
|
|
|
|
|
|
|
// curl -v http://ip:port/query?page_no=1\&page_size=10
|
|
// curl -v http://ip:port/query?page_no=1\&page_size=10
|
|
|
- http.GET("/query", Handler::query);
|
|
|
|
|
|
|
+ router.GET("/query", Handler::query);
|
|
|
|
|
|
|
|
// Content-Type: application/x-www-form-urlencoded
|
|
// Content-Type: application/x-www-form-urlencoded
|
|
|
// curl -v http://ip:port/kv -H "content-type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
|
|
// curl -v http://ip:port/kv -H "content-type:application/x-www-form-urlencoded" -d 'user=admin&pswd=123456'
|
|
|
- http.POST("/kv", Handler::kv);
|
|
|
|
|
|
|
+ router.POST("/kv", Handler::kv);
|
|
|
|
|
|
|
|
// Content-Type: application/json
|
|
// Content-Type: application/json
|
|
|
// curl -v http://ip:port/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
|
|
// curl -v http://ip:port/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
|
|
|
- http.POST("/json", Handler::json);
|
|
|
|
|
|
|
+ router.POST("/json", Handler::json);
|
|
|
|
|
|
|
|
// Content-Type: multipart/form-data
|
|
// Content-Type: multipart/form-data
|
|
|
// bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
|
|
// bin/curl -v localhost:8080/form -F "user=admin pswd=123456"
|
|
|
- http.POST("/form", Handler::form);
|
|
|
|
|
|
|
+ router.POST("/form", Handler::form);
|
|
|
|
|
|
|
|
// curl -v http://ip:port/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
|
|
// curl -v http://ip:port/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
|
|
|
// curl -v http://ip:port/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
|
|
// curl -v http://ip:port/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
|
|
|
// bin/curl -v http://ip:port/test -F 'bool=1 int=123 float=3.14 string=hello'
|
|
// bin/curl -v http://ip:port/test -F 'bool=1 int=123 float=3.14 string=hello'
|
|
|
- http.POST("/test", Handler::test);
|
|
|
|
|
|
|
+ router.POST("/test", Handler::test);
|
|
|
|
|
|
|
|
// Content-Type: application/grpc
|
|
// Content-Type: application/grpc
|
|
|
// bin/curl -v --http2 http://ip:port/grpc -H "content-type:application/grpc" -d 'protobuf'
|
|
// bin/curl -v --http2 http://ip:port/grpc -H "content-type:application/grpc" -d 'protobuf'
|
|
|
- http.POST("/grpc", Handler::grpc);
|
|
|
|
|
|
|
+ router.POST("/grpc", Handler::grpc);
|
|
|
|
|
|
|
|
// RESTful API: /group/:group_name/user/:user_id
|
|
// RESTful API: /group/:group_name/user/:user_id
|
|
|
// curl -v -X DELETE http://ip:port/group/test/user/123
|
|
// curl -v -X DELETE http://ip:port/group/test/user/123
|
|
|
- http.Delete("/group/:group_name/user/:user_id", Handler::restful);
|
|
|
|
|
|
|
+ router.Delete("/group/:group_name/user/:user_id", Handler::restful);
|
|
|
|
|
|
|
|
// bin/curl -v localhost:8080/upload -F "file=@LICENSE"
|
|
// bin/curl -v localhost:8080/upload -F "file=@LICENSE"
|
|
|
- http.POST("/upload", Handler::upload);
|
|
|
|
|
|
|
+ router.POST("/upload", Handler::upload);
|
|
|
|
|
|
|
|
// curl -v http://ip:port/login -H "Content-Type:application/json" -d '{"username":"admin","password":"123456"}'
|
|
// curl -v http://ip:port/login -H "Content-Type:application/json" -d '{"username":"admin","password":"123456"}'
|
|
|
- http.POST("/login", Handler::login);
|
|
|
|
|
|
|
+ router.POST("/login", Handler::login);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|