2024-12-31 18:40:08 -05:00
using System.Reflection ;
2025-01-01 09:36:28 -05:00
using Microsoft.AspNetCore.Components.QuickGrid.EntityFrameworkAdapter ;
using Microsoft.EntityFrameworkCore ;
using Microsoft.Extensions.DependencyInjection ;
2024-12-31 18:40:08 -05:00
using Microsoft.OpenApi.Models ;
2024-12-31 01:01:16 -05:00
using ZenithInfo.Components ;
2024-12-31 18:31:47 -08:00
using ZenithInfo.Data ;
2024-12-31 19:01:25 -05:00
using ZenithInfo.Models ;
2025-01-01 09:25:21 -05:00
using ZenithInfo.Util.SwaggerTheme ;
2024-12-31 01:01:16 -05:00
var builder = WebApplication . CreateBuilder ( args ) ;
2024-12-31 19:01:25 -05:00
// Configuration
var config = builder . Configuration . GetSection ( "Configuration" ) . Get < ZIConfiguration > ( ) ;
builder . Services . Configure < ZIConfiguration > ( builder . Configuration . GetSection ( "Configuration" ) ) ;
if ( config ? . GlobalToken = = "CHANGE-ME" )
{
throw new InvalidOperationException (
"Global token is not a valid value, please set the `CONFIGURATION__GLOBALTOKEN` environment variable to a random value. `openssl rand -hex 64` is a good way to generate a random value."
) ;
}
2024-12-31 18:31:47 -08:00
// Initialize Database
builder . Services . AddQuickGridEntityFrameworkAdapter ( ) ;
builder . Services . AddDbContextFactory < ZenithInfoContext > ( options = >
2025-01-01 09:36:28 -05:00
options . UseNpgsql ( builder . Configuration . GetConnectionString ( "ZenithInfoContext" ) )
) ;
2024-12-31 18:31:47 -08:00
2024-12-31 19:02:22 -05:00
// Sentry
if ( config ? . UseSentry = = true )
{
builder . WebHost . UseSentry ( ) ;
}
2024-12-31 01:01:16 -05:00
// Add services to the container.
2024-12-31 01:03:35 -05:00
builder . Services . AddRazorComponents ( ) . AddInteractiveServerComponents ( ) ;
2024-12-31 18:40:08 -05:00
builder . Services . AddHttpLogging ( o = > { } ) ;
builder . Services . AddEndpointsApiExplorer ( ) ;
builder . Services . AddSwaggerGen ( opts = >
{
2025-01-01 09:25:21 -05:00
opts . SwaggerDoc (
"v1" ,
new OpenApiInfo
{
Title = "Zenith Info API" ,
Version = "v1" ,
Description = "Zenith's official information provider!" ,
}
) ;
2024-12-31 18:40:08 -05:00
opts . AddSecurityDefinition (
"Bearer" ,
new OpenApiSecurityScheme
{
Description =
"JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"" ,
Name = "Authorization" ,
In = ParameterLocation . Header ,
Type = SecuritySchemeType . ApiKey ,
Scheme = "Bearer" ,
}
) ;
opts . AddSecurityRequirement (
new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType . SecurityScheme ,
Id = "Bearer" ,
} ,
Scheme = "oauth2" ,
Name = "Bearer" ,
In = ParameterLocation . Header ,
} ,
new List < string > ( )
} ,
}
) ;
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml" ;
opts . IncludeXmlComments ( Path . Combine ( AppContext . BaseDirectory , xmlFile ) ) ;
} ) ;
2024-12-31 01:01:16 -05:00
var app = builder . Build ( ) ;
2025-01-01 09:25:21 -05:00
var assembly = Assembly . GetExecutingAssembly ( ) ;
foreach ( var resource in assembly . GetManifestResourceNames ( ) )
{
app . Logger . LogDebug ( "Resource: {Resource}" , resource ) ;
}
2024-12-31 19:01:25 -05:00
if ( config ? . UseSwagger = = true )
{
app . UseSwagger ( ) ;
2025-01-01 09:25:21 -05:00
app . UseSwaggerUI ( CustomStyle . CustomModern ) ;
2024-12-31 19:01:25 -05:00
}
if ( config ? . LogRequests = = true )
{
app . UseHttpLogging ( ) ;
}
2024-12-31 18:40:08 -05:00
2024-12-31 01:01:16 -05:00
// Configure the HTTP request pipeline.
if ( ! app . Environment . IsDevelopment ( ) )
{
app . UseExceptionHandler ( "/Error" , createScopeForErrors : true ) ;
// 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 . UseHttpsRedirection ( ) ;
app . UseStaticFiles ( ) ;
app . UseAntiforgery ( ) ;
2024-12-31 01:03:35 -05:00
app . MapRazorComponents < App > ( ) . AddInteractiveServerRenderMode ( ) ;
2024-12-31 01:01:16 -05:00
2024-12-31 19:01:25 -05:00
if ( config ? . DatabaseType = = DatabaseTypes . Memory )
{
app . Logger . LogWarning (
"`Memory` database type is in use. Data will be lost when the application is restarted. If you are not in a development environment, you should use `PostgresSQL` instead."
) ;
}
2025-01-01 09:25:21 -05:00
// if (app.Environment.IsDevelopment())
// {
// app.Logger.LogInformation(
// "Development mode detected, printing configuration: {Configuration}",
// config?.ToJson()
// );
// }
2024-12-31 01:01:16 -05:00
app . Run ( ) ;