Files
Shadow/Program.cs
sherl 3486b82879 fix: temporarily disable xml serialization
in it's current state xml endpoints are not supported, as no known good
way to serialize it exists
2026-01-28 05:13:42 +01:00

115 lines
3.4 KiB
C#

using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi;
using Shadow.Data;
using Shadow.Tools;
using System.Reflection;
using System.Xml;
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.RespectBrowserAcceptHeader = true;
// options.OutputFormatters.Add(new XmlSerializerOutputFormatter(
// new XmlWriterSettings { OmitXmlDeclaration = true }
// ));
}).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));
});
builder.Services.AddHttpLogging();
var app = builder.Build();
bool shutdown = false;
bool scanNeeded = false;
if (args.FirstOrDefault() is not null)
{
// Handle CLI if arguments have been passed.
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 = 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);
shutdown = seeder.Seed();
if (!shutdown) scanNeeded = await seeder.ScanPrefetchAsync();
}
if (shutdown) return;
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpLogging();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();