114 lines
3.2 KiB
C#
114 lines
3.2 KiB
C#
using System.Reflection;
|
|
using LookingGlass.Data;
|
|
using LookingGlass.Util.SwaggerTheme;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.OpenApi.Models;
|
|
using Newtonsoft.Json;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
var config = builder.Configuration.GetSection("Configuration").Get<ApplicationSettings>();
|
|
builder.Services.Configure<ApplicationSettings>(builder.Configuration.GetSection("Configuration"));
|
|
|
|
if ((config?.AuthToken?.Length ?? 0) < 64 && !builder.Environment.IsDevelopment())
|
|
{
|
|
throw new InvalidOperationException(
|
|
"Global token is not a valid value, please set the `CONFIGURATION__GLOBALTOKEN` environment variable to a random value of at least 64 characters. `openssl rand -hex 64` is a good way to generate a random value."
|
|
);
|
|
}
|
|
|
|
builder.Services.AddDbContextFactory<ApplicationContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("ApplicationContext"))
|
|
);
|
|
|
|
if (config?.UseSentry == true)
|
|
{
|
|
builder.WebHost.UseSentry();
|
|
}
|
|
|
|
builder.Services.AddHttpLogging(o => { });
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(opts =>
|
|
{
|
|
opts.SwaggerDoc(
|
|
"v1",
|
|
new OpenApiInfo
|
|
{
|
|
Title = "Looking Glass API",
|
|
Version = "v1",
|
|
Description = "",
|
|
}
|
|
);
|
|
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));
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
foreach (var resource in assembly.GetManifestResourceNames())
|
|
{
|
|
app.Logger.LogDebug("Resource: {Resource}", resource);
|
|
}
|
|
|
|
if (config?.UseSwagger == true)
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(CustomStyle.CustomModern);
|
|
}
|
|
|
|
if (config?.LogRequests == true)
|
|
{
|
|
app.UseHttpLogging();
|
|
}
|
|
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Error", createScopeForErrors: true);
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
//app.UseAntiforgery();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.Logger.LogInformation(
|
|
"Development mode detected, printing configuration: {Configuration}",
|
|
config != null ? JsonConvert.SerializeObject(config) : "null"
|
|
);
|
|
}
|
|
|
|
app.Run();
|