abstract class replaced with IRouteHandler interface

This commit is contained in:
Timerix 2025-04-06 12:19:11 +05:00
parent 448161239e
commit 29cde170c6
5 changed files with 13 additions and 13 deletions

View File

@ -1,6 +1,6 @@
namespace DTLib.Web.Routes; namespace DTLib.Web.Routes;
public class DelegateRouteHandler(Func<HttpListenerContext, Task<HttpStatusCode>> routeHandler) : RouteHandler public class DelegateRouteHandler(Func<HttpListenerContext, Task<HttpStatusCode>> routeHandler) : IRouteHandler
{ {
public override Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx) => routeHandler(ctx); public Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx) => routeHandler(ctx);
} }

View File

@ -0,0 +1,6 @@
namespace DTLib.Web.Routes;
public interface IRouteHandler
{
public Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx);
}

View File

@ -1,6 +0,0 @@
namespace DTLib.Web.Routes;
public abstract class RouteHandler
{
public abstract Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx);
}

View File

@ -1,8 +1,8 @@
namespace DTLib.Web.Routes; namespace DTLib.Web.Routes;
public class ServeFilesRouteHandler(IOPath _publicDir, string _homePageUrl = "index.html") : RouteHandler public class ServeFilesRouteHandler(IOPath _publicDir, string _homePageUrl = "index.html") : IRouteHandler
{ {
public override async Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx) public async Task<HttpStatusCode> HandleRequest(HttpListenerContext ctx)
{ {
if (ctx.Request.HttpMethod != "GET") if (ctx.Request.HttpMethod != "GET")
return HttpStatusCode.BadRequest; return HttpStatusCode.BadRequest;

View File

@ -3,9 +3,9 @@ namespace DTLib.Web.Routes;
public class SimpleRouter : IRouter public class SimpleRouter : IRouter
{ {
/// route for any url that doesn't have its own handler /// route for any url that doesn't have its own handler
public RouteHandler? DefaultRoute { get; set; } public IRouteHandler? DefaultRoute { get; set; }
private readonly Dictionary<string, RouteHandler> _routes = new(); private readonly Dictionary<string, IRouteHandler> _routes = new();
private readonly ILogger _logger; private readonly ILogger _logger;
public SimpleRouter(ILogger logger) public SimpleRouter(ILogger logger)
@ -13,7 +13,7 @@ public class SimpleRouter : IRouter
_logger = logger; _logger = logger;
} }
public void MapRoute(string url, HttpMethod method, RouteHandler route) => _routes.Add($"{url}:{method}", route); public void MapRoute(string url, HttpMethod method, IRouteHandler route) => _routes.Add($"{url}:{method}", route);
public void MapRoute(string url, HttpMethod method, Func<HttpListenerContext, Task<HttpStatusCode>> route) public void MapRoute(string url, HttpMethod method, Func<HttpListenerContext, Task<HttpStatusCode>> route)
=> MapRoute(url, method, new DelegateRouteHandler(route)); => MapRoute(url, method, new DelegateRouteHandler(route));