using CarCareTracker.External.Implementations; using CarCareTracker.External.Interfaces; using CarCareTracker.Helper; using CarCareTracker.Logic; using CarCareTracker.Middleware; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.FileProviders; using System.Globalization; var builder = WebApplication.CreateBuilder(args); //Additional JsonFile builder.Configuration.AddJsonFile(StaticHelper.UserConfigPath, optional: true, reloadOnChange: true); builder.Configuration.AddJsonFile(StaticHelper.ServerConfigPath, optional: true, reloadOnChange: true); if (!string.IsNullOrWhiteSpace(builder.Configuration["LUBELOGGER_LOCALE_OVERRIDE"])) { var overrideCulture = new CultureInfo(builder.Configuration["LUBELOGGER_LOCALE_OVERRIDE"]); if (!string.IsNullOrWhiteSpace(builder.Configuration["LUBELOGGER_LOCALE_DT_OVERRIDE"])) { var overrideDTFormat = new CultureInfo(builder.Configuration["LUBELOGGER_LOCALE_DT_OVERRIDE"]); overrideCulture.DateTimeFormat = overrideDTFormat.DateTimeFormat; } CultureInfo.DefaultThreadCurrentCulture = overrideCulture; CultureInfo.DefaultThreadCurrentUICulture = overrideCulture; } //Print Messages StaticHelper.InitMessage(builder.Configuration); //Check Migration StaticHelper.CheckMigration(builder.Environment.WebRootPath, builder.Environment.ContentRootPath); // Add services to the container. builder.Services.AddControllersWithViews(); //LiteDB is always injected even if user uses Postgres. builder.Services.AddSingleton(); //data access method if (!string.IsNullOrWhiteSpace(builder.Configuration["POSTGRES_CONNECTION"])){ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); } else { builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); } //configure helpers builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); //configure logic builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); //Configure Auth builder.Services.AddDataProtection(); builder.Services.AddHttpContextAccessor(); builder.Services.AddAuthentication("AuthN").AddScheme("AuthN", opts => { }); builder.Services.AddAuthorization(options => { options.DefaultPolicy = new AuthorizationPolicyBuilder().AddAuthenticationSchemes("AuthN").RequireAuthenticatedUser().Build(); }); //Configure max file upload size builder.Services.Configure(options => { options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB }); builder.Services.Configure(options => { options.ValueLengthLimit = int.MaxValue; options.MultipartBodyLengthLimit = int.MaxValue; // if don't set default value is: 128 MB options.MultipartHeadersLengthLimit = int.MaxValue; }); var app = builder.Build(); // Configure the HTTP request pipeline. app.UseExceptionHandler("/Home/Error"); app.UseStaticFiles(); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider( Path.Combine(builder.Environment.ContentRootPath, "data", "images")), RequestPath = "/images", OnPrepareResponse = ctx => { if (ctx.Context.Request.Path.StartsWithSegments("/images")) { ctx.Context.Response.Headers.Append("Cache-Control", "no-store"); if (!ctx.Context.User.Identity.IsAuthenticated) { ctx.Context.Response.Redirect("/Login"); } } } }); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider( Path.Combine(builder.Environment.ContentRootPath, "data", "documents")), RequestPath = "/documents", OnPrepareResponse = ctx => { if (ctx.Context.Request.Path.StartsWithSegments("/documents")) { ctx.Context.Response.Headers.Append("Cache-Control", "no-store"); if (!ctx.Context.User.Identity.IsAuthenticated) { ctx.Context.Response.Redirect("/Login"); } } } }); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider( Path.Combine(builder.Environment.ContentRootPath, "data", "translations")), RequestPath = "/translations" }); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider( Path.Combine(builder.Environment.ContentRootPath, "data", "temp")), RequestPath = "/temp", OnPrepareResponse = ctx => { if (ctx.Context.Request.Path.StartsWithSegments("/temp")) { ctx.Context.Response.Headers.Append("Cache-Control", "no-store"); if (!ctx.Context.User.Identity.IsAuthenticated) { ctx.Context.Response.Redirect("/Login"); } } } }); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run();