Files
social-media/backend/Program.cs

141 lines
4.3 KiB
C#

using Azure.Identity;
using Hutopy;
using Hutopy.Infrastructure;
using Hutopy.Modules.Contents;
using Hutopy.Modules.Contents.Data;
using Hutopy.Modules.Creators;
using Hutopy.Modules.Creators.Data;
using Hutopy.Modules.Identity;
using Hutopy.Modules.Identity.Data;
using Hutopy.Modules.Memberships;
using Hutopy.Modules.Memberships.Data;
using Hutopy.Modules.Messaging;
using Hutopy.Modules.Messaging.Data;
using Hutopy.Modules.Tipping;
using Hutopy.Modules.Tipping.Data;
using Microsoft.AspNetCore.HttpOverrides;
using NSwag;
using NSwag.Generation.AspNetCore.Processors;
using NSwag.Generation.Processors.Security;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
if (!builder.Environment.IsDevelopment())
{
string? vaultUri = Environment.GetEnvironmentVariable("VaultUri");
if (vaultUri is null)
{
throw new InvalidOperationException("Missing VaultUri configuration setting");
}
builder.Configuration.AddAzureKeyVault(new Uri(vaultUri), new DefaultAzureCredential());
}
builder.Services.AddCors(options =>
{
options.AddPolicy(
"AllowAll",
policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
// Add services to the container.
builder.Services.AddWebServices();
builder.Services.AddAuthorizationAndAuthentication(builder.Configuration);
builder.Services.AddOpenApiDocument((
configure,
_) =>
{
configure.Title = "Hutopy API";
// Add JWT
configure.AddSecurity(
"JWT",
[],
new OpenApiSecurityScheme
{
Type = OpenApiSecuritySchemeType.ApiKey,
Name = "Authorization",
In = OpenApiSecurityApiKeyLocation.Header,
Description = "Type into the textbox: Bearer {your JWT token}."
});
configure.OperationProcessors.Add(new AspNetCoreOperationTagsProcessor());
configure.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT"));
});
string postgresConnectionString = builder.Configuration.GetConnectionString("PostgresConnection")
?? throw new InvalidOperationException(
"Missing ConnectionStrings:PostgresConnection");
builder.Services.AddFastEndpoints();
builder.AddInfrastructureModule();
builder.AddIdentityModule(options =>
options.UseNpgsql(
postgresConnectionString,
o => o.MigrationsHistoryTable("__EFMigrationsHistory", IdentityDbContext.SchemaName)));
builder.AddCreatorModule(options =>
options.UseNpgsql(
postgresConnectionString,
o => o.MigrationsHistoryTable("__EFMigrationsHistory", CreatorsDbContext.SchemaName)));
builder.AddContentModule(options =>
options.UseNpgsql(
postgresConnectionString,
o => o.MigrationsHistoryTable("__EFMigrationsHistory", ContentsDbContext.SchemaName)));
builder.AddMembershipModule(options => options.UseNpgsql(
postgresConnectionString,
o => o.MigrationsHistoryTable("__EFMigrationsHistory", MembershipsDbContext.SchemaName)));
builder.AddTippingModule(options =>
options.UseNpgsql(
postgresConnectionString,
o => o.MigrationsHistoryTable("__EFMigrationsHistory", TippingDbContext.SchemaName)));
builder.AddMessagingModule(options =>
options.UseNpgsql(
postgresConnectionString,
o => o.MigrationsHistoryTable("__EFMigrationsHistory", MessagingDbContext.SchemaName)));
WebApplication app = builder.Build();
app.UseForwardedHeaders(
new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedProto }
);
app.UseCors("AllowAll");
app.UseAuthentication();
app.UseAuthorization();
// Initialize and seed the db.
await app.UseIdentityModuleAsync();
await app.UseCreatorModuleAsync();
await app.UseContentModuleAsync();
await app.UseMembershipModuleAsync();
await app.UseTippingModuleAsync();
await app.UseMessagingModuleAsync();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHealthChecks("/health");
app.UseHttpsRedirection();
if (app.Environment.IsDevelopment())
{
app.UseOpenApi();
app.UseSwaggerUi(options => options.Path = "/api");
}
app.UseFastEndpoints();
app.Run();