108 lines
3.1 KiB
C#
108 lines
3.1 KiB
C#
using backend.Extensions;
|
|
using backend.Services.Init;
|
|
using Hangfire;
|
|
using Hangfire.Dashboard;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using Microsoft.OpenApi.Models;
|
|
using Scalar.AspNetCore;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
|
|
var isDevelopment = environment == Environments.Development;
|
|
|
|
var configurationBuilder = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile(
|
|
isDevelopment ? "appsettings.Development.json" : "appsettings.Production.json",
|
|
false,
|
|
true
|
|
)
|
|
.AddEnvironmentVariables();
|
|
|
|
IConfiguration configuration = configurationBuilder.Build();
|
|
builder.Services.SetupConfiguration(configuration);
|
|
builder.Services.ConfigureServices(configuration);
|
|
builder.Services.SetupDatabase(configuration);
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddOpenApi(options =>
|
|
{
|
|
//in production, override the server URL
|
|
if (!isDevelopment)
|
|
{
|
|
options.AddDocumentTransformer((document, _, _) =>
|
|
{
|
|
document.Servers.Clear();
|
|
document.Servers.Add(new OpenApiServer
|
|
{
|
|
Url = "https://fuelwizard.au"
|
|
});
|
|
return Task.CompletedTask;
|
|
});
|
|
}
|
|
});
|
|
|
|
var allowedOrigins = Environment.GetEnvironmentVariable("ALLOWED_ORIGINS")?.Split(',') ??
|
|
configuration.GetSection("AllowedOrigins").Get<string[]>();
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(policy =>
|
|
{
|
|
policy.WithOrigins(allowedOrigins ?? [])
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod();
|
|
});
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
//if we receive forwarded headers from the reverse proxy
|
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
|
{
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor |
|
|
ForwardedHeaders.XForwardedProto |
|
|
ForwardedHeaders.XForwardedHost
|
|
});
|
|
|
|
app.MapOpenApi("/api/openapi/v1.json");
|
|
app.MapScalarApiReference(endpointPrefix: "/api-docs", options =>
|
|
{
|
|
options
|
|
.WithOpenApiRoutePattern("/api/openapi/v1.json")
|
|
.WithTitle("Fuel Wizard API Reference");
|
|
});
|
|
app.UseHttpsRedirection();
|
|
app.MapControllers();
|
|
app.UseRateLimiter();
|
|
app.UseCors();
|
|
|
|
if (isDevelopment)
|
|
app.UseHangfireDashboard("/hangfire", new DashboardOptions
|
|
{
|
|
Authorization = [new NoAuthFilter()]
|
|
});
|
|
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
//initialise the database
|
|
var databaseInitialiser = scope.ServiceProvider.GetRequiredService<DatabaseInitialiser>();
|
|
databaseInitialiser.Initialise();
|
|
|
|
//register the recurring jobs
|
|
var hangfireInitialiser = scope.ServiceProvider.GetRequiredService<HangfireInitialiser>();
|
|
hangfireInitialiser.RegisterFuelApiRefreshJobs();
|
|
|
|
//trigger all the recurring jobs to populate the DB with data
|
|
hangfireInitialiser.InitialiseDataOnApplicationLaunch();
|
|
}
|
|
|
|
app.Run();
|
|
|
|
internal class NoAuthFilter : IDashboardAuthorizationFilter
|
|
{
|
|
public bool Authorize(DashboardContext dashboardContext)
|
|
{
|
|
return true;
|
|
}
|
|
} |