Files
social-media/src/Web/Program.cs
Jonathan Bourdon adb1cebc73 WIP
2024-07-01 23:34:23 -04:00

137 lines
3.6 KiB
C#

using Azure.Identity;
using FastEndpoints;
using Hutopy.Application;
using Hutopy.Infrastructure;
using Hutopy.Infrastructure.Data;
using Hutopy.Web;
using Hutopy.Web.Messages.Data;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.EntityFrameworkCore;
using NSwag;
using NSwag.Generation.AspNetCore.Processors;
using NSwag.Generation.Processors.Security;
var builder = WebApplication.CreateBuilder(args);
if (!builder.Environment.IsDevelopment())
{
var keyVaultEndpoint = new Uri(Environment.GetEnvironmentVariable("VaultUri") ?? "");
builder.Configuration.AddAzureKeyVault(keyVaultEndpoint, new DefaultAzureCredential());
}
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
options.AddPolicy("AllowHutopyUi", policy =>
{
policy.WithOrigins("https://zealous-bay-08204590f.5.azurestaticapps.net")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
options.AddPolicy("AllowHutopyUiPreview", policy =>
{
policy.WithOrigins("https://zealous-bay-08204590f-preview.eastus2.5.azurestaticapps.net")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
// Add services to the container.
builder.Services.AddKeyVaultIfConfigured(builder.Configuration);
builder.Services.AddApplicationServices();
builder.Services.AddInfrastructureServices(builder.Configuration);
builder.Services.AddWebServices();
builder.Services.AddAuthorizationAndAuthentication(builder.Configuration);
// TODO: This old tech should be remove - need to move Facebook / Google controllers to FastEndpoints
builder.Services.AddControllers();
builder.Services.AddOpenApiDocument((configure, sp) =>
{
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"));
});
builder.Services.AddFastEndpoints();
builder.Services.AddDbContext<MessagingDbContext>((_, options) =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("CommentStore"));
});
var app = builder.Build();
app.UseForwardedHeaders(
new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedProto }
);
app.UseCors("AllowAll");
app.UseCors("AllowHutopyUi");
app.UseCors("AllowHutopyUiPreview");
app.UseAuthentication();
app.UseAuthorization();
// Initialize and seed the db.
await app.InitialiseDatabaseAsync();
// 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();
app.UseStaticFiles();
if (app.Environment.IsDevelopment())
{
app.UseOpenApi();
app.UseSwaggerUi(options => options.Path = "/api");
}
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
//TODO: validate the behavior
// app.UseExceptionHandler();
app.MapEndpoints();
app.UseFastEndpoints();
app.Run();
namespace Hutopy.Web
{
public abstract partial class Program
{
}
}