chore: add csharpier to husky, format backend with csharpier
This commit is contained in:
parent
5fab66444f
commit
7f971e8549
73 changed files with 2098 additions and 1048 deletions
|
@ -14,21 +14,35 @@ public static class AvatarObjectExtensions
|
|||
{
|
||||
private static readonly string[] ValidContentTypes = ["image/png", "image/webp", "image/jpeg"];
|
||||
|
||||
public static async Task
|
||||
DeleteMemberAvatarAsync(this ObjectStorageService objectStorageService, Snowflake id, string hash,
|
||||
CancellationToken ct = default) =>
|
||||
await objectStorageService.RemoveObjectAsync(MemberAvatarUpdateInvocable.Path(id, hash), ct);
|
||||
public static async Task DeleteMemberAvatarAsync(
|
||||
this ObjectStorageService objectStorageService,
|
||||
Snowflake id,
|
||||
string hash,
|
||||
CancellationToken ct = default
|
||||
) =>
|
||||
await objectStorageService.RemoveObjectAsync(
|
||||
MemberAvatarUpdateInvocable.Path(id, hash),
|
||||
ct
|
||||
);
|
||||
|
||||
public static async Task
|
||||
DeleteUserAvatarAsync(this ObjectStorageService objectStorageService, Snowflake id, string hash,
|
||||
CancellationToken ct = default) =>
|
||||
await objectStorageService.RemoveObjectAsync(UserAvatarUpdateInvocable.Path(id, hash), ct);
|
||||
public static async Task DeleteUserAvatarAsync(
|
||||
this ObjectStorageService objectStorageService,
|
||||
Snowflake id,
|
||||
string hash,
|
||||
CancellationToken ct = default
|
||||
) => await objectStorageService.RemoveObjectAsync(UserAvatarUpdateInvocable.Path(id, hash), ct);
|
||||
|
||||
public static async Task DeleteFlagAsync(this ObjectStorageService objectStorageService, string hash,
|
||||
CancellationToken ct = default) =>
|
||||
await objectStorageService.RemoveObjectAsync(CreateFlagInvocable.Path(hash), ct);
|
||||
public static async Task DeleteFlagAsync(
|
||||
this ObjectStorageService objectStorageService,
|
||||
string hash,
|
||||
CancellationToken ct = default
|
||||
) => await objectStorageService.RemoveObjectAsync(CreateFlagInvocable.Path(hash), ct);
|
||||
|
||||
public static async Task<(string Hash, Stream Image)> ConvertBase64UriToImage(this string uri, int size, bool crop)
|
||||
public static async Task<(string Hash, Stream Image)> ConvertBase64UriToImage(
|
||||
this string uri,
|
||||
int size,
|
||||
bool crop
|
||||
)
|
||||
{
|
||||
if (!uri.StartsWith("data:image/"))
|
||||
throw new ArgumentException("Not a data URI", nameof(uri));
|
||||
|
@ -49,7 +63,7 @@ public static class AvatarObjectExtensions
|
|||
{
|
||||
Size = new Size(size),
|
||||
Mode = crop ? ResizeMode.Crop : ResizeMode.Max,
|
||||
Position = AnchorPositionMode.Center
|
||||
Position = AnchorPositionMode.Center,
|
||||
},
|
||||
image.Size
|
||||
);
|
||||
|
@ -65,4 +79,4 @@ public static class AvatarObjectExtensions
|
|||
|
||||
return (hash, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,37 +8,58 @@ namespace Foxnouns.Backend.Extensions;
|
|||
|
||||
public static class KeyCacheExtensions
|
||||
{
|
||||
public static async Task<string> GenerateAuthStateAsync(this KeyCacheService keyCacheService,
|
||||
CancellationToken ct = default)
|
||||
public static async Task<string> GenerateAuthStateAsync(
|
||||
this KeyCacheService keyCacheService,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
var state = AuthUtils.RandomToken().Replace('+', '-').Replace('/', '_');
|
||||
await keyCacheService.SetKeyAsync($"oauth_state:{state}", "", Duration.FromMinutes(10), ct);
|
||||
return state;
|
||||
}
|
||||
|
||||
public static async Task ValidateAuthStateAsync(this KeyCacheService keyCacheService, string state,
|
||||
CancellationToken ct = default)
|
||||
public static async Task ValidateAuthStateAsync(
|
||||
this KeyCacheService keyCacheService,
|
||||
string state,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
var val = await keyCacheService.GetKeyAsync($"oauth_state:{state}", delete: true, ct);
|
||||
if (val == null) throw new ApiError.BadRequest("Invalid OAuth state");
|
||||
if (val == null)
|
||||
throw new ApiError.BadRequest("Invalid OAuth state");
|
||||
}
|
||||
|
||||
public static async Task<string> GenerateRegisterEmailStateAsync(this KeyCacheService keyCacheService, string email,
|
||||
Snowflake? userId = null, CancellationToken ct = default)
|
||||
public static async Task<string> GenerateRegisterEmailStateAsync(
|
||||
this KeyCacheService keyCacheService,
|
||||
string email,
|
||||
Snowflake? userId = null,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
// This state is used in links, not just as JSON values, so make it URL-safe
|
||||
var state = AuthUtils.RandomToken().Replace('+', '-').Replace('/', '_');
|
||||
await keyCacheService.SetKeyAsync($"email_state:{state}", new RegisterEmailState(email, userId),
|
||||
Duration.FromDays(1), ct);
|
||||
await keyCacheService.SetKeyAsync(
|
||||
$"email_state:{state}",
|
||||
new RegisterEmailState(email, userId),
|
||||
Duration.FromDays(1),
|
||||
ct
|
||||
);
|
||||
return state;
|
||||
}
|
||||
|
||||
public static async Task<RegisterEmailState?> GetRegisterEmailStateAsync(this KeyCacheService keyCacheService,
|
||||
string state, CancellationToken ct = default) =>
|
||||
await keyCacheService.GetKeyAsync<RegisterEmailState>($"email_state:{state}", delete: true, ct);
|
||||
public static async Task<RegisterEmailState?> GetRegisterEmailStateAsync(
|
||||
this KeyCacheService keyCacheService,
|
||||
string state,
|
||||
CancellationToken ct = default
|
||||
) =>
|
||||
await keyCacheService.GetKeyAsync<RegisterEmailState>(
|
||||
$"email_state:{state}",
|
||||
delete: true,
|
||||
ct
|
||||
);
|
||||
}
|
||||
|
||||
public record RegisterEmailState(
|
||||
string Email,
|
||||
[property: JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
Snowflake? ExistingUserId);
|
||||
[property: JsonProperty(NullValueHandling = NullValueHandling.Ignore)] Snowflake? ExistingUserId
|
||||
);
|
||||
|
|
|
@ -29,8 +29,10 @@ public static class WebApplicationExtensions
|
|||
// ASP.NET's built in request logs are extremely verbose, so we use Serilog's instead.
|
||||
// Serilog doesn't disable the built-in logs, so we do it here.
|
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||||
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command",
|
||||
config.Logging.LogQueries ? LogEventLevel.Information : LogEventLevel.Fatal)
|
||||
.MinimumLevel.Override(
|
||||
"Microsoft.EntityFrameworkCore.Database.Command",
|
||||
config.Logging.LogQueries ? LogEventLevel.Information : LogEventLevel.Fatal
|
||||
)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore.Routing", LogEventLevel.Warning)
|
||||
|
@ -38,7 +40,10 @@ public static class WebApplicationExtensions
|
|||
|
||||
if (config.Logging.SeqLogUrl != null)
|
||||
{
|
||||
logCfg.WriteTo.Seq(config.Logging.SeqLogUrl, restrictedToMinimumLevel: LogEventLevel.Verbose);
|
||||
logCfg.WriteTo.Seq(
|
||||
config.Logging.SeqLogUrl,
|
||||
restrictedToMinimumLevel: LogEventLevel.Verbose
|
||||
);
|
||||
}
|
||||
|
||||
// AddSerilog doesn't seem to add an ILogger to the service collection, so add that manually.
|
||||
|
@ -74,63 +79,74 @@ public static class WebApplicationExtensions
|
|||
/// </summary>
|
||||
public static IServiceCollection AddServices(this WebApplicationBuilder builder, Config config)
|
||||
{
|
||||
builder.Host.ConfigureServices((ctx, services) =>
|
||||
{
|
||||
services
|
||||
.AddQueue()
|
||||
.AddSmtpMailer(ctx.Configuration)
|
||||
.AddDbContext<DatabaseContext>()
|
||||
.AddMetricServer(o => o.Port = config.Logging.MetricsPort)
|
||||
.AddMinio(c =>
|
||||
c.WithEndpoint(config.Storage.Endpoint)
|
||||
.WithCredentials(config.Storage.AccessKey, config.Storage.SecretKey)
|
||||
.Build())
|
||||
.AddSingleton<MetricsCollectionService>()
|
||||
.AddSingleton<IClock>(SystemClock.Instance)
|
||||
.AddSnowflakeGenerator()
|
||||
.AddSingleton<MailService>()
|
||||
.AddScoped<UserRendererService>()
|
||||
.AddScoped<MemberRendererService>()
|
||||
.AddScoped<AuthService>()
|
||||
.AddScoped<KeyCacheService>()
|
||||
.AddScoped<RemoteAuthService>()
|
||||
.AddScoped<ObjectStorageService>()
|
||||
// Background services
|
||||
.AddHostedService<PeriodicTasksService>()
|
||||
// Transient jobs
|
||||
.AddTransient<MemberAvatarUpdateInvocable>()
|
||||
.AddTransient<UserAvatarUpdateInvocable>()
|
||||
.AddTransient<CreateFlagInvocable>();
|
||||
builder.Host.ConfigureServices(
|
||||
(ctx, services) =>
|
||||
{
|
||||
services
|
||||
.AddQueue()
|
||||
.AddSmtpMailer(ctx.Configuration)
|
||||
.AddDbContext<DatabaseContext>()
|
||||
.AddMetricServer(o => o.Port = config.Logging.MetricsPort)
|
||||
.AddMinio(c =>
|
||||
c.WithEndpoint(config.Storage.Endpoint)
|
||||
.WithCredentials(config.Storage.AccessKey, config.Storage.SecretKey)
|
||||
.Build()
|
||||
)
|
||||
.AddSingleton<MetricsCollectionService>()
|
||||
.AddSingleton<IClock>(SystemClock.Instance)
|
||||
.AddSnowflakeGenerator()
|
||||
.AddSingleton<MailService>()
|
||||
.AddScoped<UserRendererService>()
|
||||
.AddScoped<MemberRendererService>()
|
||||
.AddScoped<AuthService>()
|
||||
.AddScoped<KeyCacheService>()
|
||||
.AddScoped<RemoteAuthService>()
|
||||
.AddScoped<ObjectStorageService>()
|
||||
// Background services
|
||||
.AddHostedService<PeriodicTasksService>()
|
||||
// Transient jobs
|
||||
.AddTransient<MemberAvatarUpdateInvocable>()
|
||||
.AddTransient<UserAvatarUpdateInvocable>()
|
||||
.AddTransient<CreateFlagInvocable>();
|
||||
|
||||
if (!config.Logging.EnableMetrics)
|
||||
services.AddHostedService<BackgroundMetricsCollectionService>();
|
||||
});
|
||||
if (!config.Logging.EnableMetrics)
|
||||
services.AddHostedService<BackgroundMetricsCollectionService>();
|
||||
}
|
||||
);
|
||||
|
||||
return builder.Services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddCustomMiddleware(this IServiceCollection services) => services
|
||||
.AddScoped<ErrorHandlerMiddleware>()
|
||||
.AddScoped<AuthenticationMiddleware>()
|
||||
.AddScoped<AuthorizationMiddleware>();
|
||||
public static IServiceCollection AddCustomMiddleware(this IServiceCollection services) =>
|
||||
services
|
||||
.AddScoped<ErrorHandlerMiddleware>()
|
||||
.AddScoped<AuthenticationMiddleware>()
|
||||
.AddScoped<AuthorizationMiddleware>();
|
||||
|
||||
public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder app) => app
|
||||
.UseMiddleware<ErrorHandlerMiddleware>()
|
||||
.UseMiddleware<AuthenticationMiddleware>()
|
||||
.UseMiddleware<AuthorizationMiddleware>();
|
||||
public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder app) =>
|
||||
app.UseMiddleware<ErrorHandlerMiddleware>()
|
||||
.UseMiddleware<AuthenticationMiddleware>()
|
||||
.UseMiddleware<AuthorizationMiddleware>();
|
||||
|
||||
public static async Task Initialize(this WebApplication app, string[] args)
|
||||
{
|
||||
// Read version information from .version in the repository root
|
||||
await BuildInfo.ReadBuildInfo();
|
||||
|
||||
app.Services.ConfigureQueue().LogQueuedTaskProgress(app.Services.GetRequiredService<ILogger<IQueue>>());
|
||||
app.Services.ConfigureQueue()
|
||||
.LogQueuedTaskProgress(app.Services.GetRequiredService<ILogger<IQueue>>());
|
||||
|
||||
await using var scope = app.Services.CreateAsyncScope();
|
||||
var logger = scope.ServiceProvider.GetRequiredService<ILogger>().ForContext<WebApplication>();
|
||||
var logger = scope
|
||||
.ServiceProvider.GetRequiredService<ILogger>()
|
||||
.ForContext<WebApplication>();
|
||||
var db = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||
|
||||
logger.Information("Starting Foxnouns.NET {Version} ({Hash})", BuildInfo.Version, BuildInfo.Hash);
|
||||
logger.Information(
|
||||
"Starting Foxnouns.NET {Version} ({Hash})",
|
||||
BuildInfo.Version,
|
||||
BuildInfo.Hash
|
||||
);
|
||||
|
||||
var pendingMigrations = (await db.Database.GetPendingMigrationsAsync()).ToList();
|
||||
if (args.Contains("--migrate") || args.Contains("--migrate-and-start"))
|
||||
|
@ -146,17 +162,19 @@ public static class WebApplicationExtensions
|
|||
logger.Information("Successfully migrated database");
|
||||
}
|
||||
|
||||
if (!args.Contains("--migrate-and-start")) Environment.Exit(0);
|
||||
if (!args.Contains("--migrate-and-start"))
|
||||
Environment.Exit(0);
|
||||
}
|
||||
else if (pendingMigrations.Count > 0)
|
||||
{
|
||||
logger.Fatal(
|
||||
"There are {Count} pending migrations, run server with --migrate or --migrate-and-start to run migrations.",
|
||||
pendingMigrations.Count);
|
||||
pendingMigrations.Count
|
||||
);
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
||||
logger.Information("Initializing frontend OAuth application");
|
||||
_ = await db.GetFrontendApplicationAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue