From 523cdb8ad2b0053ba1789770429aa2b1fef721ef Mon Sep 17 00:00:00 2001 From: uwaa Date: Thu, 7 Nov 2024 09:56:06 +0000 Subject: [PATCH] add synchronous variant of FuncEndpointDelegate --- HTTP/Routing/FuncEndpoint.cs | 8 +++++--- HTTP/Routing/Router.cs | 12 +++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/HTTP/Routing/FuncEndpoint.cs b/HTTP/Routing/FuncEndpoint.cs index a23e1e6..ed50aaa 100644 --- a/HTTP/Routing/FuncEndpoint.cs +++ b/HTTP/Routing/FuncEndpoint.cs @@ -2,13 +2,15 @@ namespace Uwaa.HTTP.Routing; -public delegate Task FuncEndpointDelegate(HttpRequest req); +public delegate Task FuncEndpointDelegateAsync(HttpRequest req); + +public delegate HttpResponse? FuncEndpointDelegate(HttpRequest req); public class FuncEndpoint : RouterBase { - public FuncEndpointDelegate Function; + public FuncEndpointDelegateAsync Function; - public FuncEndpoint(FuncEndpointDelegate function) + public FuncEndpoint(FuncEndpointDelegateAsync function) { Function = function; } diff --git a/HTTP/Routing/Router.cs b/HTTP/Routing/Router.cs index ffd8ac6..f5ed9b9 100644 --- a/HTTP/Routing/Router.cs +++ b/HTTP/Routing/Router.cs @@ -43,7 +43,17 @@ public class Router : RouterBase /// /// Adds an endpoint to the router on the given sub path. /// - public void Add(string? key, FuncEndpointDelegate func) => Routes.Add(new Route(key, new FuncEndpoint(func))); + public void Add(string? key, FuncEndpointDelegateAsync func) => Routes.Add(new Route(key, new FuncEndpoint(func))); + + /// + /// Adds a wildcard endpoint to the router. + /// + public void Add(FuncEndpointDelegateAsync func) => Add(null, func); + + /// + /// Adds an endpoint to the router on the given sub path. + /// + public void Add(string? key, FuncEndpointDelegate func) => Add(key, req => Task.FromResult(func(req))); /// /// Adds a wildcard endpoint to the router.