using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using BirdsiteLive.Common.Settings; using BirdsiteLive.Common.Structs; using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Postgres.DataAccessLayers; using BirdsiteLive.DAL.Postgres.Settings; using BirdsiteLive.Models; using BirdsiteLive.Twitter; using BirdsiteLive.Twitter.Tools; using Lamar; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace BirdsiteLive { public class Startup { public Startup(IWebHostEnvironment env) { Console.WriteLine($"EnvironmentName {env.EnvironmentName}"); var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName.ToLowerInvariant()}.json", optional: true) .AddEnvironmentVariables(); if (env.IsDevelopment()) builder.AddUserSecrets(); Configuration = builder.Build(); } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var logsSettings = Configuration.GetSection("Logging").Get(); if(string.Equals("insights", logsSettings.Type, StringComparison.OrdinalIgnoreCase)) { var key = logsSettings.InstrumentationKey; services.AddApplicationInsightsTelemetry(key); } services.AddControllersWithViews(); services.AddHttpClient("BirdsiteLIVE", httpClient => { ProductInfoHeaderValue product = new("BirdsiteLIVE", $"{Program.VERSION}"); ProductInfoHeaderValue comment = new($"(+https://{Configuration["Instance:Domain"]})"); httpClient.DefaultRequestHeaders.UserAgent.Add(product); httpClient.DefaultRequestHeaders.UserAgent.Add(comment); }); } public void ConfigureContainer(ServiceRegistry services) { var twitterSettings = Configuration.GetSection("Twitter").Get(); services.For().Use(x => twitterSettings); var instanceSettings = Configuration.GetSection("Instance").Get(); services.For().Use(x => instanceSettings); var dbSettings = Configuration.GetSection("Db").Get(); services.For().Use(x => dbSettings); var logsSettings = Configuration.GetSection("Logging").Get(); services.For().Use(x => logsSettings); var moderationSettings = Configuration.GetSection("Moderation").Get(); services.For().Use(x => moderationSettings); if (string.Equals(dbSettings.Type, DbTypes.Postgres, StringComparison.OrdinalIgnoreCase)) { var connString = $"Host={dbSettings.Host};Username={dbSettings.User};Password={dbSettings.Password};Database={dbSettings.Name}"; var postgresSettings = new PostgresSettings { ConnString = connString }; services.For().Use(x => postgresSettings); services.For().Use().Singleton(); services.For().Use().Singleton(); services.For().Use().Singleton(); } else { throw new NotImplementedException($"{dbSettings.Type} is not supported"); } services.For().DecorateAllWith(); services.For().Use().Singleton(); services.For().DecorateAllWith(); services.For().Use().Singleton(); services.For().Use().Singleton(); services.Scan(_ => { _.Assembly("BirdsiteLive.Twitter"); _.Assembly("BirdsiteLive.Domain"); _.Assembly("BirdsiteLive.DAL"); _.Assembly("BirdsiteLive.DAL.Postgres"); _.Assembly("BirdsiteLive.Moderation"); _.Assembly("BirdsiteLive.Pipeline"); _.TheCallingAssembly(); //_.AssemblyContainingType(); //_.Exclude(type => type.Name.Contains("Settings")); _.WithDefaultConventions(); _.LookForRegistries(); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } 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.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } } }