Files
Shadow/Program.cs
sherl f648a73cb2
All checks were successful
Update changelog / changelog (push) Successful in 25s
feat: add database connections, initial migration and early CLI tooling
2025-12-09 01:46:05 +01:00

107 lines
3.1 KiB
C#

using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi;
using Shadow.Controllers;
using Shadow.Data;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
string connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new ArgumentException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseNpgsql(connectionString);
});
builder.Services.AddControllers(options =>
{
// Add XML serializer
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
}).AddJsonOptions(options =>
{
// Pretty-print JSON
options.JsonSerializerOptions.WriteIndented = true;
// Preserve keys' case
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
builder.Services.AddOpenApi();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddScoped<GeneralUseHelpers>();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Shadow API",
Description = "An ASP.NET Core Web music API compatible with a subset of Subsonic endpoints",
Contact = new OpenApiContact
{
Name = "Shadow's production branch",
Url = new Uri("https://shadow.7o7.cx")
},
License = new OpenApiLicense
{
Name = "AGPLv3",
Url = new Uri("https://www.gnu.org/licenses/agpl-3.0.en.html")
}
});
//// https://stackoverflow.com/a/58972781
//options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
//{
// Description = @"JWT Authorization header using the Bearer scheme. <br/>
// Enter your JWT from /api/v1/auth/login to authorize.",
// Name = "Authorization",
// In = ParameterLocation.Header,
// Type = SecuritySchemeType.Http,
// Scheme = "Bearer"
//});
//options.AddSecurityRequirement((document) => new OpenApiSecurityRequirement()
//{
// [new OpenApiSecuritySchemeReference("bearer", document)] = []
//});
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
var app = builder.Build();
if (args.FirstOrDefault() is not null)
{
// Handle CLI if arguments have been passed.
bool shutdown = false;
using (IServiceScope scope = app.Services.CreateScope())
{
ApplicationDbContext db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
GeneralUseHelpers guhf = scope.ServiceProvider.GetRequiredService<GeneralUseHelpers>();
Cli cli = new(db, guhf, args);
shutdown = await cli.Parse();
}
if (shutdown) return;
}
// Seed database
using (IServiceScope scope = app.Services.CreateScope())
{
ApplicationDbContext db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
GeneralUseHelpers guhf = scope.ServiceProvider.GetRequiredService<GeneralUseHelpers>();
Seeder seeder = new(db, guhf);
seeder.Seed();
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();