Skip to content

Commit

Permalink
⚒️add request handler wrapper
Browse files Browse the repository at this point in the history
🐛webhost builder appsettings path
✨demo client now wait for process to finish
  • Loading branch information
Lord-Turmoil committed Dec 1, 2024
1 parent 75004f3 commit 08d1389
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
4 changes: 2 additions & 2 deletions demo/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ int main(int argc, char* argv[])
logger = builder->GetLogger("Demo");

// Register handlers and run the server.
builder->Get("/ping", RequestHandler<>::Bind(ping))
->Post("/echo", RequestHandler<TextRequest, JsonResponse>::Bind(echo))
builder->Get("/ping", RequestHandler::Bind(ping))
->Post("/echo", CustomHandler<TextRequest, JsonResponse>::Bind(echo))
->Build()
->Run();

Expand Down
13 changes: 8 additions & 5 deletions include/minet/components/RequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#include "minet/core/IRequestHandler.h"

#include "minet/common/Http.h"
#include "minet/components/Requests.h"
#include "minet/components/Responses.h"
#include "minet/core/HttpContext.h"
#include "minet/common/Http.h"

#include <functional>

Expand Down Expand Up @@ -48,13 +48,12 @@ template <typename TResponse> struct ResponseFormatter
* @tparam TRequest Type of the request.
* @tparam TResponse Type of the response.
*/
template <typename TRequest = TextRequest, typename TResponse = TextResponse>
class RequestHandler final : public IRequestHandler
template <typename TRequest, typename TResponse> class RequestHandlerImpl final : public IRequestHandler
{
public:
using RequestHandlerFn = std::function<void(const TRequest&, TResponse&)>;

explicit RequestHandler(RequestHandlerFn handler) : _handler(std::move(handler))
explicit RequestHandlerImpl(RequestHandlerFn handler) : _handler(std::move(handler))
{
}

Expand All @@ -76,13 +75,17 @@ class RequestHandler final : public IRequestHandler

static Ref<IRequestHandler> Bind(RequestHandlerFn handler)
{
return CreateRef<RequestHandler>(std::move(handler));
return CreateRef<RequestHandlerImpl>(std::move(handler));
}

private:
RequestHandlerFn _handler;
};

using RequestHandler = RequestHandlerImpl<TextRequest, TextResponse>;
using RestfulHandler = RequestHandlerImpl<JsonRequest, JsonResponse>;
template <typename TRequest, typename TResponse> using CustomHandler = RequestHandlerImpl<TRequest, TResponse>;

/*
* ===================================================================
* ------------------------- Specializations -------------------------
Expand Down
2 changes: 1 addition & 1 deletion include/minet/core/WebHostBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class WebHostBuilder final : public std::enable_shared_from_this<WebHostBuilder>
* This must be called before any other methods, and it should only be
* called once.
*/
Ref<WebHostBuilder> UseAppSettings(const std::string& path = "");
Ref<WebHostBuilder> UseAppSettings(const std::string& path = "appsettings.json");

Ref<WebHostBuilder> Get(const std::string& path, const Ref<IRequestHandler>& handler);
Ref<WebHostBuilder> Post(const std::string& path, const Ref<IRequestHandler>& handler);
Expand Down
17 changes: 13 additions & 4 deletions scripts/demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function run_server() {
function send_requests() {
BASE_URL="localhost:5000"
ROUND=${1:-10}
start=`date +%s`
for (( i = 1; i <= $ROUND; i++ )); do
rand=$(( RANDOM % 2 )) # choose 0 or 1
if [ $rand -eq 0 ]; then
Expand All @@ -47,8 +46,6 @@ function send_requests() {
# rand=$(( RANDOM % 3 )) # sleep 0-3 seconds
# sleep $rand
done
end=`date +%s`
echo "Sent $ROUND requests in $((end-start)) seconds"
}

function run_client() {
Expand All @@ -58,13 +55,23 @@ function run_client() {
ROUND=${2:?"Expect round"}
if [ $LEVEL -gt 0 ]; then
bash $0 client _ $((LEVEL-1)) $ROUND &
c1=$!
bash $0 client _ $((LEVEL-1)) $ROUND &
c2=$!
wait $c1
wait $c2
else
send_requests $ROUND
fi
else
ROUND=${1:-10}
start=`date +%s`

bash $0 client _ 2 $ROUND
wait $!

end=`date +%s`
echo "Sent $((ROUND*4)) ($ROUND * 4) requests in $((end-start)) seconds"
fi
}

Expand All @@ -74,7 +81,9 @@ if [ "$option" == "server" ]; then
echo -e "\033[0;32mChoose to run server\033[0m"
run_server $@
elif [ "$option" == "client" ]; then
echo -e "\033[0;32mChoose to run client...\033[0m"
if [ "$1" != "_" ]; then
echo -e "\033[0;32mChoose to run client...\033[0m"
fi
run_client $@
else
echo "Usage: $0 [server|client] [args]"
Expand Down
3 changes: 1 addition & 2 deletions src/core/WebHostBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ MINET_BEGIN

static void _PrintBanner();

WebHostBuilder::WebHostBuilder(Private)
: _appSettingsPath("appsettings.json"), _container(mioc::ServiceContainer::New(true)), _initialized(false)
WebHostBuilder::WebHostBuilder(Private) : _container(mioc::ServiceContainer::New(true)), _initialized(false)
{
_PrintBanner();
}
Expand Down

0 comments on commit 08d1389

Please sign in to comment.