feat: add database connections, initial migration and early CLI tooling
All checks were successful
Update changelog / changelog (push) Successful in 25s
All checks were successful
Update changelog / changelog (push) Successful in 25s
This commit is contained in:
49
Program.cs
49
Program.cs
@@ -1,12 +1,34 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
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);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
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
|
||||
@@ -41,12 +63,33 @@ builder.Services.AddSwaggerGen(options =>
|
||||
// [new OpenApiSecuritySchemeReference("bearer", document)] = []
|
||||
//});
|
||||
|
||||
// using System.Reflection;
|
||||
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())
|
||||
|
||||
Reference in New Issue
Block a user