diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-10-05 14:45:21 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-10-05 14:45:21 +0200 |
| commit | b7e39b59fd0fc7b5610ebff29035bf622079e0d8 (patch) | |
| tree | 64be84ebbdac9f7ceced983390c53b10d575af5c /server/src/Program.cs | |
| parent | 2001c035fbb417ab0a3d42cfb04d17420bde4086 (diff) | |
| download | greatoffice-b7e39b59fd0fc7b5610ebff29035bf622079e0d8.tar.xz greatoffice-b7e39b59fd0fc7b5610ebff29035bf622079e0d8.zip | |
refactor: Change file structure
Diffstat (limited to 'server/src/Program.cs')
| -rw-r--r-- | server/src/Program.cs | 236 |
1 files changed, 0 insertions, 236 deletions
diff --git a/server/src/Program.cs b/server/src/Program.cs deleted file mode 100644 index 9a6bc3f..0000000 --- a/server/src/Program.cs +++ /dev/null @@ -1,236 +0,0 @@ -global using System; -global using System.Linq; -global using System.IO; -global using System.Net.Mail; -global using System.Net; -global using System.Threading; -global using System.Threading.Tasks; -global using System.Collections.Generic; -global using System.Runtime.Serialization; -global using System.ComponentModel.DataAnnotations.Schema; -global using System.Security.Claims; -global using System.Text.Json; -global using System.Text.Json.Serialization; -global using IOL.GreatOffice.Api.Data.Database; -global using IOL.GreatOffice.Api.Data.Exceptions; -global using IOL.GreatOffice.Api.Data.Dtos; -global using IOL.GreatOffice.Api.Data.Enums; -global using IOL.GreatOffice.Api.Data.Models; -global using IOL.GreatOffice.Api.Data.Results; -global using IOL.Helpers; -global using Microsoft.OpenApi.Models; -global using Microsoft.AspNetCore.Authentication.Cookies; -global using Microsoft.AspNetCore.Builder; -global using Microsoft.AspNetCore.DataProtection; -global using Microsoft.AspNetCore.Hosting; -global using Microsoft.AspNetCore.Authorization; -global using Microsoft.AspNetCore.Http; -global using Microsoft.AspNetCore.Authentication; -global using Microsoft.AspNetCore.Mvc; -global using Microsoft.EntityFrameworkCore; -global using Microsoft.Extensions.Configuration; -global using Microsoft.Extensions.DependencyInjection; -global using Microsoft.Extensions.Hosting; -global using Microsoft.Extensions.Logging; -global using Serilog; -global using IOL.GreatOffice.Api.Data; -global using IOL.GreatOffice.Api.Data.Static; -global using IOL.GreatOffice.Api.Services; -global using IOL.GreatOffice.Api.Utilities; -using System.Reflection; -using IOL.GreatOffice.Api.Endpoints.V1; -using IOL.GreatOffice.Api.Jobs; -using Microsoft.AspNetCore.HttpOverrides; -using Microsoft.AspNetCore.Mvc.Versioning; -using Quartz; - -namespace IOL.GreatOffice.Api; - -public static class Program -{ - public static WebApplicationBuilder CreateAppBuilder(string[] args) { - var builder = WebApplication.CreateBuilder(args); - builder.Services.AddLogging(); - builder.Services.AddHttpClient(); - builder.Services.AddMemoryCache(); - builder.Services.AddScoped<MailService>(); - builder.Services.AddScoped<PasswordResetService>(); - builder.Services.AddScoped<UserService>(); - builder.Services.AddTransient<VaultService>(); - var vaultService = builder.Services.BuildServiceProvider().GetRequiredService<VaultService>(); - var configuration = vaultService.GetCurrentAppConfiguration(); - var logger = new LoggerConfiguration() - .Enrich.FromLogContext() - .ReadFrom.Configuration(builder.Configuration) - .WriteTo.Console(); - - if (!builder.Environment.IsDevelopment() && configuration.SEQ_API_KEY.HasValue() && configuration.SEQ_API_URL.HasValue()) { - logger.WriteTo.Seq(configuration.SEQ_API_URL, apiKey: configuration.SEQ_API_KEY); - } - - Log.Logger = logger.CreateLogger(); - Log.Information("Starting web host, " - + JsonSerializer.Serialize(configuration.GetPublicVersion(), - new JsonSerializerOptions() { - WriteIndented = true - })); - builder.Host.UseSerilog(Log.Logger); - builder.WebHost.ConfigureKestrel(kestrel => { - kestrel.AddServerHeader = false; - }); - - if (builder.Environment.IsDevelopment()) { - builder.Services.AddCors(); - } - - if (builder.Environment.IsProduction()) { - builder.Services.Configure<ForwardedHeadersOptions>(options => { - options.ForwardedHeaders = ForwardedHeaders.XForwardedProto; - }); - } - - builder.Services - .AddDataProtection() - .ProtectKeysWithCertificate(configuration.CERT1()) - .PersistKeysToDbContext<AppDbContext>(); - - builder.Services.Configure(JsonSettings.Default); - builder.Services.AddQuartz(options => { - options.UsePersistentStore(o => { - o.UsePostgres(builder.Configuration.GetQuartzDatabaseConnectionString(vaultService.GetCurrentAppConfiguration)); - o.UseSerializer<QuartzJsonSerializer>(); - }); - options.UseMicrosoftDependencyInjectionJobFactory(); - options.RegisterJobs(); - }); - - builder.Services.AddQuartzHostedService(options => { - options.WaitForJobsToComplete = true; - }); - - builder.Services.AddAuthentication(options => { - options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; - }) - .AddCookie(options => { - options.Cookie.Name = "go_session"; - options.Cookie.HttpOnly = true; - options.Cookie.IsEssential = true; - options.SlidingExpiration = true; - options.Events.OnRedirectToAccessDenied = - options.Events.OnRedirectToLogin = c => { - c.Response.StatusCode = StatusCodes.Status401Unauthorized; - return Task.FromResult<object>(null); - }; - }) - .AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>(AppConstants.BASIC_AUTH_SCHEME, default); - - builder.Services.AddDbContext<AppDbContext>(options => { - options.UseNpgsql(builder.Configuration.GetAppDatabaseConnectionString(vaultService.GetCurrentAppConfiguration), - npgsqlDbContextOptionsBuilder => { - npgsqlDbContextOptionsBuilder.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery); - npgsqlDbContextOptionsBuilder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), default); - }) - .UseSnakeCaseNamingConvention(); - if (builder.Environment.IsDevelopment()) { - options.EnableSensitiveDataLogging(); - } - }); - - builder.Services.AddApiVersioning(options => { - options.ApiVersionReader = new UrlSegmentApiVersionReader(); - options.ReportApiVersions = true; - options.AssumeDefaultVersionWhenUnspecified = false; - }); - builder.Services.AddVersionedApiExplorer(options => { - options.SubstituteApiVersionInUrl = true; - }); - builder.Services.AddSwaggerGen(options => { - options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, Assembly.GetExecutingAssembly().GetName().Name + ".xml")); - options.UseApiEndpoints(); - options.OperationFilter<SwaggerDefaultValues>(); - options.SwaggerDoc(ApiSpecV1.Document.VersionName, ApiSpecV1.Document.OpenApiInfo); - options.AddSecurityDefinition("Basic", - new OpenApiSecurityScheme { - Name = "Authorization", - Type = SecuritySchemeType.ApiKey, - Scheme = "Basic", - BearerFormat = "Basic", - In = ParameterLocation.Header, - Description = - "Enter your token in the text input below.\r\n\r\nExample: \"Basic 12345abcdef\"", - }); - - options.AddSecurityRequirement(new OpenApiSecurityRequirement { - { - new OpenApiSecurityScheme { - Reference = new OpenApiReference { - Type = ReferenceType.SecurityScheme, - Id = "Basic" - } - }, - Array.Empty<string>() - } - }); - }); - - builder.Services - .AddControllers() - .AddJsonOptions(JsonSettings.Default); - - return builder; - } - - public static WebApplication CreateWebApplication(WebApplicationBuilder builder) { - var app = builder.Build(); - - if (app.Environment.IsDevelopment()) { - app.UseDeveloperExceptionPage(); - app.UseCors(cors => { - cors.AllowAnyMethod(); - cors.AllowAnyHeader(); - cors.SetIsOriginAllowed((origin) => true); - cors.AllowCredentials(); - }); - } - - if (app.Environment.IsProduction()) { - app.UseForwardedHeaders(); - } - - app.UseDefaultFiles() - .UseStaticFiles() - .UseRouting() - .UseSerilogRequestLogging() - .UseStatusCodePages() - .UseAuthentication() - .UseAuthorization() - .UseSwagger() - .UseSwaggerUI(options => { - options.SwaggerEndpoint(ApiSpecV1.Document.SwaggerPath, ApiSpecV1.Document.VersionName); - options.DocumentTitle = AppConstants.API_NAME; - }) - .UseEndpoints(endpoints => { - endpoints.MapControllers(); - }); - return app; - } - - public static int Main(string[] args) { - try { - CreateWebApplication(CreateAppBuilder(args)).Run(); - return 0; - } catch (Exception ex) { - // This is subject to change in future .net versions, see https://github.com/dotnet/runtime/issues/60600. - if (ex.GetType().Name.Equals("StopTheHostException", StringComparison.Ordinal)) { - throw; - } - - Log.Fatal(ex, "Unhandled exception"); - return 1; - } finally { - Log.Information("Shut down complete, flusing logs..."); - Log.CloseAndFlush(); - } - } -} |
