using APCViewer.HostedService; using APCViewer.Models; using APCViewer.Singleton; using Helper.Log; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using Shared; using System; using System.IO; using System.Net; using System.Reflection; namespace APCViewer { public class Startup { private Background _Background; private readonly IsEnvironment _IsEnvironment; private readonly IConfiguration _Configuration; private readonly IWebHostEnvironment _WebHostEnvironment; public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment) { _Configuration = configuration; _WebHostEnvironment = webHostEnvironment; if (!(configuration is ConfigurationRoot configurationRoot)) _IsEnvironment = new IsEnvironment(webHostEnvironment.IsDevelopment(), webHostEnvironment.IsStaging(), webHostEnvironment.IsProduction()); else { foreach (IConfigurationProvider provider in configurationRoot.Providers) { if (!(provider is JsonConfigurationProvider jsonConfigurationProvider)) continue; if (jsonConfigurationProvider.Source.Optional) continue; _IsEnvironment = new IsEnvironment(jsonConfigurationProvider.Source.Path); } } } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { LogLevel defaultLogLevel; LogLevel log4netLogLevel; IConfigurationSection configurationSection; configurationSection = _Configuration.GetSection("Logging:LogLevel:Default"); if (configurationSection is null || configurationSection.Value is null) defaultLogLevel = LogLevel.Debug; else if (!Enum.TryParse(configurationSection.Value, out defaultLogLevel)) defaultLogLevel = LogLevel.Debug; configurationSection = _Configuration.GetSection("Logging:LogLevel:Log4netProvider"); if (configurationSection is null || configurationSection.Value is null) log4netLogLevel = defaultLogLevel; else if (!Enum.TryParse(configurationSection.Value, out log4netLogLevel)) log4netLogLevel = defaultLogLevel; Assembly assembly = Assembly.GetExecutingAssembly(); string workingDirectory = Log.GetWorkingDirectory(assembly.GetName().Name, "IFXApps"); services.AddLogging(logging => { logging.AddProvider(new DebugProvider(defaultLogLevel)); logging.AddProvider(new ConsoleProvider(defaultLogLevel)); logging.AddProvider(new Log4netProvider(typeof(Startup), log4netLogLevel, workingDirectory)); }); services.AddDirectoryBrowser(); services.AddControllersWithViews().AddRazorRuntimeCompilation(); services.AddServerSideBlazor(); services.AddSingleton(); services.AddSingleton(); _Background = new Background(_IsEnvironment, _Configuration, workingDirectory); services.AddSingleton(b => _Background); services.AddHostedService(t => new TimedHostedService(_Background, _Configuration, t.GetRequiredService())); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "APCViewer", Version = "v1" }); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime) { if (env.IsDevelopment()) { if (string.IsNullOrEmpty(_Background.Storage.UrlRoot)) { Environment.ExitCode = -1; lifetime.StopApplication(); } app.UseDeveloperExceptionPage(); app.UseSwagger(c => { c.SerializeAsV2 = true; }); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "APC Viewer API V1"); c.RoutePrefix = string.Empty; }); } else { app.UseExceptionHandler("/Home/Error"); // 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.UseSwagger(c => { c.SerializeAsV2 = true; }); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/APCViewer/swagger/v1/swagger.json", "APC Viewer API V1"); c.RoutePrefix = string.Empty; }); } // app.UseDirectoryBrowser(new DirectoryBrowserOptions // { // FileProvider = new PhysicalFileProvider(Path.Combine(env.WebRootPath, "images")), // RequestPath = "/MyImages" // }); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { //endpoints.MapBlazorHub(); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); app.UseDirectoryBrowser(new DirectoryBrowserOptions { FileProvider = new PhysicalFileProvider(Path.Combine(env.WebRootPath, "images")), RequestPath = "/MyImages" }); } } }