Fix AOT YAML problem
This commit is contained in:
		
							
								
								
									
										10
									
								
								VirtualDDNSRouter.Server/Context/YamlStaticContext.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								VirtualDDNSRouter.Server/Context/YamlStaticContext.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
using VirtualDDNSRouter.Server.Models;
 | 
			
		||||
using YamlDotNet.Serialization;
 | 
			
		||||
 | 
			
		||||
namespace VirtualDDNSRouter.Server.Context;
 | 
			
		||||
 | 
			
		||||
[YamlStaticContext]
 | 
			
		||||
[YamlSerializable(typeof(Rule))]
 | 
			
		||||
public partial class YamlStaticContext : YamlDotNet.Serialization.StaticContext
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
@@ -5,9 +5,9 @@ namespace VirtualDDNSRouter.Server.Models;
 | 
			
		||||
 | 
			
		||||
public record Rule
 | 
			
		||||
{
 | 
			
		||||
    public string name { get; init; } = string.Empty;
 | 
			
		||||
    public string apiKey { get; init; } = string.Empty;
 | 
			
		||||
    public string path { get; init; } = string.Empty;
 | 
			
		||||
    public string name { get; set; } = string.Empty;
 | 
			
		||||
    public string apiKey { get; set; } = string.Empty;
 | 
			
		||||
    public string path { get; set; } = string.Empty;
 | 
			
		||||
 | 
			
		||||
    public Rule() { } // Needed for AOT static deserializer
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,18 +1,10 @@
 | 
			
		||||
using System.Text.Json.Serialization;
 | 
			
		||||
using Microsoft.AspNetCore.Http.HttpResults;
 | 
			
		||||
using VirtualDDNSRouter.Server.Interfaces;
 | 
			
		||||
using VirtualDDNSRouter.Server.Models;
 | 
			
		||||
using VirtualDDNSRouter.Server.Services;
 | 
			
		||||
using Route = VirtualDDNSRouter.Server.Models.Route;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
var builder = WebApplication.CreateSlimBuilder(args);
 | 
			
		||||
 | 
			
		||||
builder.Services.ConfigureHttpJsonOptions(options =>
 | 
			
		||||
{
 | 
			
		||||
    options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
builder.Services.AddSingleton<IYamlParser, YamlParser>();
 | 
			
		||||
 | 
			
		||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
 | 
			
		||||
@@ -51,16 +43,8 @@ app.MapGet("/goto/{path}", (string path) =>
 | 
			
		||||
    var ruleExists = routes.Any(r => r.path == path);
 | 
			
		||||
    if (!ruleExists) Results.NoContent();
 | 
			
		||||
    var redirectRoute = routes.FirstOrDefault(r => r.path == path);
 | 
			
		||||
    if (redirectRoute is null) return Task.FromResult(Results.NotFound());
 | 
			
		||||
    return Task.FromResult(Results.Redirect($"http://{redirectRoute.ipAddress}:{redirectRoute.port}"));
 | 
			
		||||
    
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
app.Run();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//[JsonSerializable(typeof(Rule))]
 | 
			
		||||
[JsonSerializable(typeof(Route))]
 | 
			
		||||
internal partial class AppJsonSerializerContext : JsonSerializerContext
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
using System.Diagnostics.CodeAnalysis;
 | 
			
		||||
using VirtualDDNSRouter.Server.Context;
 | 
			
		||||
using VirtualDDNSRouter.Server.Interfaces;
 | 
			
		||||
using VirtualDDNSRouter.Server.Models;
 | 
			
		||||
using YamlDotNet.Serialization;
 | 
			
		||||
@@ -18,7 +19,7 @@ public class YamlParser : IYamlParser
 | 
			
		||||
        var yamlContent = await File.ReadAllTextAsync(_yamlFilePath).ConfigureAwait(false);
 | 
			
		||||
 | 
			
		||||
        // Build the deserializer with explicit naming convention
 | 
			
		||||
        var deserializer = new DeserializerBuilder()
 | 
			
		||||
        var deserializer = new StaticDeserializerBuilder(new YamlStaticContext())
 | 
			
		||||
            .WithNamingConvention(UnderscoredNamingConvention.Instance) // maps api_key -> apiKey
 | 
			
		||||
            .IgnoreUnmatchedProperties()
 | 
			
		||||
            .Build();
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,13 @@
 | 
			
		||||
<Project Sdk="Microsoft.NET.Sdk.Web">
 | 
			
		||||
 | 
			
		||||
    <PropertyGroup>
 | 
			
		||||
        <TargetFramework>net10.0</TargetFramework>
 | 
			
		||||
<!--        <TargetFramework>net10.0</TargetFramework>
 | 
			
		||||
        <Nullable>enable</Nullable>
 | 
			
		||||
        <ImplicitUsings>enable</ImplicitUsings>
 | 
			
		||||
        <NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
 | 
			
		||||
        <InvariantGlobalization>true</InvariantGlobalization>
 | 
			
		||||
 | 
			
		||||
        <!-- Publish settings -->
 | 
			
		||||
        <!– Publish settings –>
 | 
			
		||||
        <PublishSingleFile>true</PublishSingleFile>
 | 
			
		||||
        <SelfContained>true</SelfContained>
 | 
			
		||||
        <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
 | 
			
		||||
@@ -19,7 +19,15 @@
 | 
			
		||||
        <DebugSymbols>false</DebugSymbols>
 | 
			
		||||
        <OptimizationLevel>Release</OptimizationLevel>
 | 
			
		||||
        <TieredPGO>true</TieredPGO>
 | 
			
		||||
        <IlcOptimizationPreference>Size</IlcOptimizationPreference>
 | 
			
		||||
        <IlcOptimizationPreference>Size</IlcOptimizationPreference>-->
 | 
			
		||||
 | 
			
		||||
        <TargetFramework>net10.0</TargetFramework>
 | 
			
		||||
        <Nullable>enable</Nullable>
 | 
			
		||||
        <ImplicitUsings>enable</ImplicitUsings>
 | 
			
		||||
        <NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
 | 
			
		||||
        <InvariantGlobalization>true</InvariantGlobalization>
 | 
			
		||||
        <PublishAot>true</PublishAot>
 | 
			
		||||
        <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
 | 
			
		||||
    </PropertyGroup>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -27,7 +35,6 @@
 | 
			
		||||
        <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0-preview.6.25358.103"/>
 | 
			
		||||
        <PackageReference Include="Vecc.YamlDotNet.Analyzers.StaticGenerator" Version="16.3.0" />
 | 
			
		||||
        <PackageReference Include="YamlDotNet" Version="16.3.0" />
 | 
			
		||||
        <TrimmerRootAssembly Include="YamlDotNet"/>
 | 
			
		||||
    </ItemGroup>
 | 
			
		||||
 | 
			
		||||
    <ItemGroup>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user