From a89617d4573ad7e6b80d5688bff5215c88483bf8 Mon Sep 17 00:00:00 2001 From: Lord-Turmoil Date: Sun, 1 Dec 2024 14:47:39 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9Bfixed=20completed=20task=20not=20st?= =?UTF-8?q?arted=20=E2=9A=92=EF=B8=8Fupdate=20README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 25 +++++++++++++++++-------- src/threading/Task.h | 4 ++-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4907c86..ebdf092 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ > Before you move on, ensure you have CMake and your C++ compiler supports C++ 17.🫡 > [!WARNING] -> If you build **minet-core** with Debug configuration on Ubuntu 20.04, you may get linker error saying missing `libtsan_preinit.o`. Check [(TSAN) /usr/bin/ld can't find libtsan_preinit.o](https://stackoverflow.com/questions/77858687/tsan-usr-bin-ld-cant-find-libtsan-preinit-o) for solutions. +> If you build **minet-core** with TSan (Thread Sanitizer) on Ubuntu 20.04, you may get linker error saying missing `libtsan_preinit.o`. Check [(TSAN) /usr/bin/ld can't find libtsan_preinit.o](https://stackoverflow.com/questions/77858687/tsan-usr-bin-ld-cant-find-libtsan-preinit-o) for solutions. ## Prepare the Repository @@ -60,9 +60,9 @@ int main() { WebHostBuilder::Create() ->UseAppSettings() - ->Get("/ping", RequestHandler<>::Bind( + ->Get("/ping", RequestHandler::Bind( [](const TextRequest& request, TextResponse& response) { - response.Text().append("pong"); + response.Text().assign("pong"); })) ->Build() ->Run(); @@ -114,17 +114,26 @@ Request handler is where you write the server logic. It takes the request and re using RequestHandlerFn = std::function; ``` -To create a request handler, you need to use `RequestHandler::Bind()`. By default, it will use `TextRequest` and `TextResponse`. Then, you can register endpoints like this. +**minet-core** provides a convenient way to create request handlers by using predefined templates. For plain text request and response, you can use `RequestHandler` directly. For JSON request and response, you can use `RestfulHandler`. If request and response types are different, you can use `CustomHandler` instead. Below are their definitions. ```cpp -void ping(const TextRequest& request, TextResponse& response); -void echo(const TextRequest& request, JsonResponse& response); +using RequestHandler = RequestHandlerImpl; +using RestfulHandler = RequestHandlerImpl; +template +using CustomHandler = RequestHandlerImpl; +``` + +```cpp +void text(const TextRequest& request, TextResponse& response); +void json(const JsonRequest& request, JsonResponse& response); +void custom(const TextRequest& request, JsonResponse& response); // create WebHostBuilder builder - ->Get("/ping", RequestHandler<>::Bind(ping)) - ->Post("/echo", RequestHandler::Bind(echo)); + ->Get("/text", RequestHandler::Bind(text)) + ->Post("/json", RestfulHandler::Bind(json)) + ->Post("/custom", CustomHandler::Bind(custom)); ``` See, isn't it easy?😉 diff --git a/src/threading/Task.h b/src/threading/Task.h index ef555f3..7887f55 100644 --- a/src/threading/Task.h +++ b/src/threading/Task.h @@ -111,7 +111,7 @@ template Ref Task::Create(TRoutine&& routine) inline Ref Task::Completed() { - return CreateRef([] {}, Private()); + return CreateRef([] {}, Private())->StartAsync(); } inline Ref Task::StartAsync() @@ -170,7 +170,7 @@ Ref> ValueTask::Create(TRoutine&& routine) template Ref> ValueTask::Completed(const TResult& result) { - return CreateRef([result] { return result; }, Private()); + return CreateRef([result] { return result; }, Private())->StartAsync(); } template Ref> ValueTask::StartAsync()