Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed Apr 24, 2024
1 parent ffc9396 commit 3ef5f55
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
25 changes: 20 additions & 5 deletions src/Routing/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

record struct RouteDictItem(System.Type type, System.Func<object, Sisk.Core.Http.HttpResponse> lambda);
record struct RouteDictItem(System.Type type, Delegate lambda);

namespace Sisk.Core.Routing
{
Expand Down Expand Up @@ -50,8 +50,8 @@ internal void BindServer(HttpServer server)
}
else
{
ParentServer = server;
server.handler.SetupRouter(this);
ParentServer = server;
}
}

Expand All @@ -70,6 +70,17 @@ public static string CombinePaths(params string[] paths)
return PathUtility.CombinePaths(paths);
}

/// <summary>
/// Gets an boolean indicating where this <see cref="Router"/> is read-only or not.
/// </summary>
/// <definition>
/// public bool IsReadOnly { get; }
/// </definition>
/// <type>
/// Property
/// </type>
public bool IsReadOnly { get => ParentServer is not null; }

/// <summary>
/// Gets or sets whether this <see cref="Router"/> will match routes ignoring case.
/// </summary>
Expand Down Expand Up @@ -162,8 +173,12 @@ public Router()
/// <type>
/// Method
/// </type>
public void RegisterValueHandler<T>(Func<object, HttpResponse> actionHandler)
public void RegisterValueHandler<T>(Func<T, HttpResponse> actionHandler)
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.Router_ReadOnlyException);
}
Type type = typeof(T);
if (type == typeof(HttpResponse))
{
Expand Down Expand Up @@ -194,9 +209,9 @@ HttpResponse ResolveAction(object routeResult)
for (int i = 0; i < hspan.Length; i++)
{
ref RouteDictItem current = ref Unsafe.Add(ref pointer, i);
if (current.type.Equals(actionType))
if (actionType.IsAssignableTo(current.type))
{
return current.lambda(routeResult);
return (HttpResponse)current.lambda.DynamicInvoke(routeResult)!;
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Routing/Router__CoreSetters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,13 @@ public void SetRoute(RouteMethod method, string path, RouteAction action, string
/// </definition>
/// <type>
/// Method
/// </type>
/// </type>
public void SetRoute(Route r)
{
if (IsReadOnly)
{
throw new InvalidOperationException(SR.Router_ReadOnlyException);
}
Route? collisonRoute;
if (!r.UseRegex && (collisonRoute = GetCollisionRoute(r.Method, r.Path)) != null)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Sisk.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<EnableTrimAnalyzer>True</EnableTrimAnalyzer>
<IsAotCompatible>true</IsAotCompatible>

<PackageId>Sisk.HttpServer</PackageId>
<Title>Sisk.HttpServer</Title>
Expand All @@ -30,7 +31,7 @@

<AssemblyVersion>0.16.2.0</AssemblyVersion>
<FileVersion>0.16.2.0</FileVersion>
<Version>0.16.2.0-beta2</Version>
<Version>0.16.2.0-beta4</Version>

<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<SignAssembly>False</SignAssembly>
Expand Down

0 comments on commit 3ef5f55

Please sign in to comment.