|
@@ -5,6 +5,8 @@
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
#include "HttpServer.h"
|
|
#include "HttpServer.h"
|
|
|
|
|
+#include "hthread.h" // import hv_gettid
|
|
|
|
|
+#include "hasync.h" // import hv::async
|
|
|
|
|
|
|
|
using namespace hv;
|
|
using namespace hv;
|
|
|
|
|
|
|
@@ -34,12 +36,20 @@ int main(int argc, char** argv) {
|
|
|
|
|
|
|
|
HttpService router;
|
|
HttpService router;
|
|
|
|
|
|
|
|
|
|
+ /* Static file service */
|
|
|
// curl -v http://ip:port/
|
|
// curl -v http://ip:port/
|
|
|
router.Static("/", "./html");
|
|
router.Static("/", "./html");
|
|
|
|
|
|
|
|
- // curl -v http://ip:port/proxy/get
|
|
|
|
|
- router.Proxy("/proxy/", "http://httpbin.org/");
|
|
|
|
|
|
|
+ /* Forward proxy service */
|
|
|
|
|
+ router.EnableForwardProxy();
|
|
|
|
|
+ // curl -v http://httpbin.org/get --proxy http://127.0.0.1:8080
|
|
|
|
|
+ router.AddTrustProxy("*httpbin.org");
|
|
|
|
|
|
|
|
|
|
+ /* Reverse proxy service */
|
|
|
|
|
+ // curl -v http://ip:port/httpbin/get
|
|
|
|
|
+ router.Proxy("/httpbin/", "http://httpbin.org/");
|
|
|
|
|
+
|
|
|
|
|
+ /* API handlers */
|
|
|
// curl -v http://ip:port/ping
|
|
// curl -v http://ip:port/ping
|
|
|
router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
|
|
router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
|
|
|
return resp->String("pong");
|
|
return resp->String("pong");
|
|
@@ -57,12 +67,13 @@ int main(int argc, char** argv) {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
// curl -v http://ip:port/get?env=1
|
|
// curl -v http://ip:port/get?env=1
|
|
|
- router.GET("/get", [](HttpRequest* req, HttpResponse* resp) {
|
|
|
|
|
- resp->json["origin"] = req->client_addr.ip;
|
|
|
|
|
- resp->json["url"] = req->url;
|
|
|
|
|
- resp->json["args"] = req->query_params;
|
|
|
|
|
- resp->json["headers"] = req->headers;
|
|
|
|
|
- return 200;
|
|
|
|
|
|
|
+ router.GET("/get", [](const HttpContextPtr& ctx) {
|
|
|
|
|
+ hv::Json resp;
|
|
|
|
|
+ resp["origin"] = ctx->ip();
|
|
|
|
|
+ resp["url"] = ctx->url();
|
|
|
|
|
+ resp["args"] = ctx->params();
|
|
|
|
|
+ resp["headers"] = ctx->headers();
|
|
|
|
|
+ return ctx->send(resp.dump(2));
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
// curl -v http://ip:port/echo -d "hello,world!"
|
|
// curl -v http://ip:port/echo -d "hello,world!"
|
|
@@ -77,6 +88,22 @@ int main(int argc, char** argv) {
|
|
|
return ctx->send(resp.dump(2));
|
|
return ctx->send(resp.dump(2));
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ // curl -v http://ip:port/async
|
|
|
|
|
+ router.GET("/async", [](const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
|
|
|
|
|
+ writer->Begin();
|
|
|
|
|
+ writer->WriteHeader("X-Response-tid", hv_gettid());
|
|
|
|
|
+ writer->WriteHeader("Content-Type", "text/plain");
|
|
|
|
|
+ writer->WriteBody("This is an async response.\n");
|
|
|
|
|
+ writer->End();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // middleware
|
|
|
|
|
+ router.AllowCORS();
|
|
|
|
|
+ router.Use([](HttpRequest* req, HttpResponse* resp) {
|
|
|
|
|
+ resp->SetHeader("X-Request-tid", hv::to_string(hv_gettid()));
|
|
|
|
|
+ return HTTP_STATUS_NEXT;
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
HttpServer server;
|
|
HttpServer server;
|
|
|
server.service = &router;
|
|
server.service = &router;
|
|
|
server.port = port;
|
|
server.port = port;
|
|
@@ -102,5 +129,6 @@ int main(int argc, char** argv) {
|
|
|
|
|
|
|
|
// press Enter to stop
|
|
// press Enter to stop
|
|
|
while (getchar() != '\n');
|
|
while (getchar() != '\n');
|
|
|
|
|
+ hv::async::cleanup();
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|