Compare commits
2 commits
d94fa95162
...
523cdb8ad2
Author | SHA1 | Date | |
---|---|---|---|
|
523cdb8ad2 | ||
|
c6d99ea9be |
3 changed files with 40 additions and 5 deletions
|
@ -1,8 +1,12 @@
|
|||
namespace Uwaa.HTTP;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Uwaa.HTTP;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a Multipurpose Internet Mail Extensions type, which indicates the nature and format of a document/file/chunk of data.
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(MIMETypeConverter))]
|
||||
public readonly record struct MIMEType
|
||||
{
|
||||
public static explicit operator string(MIMEType type) => type.ToString();
|
||||
|
@ -73,3 +77,22 @@ public readonly record struct MIMEType
|
|||
/// </summary>
|
||||
public override readonly string ToString() => $"{Type ?? "*"}/{Subtype ?? "*"}";
|
||||
}
|
||||
|
||||
class MIMETypeConverter : JsonConverter<MIMEType>
|
||||
{
|
||||
public sealed override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(MIMEType);
|
||||
|
||||
public override MIMEType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
string? str = reader.GetString();
|
||||
if (str == null)
|
||||
throw new JsonException("Cannot read MIME type");
|
||||
|
||||
return new MIMEType(str);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, MIMEType status, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(status.ToString());
|
||||
}
|
||||
}
|
|
@ -2,13 +2,15 @@
|
|||
|
||||
namespace Uwaa.HTTP.Routing;
|
||||
|
||||
public delegate Task<HttpResponse?> FuncEndpointDelegate(HttpRequest req);
|
||||
public delegate Task<HttpResponse?> 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;
|
||||
}
|
||||
|
|
|
@ -43,7 +43,17 @@ public class Router : RouterBase
|
|||
/// <summary>
|
||||
/// Adds an endpoint to the router on the given sub path.
|
||||
/// </summary>
|
||||
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)));
|
||||
|
||||
/// <summary>
|
||||
/// Adds a wildcard endpoint to the router.
|
||||
/// </summary>
|
||||
public void Add(FuncEndpointDelegateAsync func) => Add(null, func);
|
||||
|
||||
/// <summary>
|
||||
/// Adds an endpoint to the router on the given sub path.
|
||||
/// </summary>
|
||||
public void Add(string? key, FuncEndpointDelegate func) => Add(key, req => Task.FromResult(func(req)));
|
||||
|
||||
/// <summary>
|
||||
/// Adds a wildcard endpoint to the router.
|
||||
|
|
Loading…
Reference in a new issue