Skip to content

Commit 1785149

Browse files
committed
chore: migrate to new syntax of dotnet 10
1 parent 0c49e44 commit 1785149

28 files changed

+1191
-1121
lines changed

src/GZCTF.AppHost/MinIO/MinIOResource.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,11 @@ class MinIOResource(string name, string? accessKey = null, string? secretKey = n
3737
internal const int DefaultApiPort = 9000;
3838
internal const int DefaultConsolePort = 9001;
3939

40-
private EndpointReference? _apiReference;
41-
private EndpointReference? _consoleReference;
42-
4340
private EndpointReference ApiEndpoint =>
44-
_apiReference ??= new EndpointReference(this, ApiEndpointName);
41+
field ??= new EndpointReference(this, ApiEndpointName);
4542

4643
private EndpointReference ConsoleEndpoint =>
47-
_consoleReference ??= new EndpointReference(this, ConsoleEndpointName);
44+
field ??= new EndpointReference(this, ConsoleEndpointName);
4845

4946
public ReferenceExpression ConnectionStringExpression =>
5047
ReferenceExpression.Create(
@@ -85,18 +82,17 @@ internal static IResourceBuilder<MinIOResource> AddMinIO(
8582
.WithArgs("server", "/data", "--console-address", $":{MinIOResource.DefaultConsolePort}");
8683
}
8784

88-
private static IResourceBuilder<MinIOResource> ConfigureCredentials(
89-
this IResourceBuilder<MinIOResource> builder,
90-
MinIOBuilder options) => builder
91-
.WithEnvironment("MINIO_ROOT_USER", options.AccessKey ?? "minioadmin")
92-
.WithEnvironment("MINIO_ROOT_PASSWORD", options.SecretKey ?? "minioadmin");
93-
94-
private static IResourceBuilder<MinIOResource> ConfigureVolume(
95-
this IResourceBuilder<MinIOResource> builder,
96-
MinIOBuilder options)
85+
extension(IResourceBuilder<MinIOResource> builder)
9786
{
98-
if (!string.IsNullOrEmpty(options.DataVolumePath))
99-
builder = builder.WithVolume(options.DataVolumePath, "/data");
100-
return builder;
87+
private IResourceBuilder<MinIOResource> ConfigureCredentials(MinIOBuilder options) => builder
88+
.WithEnvironment("MINIO_ROOT_USER", options.AccessKey ?? "minioadmin")
89+
.WithEnvironment("MINIO_ROOT_PASSWORD", options.SecretKey ?? "minioadmin");
90+
91+
IResourceBuilder<MinIOResource> ConfigureVolume(MinIOBuilder options)
92+
{
93+
if (!string.IsNullOrEmpty(options.DataVolumePath))
94+
builder = builder.WithVolume(options.DataVolumePath, "/data");
95+
return builder;
96+
}
10197
}
10298
}

src/GZCTF/Controllers/AccountController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System.Net.Mime;
2-
using GZCTF.Extensions;
32
using GZCTF.Middlewares;
43
using GZCTF.Models.Internal;
54
using GZCTF.Models.Request.Account;
65
using GZCTF.Repositories.Interface;
6+
using GZCTF.Services;
77
using GZCTF.Services.Config;
88
using GZCTF.Services.Mail;
99
using Microsoft.AspNetCore.Identity;
@@ -24,7 +24,7 @@ public class AccountController(
2424
IMailSender mailSender,
2525
IBlobRepository blobService,
2626
IHostEnvironment environment,
27-
ICaptchaExtension captcha,
27+
ICaptchaService captcha,
2828
IConfigService configService,
2929
IOptionsSnapshot<AccountPolicy> accountPolicy,
3030
IOptionsSnapshot<GlobalConfig> globalConfig,

src/GZCTF/Controllers/InfoController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Security.Cryptography;
2-
using GZCTF.Extensions;
32
using GZCTF.Middlewares;
43
using GZCTF.Models.Internal;
54
using GZCTF.Models.Request.Account;
65
using GZCTF.Models.Request.Info;
76
using GZCTF.Repositories.Interface;
7+
using GZCTF.Services;
88
using GZCTF.Services.Cache;
99
using Microsoft.AspNetCore.Mvc;
1010
using Microsoft.AspNetCore.RateLimiting;
@@ -22,7 +22,7 @@ namespace GZCTF.Controllers;
2222
public class InfoController(
2323
CacheHelper cacheHelper,
2424
IDistributedCache cache,
25-
ICaptchaExtension captcha,
25+
ICaptchaService captcha,
2626
IPostRepository postRepository,
2727
IServiceProvider serviceProvider,
2828
ILogger<InfoController> logger,

src/GZCTF/Extensions/DatabaseSinkExtension.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ namespace GZCTF.Extensions;
99

1010
public static class DatabaseSinkExtension
1111
{
12-
public static LoggerConfiguration Database(this LoggerSinkConfiguration loggerConfiguration,
13-
IServiceProvider serviceProvider) =>
14-
loggerConfiguration.Sink(new DatabaseSink(serviceProvider), LogEventLevel.Information);
12+
extension(LoggerSinkConfiguration loggerConfiguration)
13+
{
14+
public LoggerConfiguration Database(IServiceProvider serviceProvider) =>
15+
loggerConfiguration.Sink(new DatabaseSink(serviceProvider), LogEventLevel.Information);
16+
}
1517
}
1618

1719
public class DatabaseSink : ILogEventSink, IDisposable

src/GZCTF/Extensions/HandlerExtension.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,35 @@ static readonly DistributedCacheEntryOptions
3535

3636
static string IndexTemplate = string.Empty;
3737

38-
public static void AddEntityConfiguration(this IConfigurationBuilder builder,
39-
Action<DbContextOptionsBuilder> optionsAction) =>
40-
builder.Add(new EntityConfigurationSource(optionsAction));
41-
42-
public static void UseCustomFavicon(this WebApplication app) =>
43-
app.MapGet("/favicon.webp", FaviconHandler);
38+
extension(IConfigurationBuilder builder)
39+
{
40+
public void AddEntityConfiguration(Action<DbContextOptionsBuilder> optionsAction) =>
41+
builder.Add(new EntityConfigurationSource(optionsAction));
42+
}
4443

45-
public static void UseIndexAsync(this WebApplication app)
44+
extension(WebApplication app)
4645
{
47-
var index = app.Environment.WebRootFileProvider.GetFileInfo("index.html");
46+
public void UseCustomFavicon() =>
47+
app.MapGet("/favicon.webp", FaviconHandler);
4848

49-
if (!index.Exists || index.PhysicalPath is null)
49+
public void UseIndexAsync()
5050
{
51-
app.MapFallback(context =>
51+
var index = app.Environment.WebRootFileProvider.GetFileInfo("index.html");
52+
53+
if (!index.Exists || index.PhysicalPath is null)
5254
{
53-
context.Response.StatusCode = StatusCodes.Status404NotFound;
54-
context.Response.ContentType = MediaTypeNames.Text.Html;
55-
return Task.CompletedTask;
56-
});
57-
return;
55+
app.MapFallback(context =>
56+
{
57+
context.Response.StatusCode = StatusCodes.Status404NotFound;
58+
context.Response.ContentType = MediaTypeNames.Text.Html;
59+
return Task.CompletedTask;
60+
});
61+
return;
62+
}
63+
64+
IndexTemplate = File.ReadAllText(index.PhysicalPath);
65+
app.MapFallback(IndexHandler);
5866
}
59-
60-
IndexTemplate = File.ReadAllText(index.PhysicalPath);
61-
app.MapFallback(IndexHandler);
6267
}
6368

6469
static string GetETag(StringSegment hash) => $"\"favicon-{hash}\"";

src/GZCTF/Extensions/OtherExtensions.cs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,65 @@ namespace GZCTF.Extensions;
55

66
public static class ListExtensions
77
{
8-
public static int GetSetHashCode<T>(this IList<T> list) =>
9-
list.Count + list.Distinct().Aggregate(0, (x, y) => x.GetHashCode() ^ y?.GetHashCode() ?? 0xdead);
8+
extension<T>(IList<T> list)
9+
{
10+
public int GetSetHashCode() =>
11+
list.Count + list.Distinct().Aggregate(0, (x, y) => x.GetHashCode() ^ y?.GetHashCode() ?? 0xdead);
12+
}
1013
}
1114

1215
public static class QueryableExtensions
1316
{
14-
/// <summary>
15-
/// Take part of the data if count is greater than 0, otherwise take all data
16-
/// Warn: Injections may occur if the count is not validated
17-
/// </summary>
18-
/// <returns></returns>
19-
public static IQueryable<T> TakeAllIfZero<T>(this IQueryable<T> items, int count = 100, int skip = 0) =>
20-
count switch
21-
{
22-
> 0 => items.Skip(skip).Take(count),
23-
_ => items
24-
};
17+
extension<T>(IQueryable<T> items)
18+
{
19+
/// <summary>
20+
/// Take part of the data if count is greater than 0, otherwise take all data
21+
/// Warn: Injections may occur if the count is not validated
22+
/// </summary>
23+
/// <returns></returns>
24+
public IQueryable<T> TakeAllIfZero(int count = 100, int skip = 0) =>
25+
count switch
26+
{
27+
> 0 => items.Skip(skip).Take(count),
28+
_ => items
29+
};
30+
}
2531
}
2632

2733
public static class ArrayExtensions
2834
{
29-
public static ArrayResponse<T> ToResponse<T>(this IEnumerable<T> array, int? tot = null) where T : class =>
30-
array switch
31-
{
32-
null => new([]),
33-
T[] arr => new(arr, tot),
34-
_ => new(array.ToArray(), tot)
35-
};
35+
extension<T>(IEnumerable<T> array) where T : class
36+
{
37+
public ArrayResponse<T> ToResponse(int? tot = null) =>
38+
array switch
39+
{
40+
null => new([]),
41+
T[] arr => new(arr, tot),
42+
_ => new(array.ToArray(), tot)
43+
};
44+
}
3645
}
3746

3847
public static class IPAddressExtensions
3948
{
40-
public static IEnumerable<IPAddress> ResolveIP(this string? host) =>
41-
!string.IsNullOrWhiteSpace(host)
42-
? Dns.GetHostAddresses(host)
43-
: [];
49+
extension(string? host)
50+
{
51+
public IEnumerable<IPAddress> ResolveIP() =>
52+
!string.IsNullOrWhiteSpace(host)
53+
? Dns.GetHostAddresses(host)
54+
: [];
55+
}
4456
}
4557

4658
internal static class JsonSerializerOptionsExtensions
4759
{
48-
public static void ConfigCustomSerializerOptions(this JsonSerializerOptions options)
60+
extension(JsonSerializerOptions options)
4961
{
50-
options.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
51-
options.Converters.Add(new DateTimeOffsetJsonConverter());
52-
options.Converters.Add(new IPAddressJsonConverter());
62+
public void ConfigCustomSerializerOptions()
63+
{
64+
options.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
65+
options.Converters.Add(new DateTimeOffsetJsonConverter());
66+
options.Converters.Add(new IPAddressJsonConverter());
67+
}
5368
}
5469
}

src/GZCTF/Extensions/SignalRSinkExtension.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ namespace GZCTF.Extensions;
1212

1313
public static class SignalRSinkExtension
1414
{
15-
public static LoggerConfiguration SignalR(this LoggerSinkConfiguration loggerConfiguration,
16-
IServiceProvider serviceProvider) =>
17-
loggerConfiguration.Sink(new SignalRSink(serviceProvider), LogEventLevel.Information);
15+
extension(LoggerSinkConfiguration loggerConfiguration)
16+
{
17+
public LoggerConfiguration SignalR(IServiceProvider serviceProvider) =>
18+
loggerConfiguration.Sink(new SignalRSink(serviceProvider), LogEventLevel.Information);
19+
}
1820
}
1921

2022
public class SignalRSink(IServiceProvider serviceProvider) : ILogEventSink

src/GZCTF/Extensions/Startup/AppBuilderExtensions.cs

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,77 @@ namespace GZCTF.Extensions.Startup;
55

66
static class AppBuilderExtensions
77
{
8-
internal static void ConfigureWebHost(this WebApplicationBuilder builder)
8+
extension(WebApplicationBuilder builder)
99
{
10-
builder.Services.ConfigureHttpJsonOptions(options =>
10+
internal void ConfigureWebHost()
1111
{
12-
options.SerializerOptions.ConfigCustomSerializerOptions();
13-
});
14-
15-
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources")
16-
.Configure<RequestLocalizationOptions>(options =>
12+
builder.Services.ConfigureHttpJsonOptions(options =>
1713
{
18-
options
19-
.AddSupportedCultures(SupportedCultures)
20-
.AddSupportedUICultures(SupportedCultures);
21-
22-
options.ApplyCurrentCultureToResponseHeaders = true;
14+
options.SerializerOptions.ConfigCustomSerializerOptions();
2315
});
2416

25-
builder.WebHost.ConfigureKestrel(options =>
26-
{
27-
var kestrelSection = builder.Configuration.GetSection("Kestrel");
28-
options.Configure(kestrelSection);
29-
kestrelSection.Bind(options);
30-
}).UseKestrel(options =>
31-
{
32-
options.ListenAnyIP(ServerPort);
33-
options.ListenAnyIP(MetricPort);
34-
});
17+
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources")
18+
.Configure<RequestLocalizationOptions>(options =>
19+
{
20+
options
21+
.AddSupportedCultures(SupportedCultures)
22+
.AddSupportedUICultures(SupportedCultures);
3523

36-
builder.Logging.ClearProviders();
37-
builder.Logging.SetMinimumLevel(LogLevel.Trace);
38-
builder.Logging.AddSerilog(dispose: true);
39-
builder.Host.UseSerilog(dispose: true);
40-
builder.Configuration.AddEnvironmentVariables("GZCTF_");
24+
options.ApplyCurrentCultureToResponseHeaders = true;
25+
});
4126

42-
builder.Services.AddServiceDiscovery();
43-
builder.Services.ConfigureHttpClientDefaults(http =>
44-
{
45-
http.AddStandardResilienceHandler();
46-
http.AddServiceDiscovery();
47-
});
48-
}
49-
50-
internal static void ConfigureCacheAndSignalR(this WebApplicationBuilder builder)
51-
{
52-
var signalrBuilder = builder.Services.AddSignalR().AddJsonProtocol(options =>
53-
{
54-
options.PayloadSerializerOptions.ConfigCustomSerializerOptions();
55-
});
27+
builder.WebHost.ConfigureKestrel(options =>
28+
{
29+
var kestrelSection = builder.Configuration.GetSection("Kestrel");
30+
options.Configure(kestrelSection);
31+
kestrelSection.Bind(options);
32+
}).UseKestrel(options =>
33+
{
34+
options.ListenAnyIP(ServerPort);
35+
options.ListenAnyIP(MetricPort);
36+
});
5637

57-
var connectionString = builder.Configuration.GetConnectionString("RedisCache");
38+
builder.Logging.ClearProviders();
39+
builder.Logging.SetMinimumLevel(LogLevel.Trace);
40+
builder.Logging.AddSerilog(dispose: true);
41+
builder.Host.UseSerilog(dispose: true);
42+
builder.Configuration.AddEnvironmentVariables("GZCTF_");
5843

59-
if (string.IsNullOrWhiteSpace(connectionString))
60-
{
61-
builder.Services.AddDistributedMemoryCache();
44+
builder.Services.AddServiceDiscovery();
45+
builder.Services.ConfigureHttpClientDefaults(http =>
46+
{
47+
http.AddStandardResilienceHandler();
48+
http.AddServiceDiscovery();
49+
});
6250
}
63-
else
51+
52+
internal void ConfigureCacheAndSignalR()
6453
{
65-
builder.Services.AddStackExchangeRedisCache(options =>
54+
var signalrBuilder = builder.Services.AddSignalR().AddJsonProtocol(options =>
6655
{
67-
options.Configuration = connectionString;
56+
options.PayloadSerializerOptions.ConfigCustomSerializerOptions();
6857
});
6958

70-
signalrBuilder.AddStackExchangeRedis(connectionString, options =>
59+
var connectionString = builder.Configuration.GetConnectionString("RedisCache");
60+
61+
if (string.IsNullOrWhiteSpace(connectionString))
7162
{
72-
options.Configuration.ChannelPrefix = new RedisChannel("GZCTF", RedisChannel.PatternMode.Literal);
73-
});
74-
}
63+
builder.Services.AddDistributedMemoryCache();
64+
}
65+
else
66+
{
67+
builder.Services.AddStackExchangeRedisCache(options =>
68+
{
69+
options.Configuration = connectionString;
70+
});
7571

76-
builder.Services.AddMemoryCache();
72+
signalrBuilder.AddStackExchangeRedis(connectionString, options =>
73+
{
74+
options.Configuration.ChannelPrefix = new RedisChannel("GZCTF", RedisChannel.PatternMode.Literal);
75+
});
76+
}
77+
78+
builder.Services.AddMemoryCache();
79+
}
7780
}
7881
}

0 commit comments

Comments
 (0)