Add custom route constraints for char and ulong parameters

This commit is contained in:
2025-11-14 05:04:03 +00:00
parent 37d8df4b7d
commit 6f0de86be4
3 changed files with 95 additions and 1 deletions

View File

@@ -0,0 +1,43 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2021 Natalia Portillo
*******************************************************************************/
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace Marechai.Server.Helpers;
/// <summary>Custom route constraint for char parameters</summary>
public class CharRouteConstraint : IRouteConstraint
{
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values,
RouteDirection routeDirection)
{
if(!values.TryGetValue(routeKey, out object value)) return false;
string stringValue = value?.ToString() ?? string.Empty;
return stringValue.Length == 1 && char.TryParse(stringValue, out _);
}
}

View File

@@ -0,0 +1,41 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2021 Natalia Portillo
*******************************************************************************/
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace Marechai.Server.Helpers;
/// <summary>Custom route constraint for ulong parameters</summary>
public class UlongRouteConstraint : IRouteConstraint
{
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values,
RouteDirection routeDirection)
{
if(!values.TryGetValue(routeKey, out object value)) return false;
return ulong.TryParse(value?.ToString() ?? string.Empty, out _);
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Aaru.CommonTypes.Interop;
using Marechai.Database;
using Marechai.Database.Models;
@@ -10,6 +11,7 @@ using Marechai.Server.Services;
using Markdig;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -21,7 +23,7 @@ namespace Marechai.Server;
file class Program
{
static IDbCore _database;
private static IDbCore _database;
public static void Main(string[] args)
{
@@ -154,11 +156,19 @@ file class Program
options.JsonSerializerOptions.Converters.Add(new IsoDateTimeConverter());
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.WriteIndented = true;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
// Add custom route constraints
builder.Services.Configure<RouteOptions>(options =>
{
options.ConstraintMap["ulong"] = typeof(UlongRouteConstraint);
options.ConstraintMap["char"] = typeof(CharRouteConstraint);
});
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;