cloutier--bird.makeup/src/BirdsiteLive/Startup.cs

143 lines
5.5 KiB
C#
Raw Normal View History

2020-03-21 00:39:32 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
2021-01-16 01:18:03 -05:00
using System.Net.Http;
2020-03-21 00:39:32 -04:00
using System.Threading.Tasks;
2020-03-22 01:29:51 -04:00
using BirdsiteLive.Common.Settings;
2020-12-28 02:19:11 -05:00
using BirdsiteLive.Common.Structs;
2020-07-07 21:03:20 -04:00
using BirdsiteLive.DAL.Contracts;
using BirdsiteLive.DAL.Postgres.DataAccessLayers;
using BirdsiteLive.DAL.Postgres.Settings;
2020-03-21 17:11:35 -04:00
using BirdsiteLive.Models;
2021-01-17 23:05:00 -05:00
using BirdsiteLive.Twitter;
2021-01-30 00:22:29 -05:00
using BirdsiteLive.Twitter.Tools;
2020-03-21 18:58:23 -04:00
using Lamar;
2020-03-21 00:39:32 -04:00
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
{
2020-03-21 17:11:35 -04:00
public Startup(IWebHostEnvironment env)
2020-03-21 00:39:32 -04:00
{
2020-03-21 17:11:35 -04:00
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<Startup>();
Configuration = builder.Build();
2020-03-21 00:39:32 -04:00
}
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)
{
2020-12-30 03:00:23 -05:00
var logsSettings = Configuration.GetSection("Logging").Get<LogsSettings>();
if(string.Equals("insights", logsSettings.Type, StringComparison.OrdinalIgnoreCase))
{
var key = logsSettings.InstrumentationKey;
services.AddApplicationInsightsTelemetry(key);
}
2020-03-21 17:11:35 -04:00
2020-03-21 00:39:32 -04:00
services.AddControllersWithViews();
2021-01-16 01:18:03 -05:00
services.AddHttpClient();
2020-03-21 00:39:32 -04:00
}
2020-03-21 18:58:23 -04:00
public void ConfigureContainer(ServiceRegistry services)
{
var twitterSettings = Configuration.GetSection("Twitter").Get<TwitterSettings>();
2020-03-22 01:29:51 -04:00
services.For<TwitterSettings>().Use(x => twitterSettings);
2020-03-21 18:58:23 -04:00
2020-06-06 00:14:42 -04:00
var instanceSettings = Configuration.GetSection("Instance").Get<InstanceSettings>();
services.For<InstanceSettings>().Use(x => instanceSettings);
2020-12-28 02:19:11 -05:00
var dbSettings = Configuration.GetSection("Db").Get<DbSettings>();
services.For<DbSettings>().Use(x => dbSettings);
2020-12-30 03:00:23 -05:00
var logsSettings = Configuration.GetSection("Logging").Get<LogsSettings>();
services.For<LogsSettings>().Use(x => logsSettings);
2021-02-03 01:25:47 -05:00
var moderationSettings = Configuration.GetSection("Moderation").Get<ModerationSettings>();
services.For<ModerationSettings>().Use(x => moderationSettings);
2020-12-28 02:19:11 -05:00
if (string.Equals(dbSettings.Type, DbTypes.Postgres, StringComparison.OrdinalIgnoreCase))
2020-07-07 21:03:20 -04:00
{
2020-12-28 02:19:11 -05:00
var connString = $"Host={dbSettings.Host};Username={dbSettings.User};Password={dbSettings.Password};Database={dbSettings.Name}";
var postgresSettings = new PostgresSettings
{
ConnString = connString
};
services.For<PostgresSettings>().Use(x => postgresSettings);
2021-01-17 23:05:00 -05:00
2020-12-28 02:19:11 -05:00
services.For<ITwitterUserDal>().Use<TwitterUserPostgresDal>().Singleton();
services.For<IFollowersDal>().Use<FollowersPostgresDal>().Singleton();
services.For<IDbInitializerDal>().Use<DbInitializerPostgresDal>().Singleton();
}
else
{
throw new NotImplementedException($"{dbSettings.Type} is not supported");
}
2021-01-17 23:05:00 -05:00
services.For<ITwitterUserService>().DecorateAllWith<CachedTwitterUserService>();
services.For<ITwitterUserService>().Use<TwitterUserService>().Singleton();
2020-07-07 21:03:20 -04:00
2021-01-30 00:22:29 -05:00
services.For<ITwitterAuthenticationInitializer>().Use<TwitterAuthenticationInitializer>().Singleton();
2020-03-21 18:58:23 -04:00
services.Scan(_ =>
{
_.Assembly("BirdsiteLive.Twitter");
2020-06-06 00:14:42 -04:00
_.Assembly("BirdsiteLive.Domain");
2020-07-07 21:03:20 -04:00
_.Assembly("BirdsiteLive.DAL");
_.Assembly("BirdsiteLive.DAL.Postgres");
2020-07-16 01:19:41 -04:00
_.Assembly("BirdsiteLive.Pipeline");
2020-03-21 18:58:23 -04:00
_.TheCallingAssembly();
//_.AssemblyContainingType<IDal>();
//_.Exclude(type => type.Name.Contains("Settings"));
2020-07-07 21:03:20 -04:00
2020-03-21 18:58:23 -04:00
_.WithDefaultConventions();
_.LookForRegistries();
});
}
2020-03-21 00:39:32 -04:00
// 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();
}
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();
}
2020-03-21 17:11:35 -04:00
//app.UseHttpsRedirection();
2020-03-21 00:39:32 -04:00
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}