Last active
October 16, 2018 13:13
-
-
Save CodingGorilla/5658a9772d80664e44ba3cc71377d1d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Startup | |
{ | |
private readonly IConfiguration _configuration; | |
private readonly IHostingEnvironment _env; | |
private readonly LoggingLevelSwitch _logLevelSwitch; | |
public Startup(IConfiguration configuration, IHostingEnvironment env) | |
{ | |
_configuration = configuration; | |
_env = env; | |
_logLevelSwitch = new LoggingLevelSwitch(); | |
} | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddOptions(); | |
services.Configure<AuthServiceOptions>(_configuration); | |
services.Configure<MartenConnectionSettings>(_configuration.GetSection("marten")); | |
services.Configure<TokenSettings>(_configuration.GetSection("tokenSettings")); | |
services.Configure<MailSettings>(_configuration.GetSection("mailSettings")); | |
services.AddSingleton(sp => sp.GetRequiredService<IOptions<TokenSettings>>().Value.ToJwtSettingsOptions()); | |
services.AddTransient<SystemServiceAuthorizationHandler>(); | |
services.AddCors(x => x.AddPolicy("AllowAny", y => y.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().Build())); | |
services.Configure<MvcOptions>(opts => opts.Filters.Add(new CorsAuthorizationFilterFactory("AllowAny"))); | |
var tokenSettings = _configuration.GetSection("tokenSettings").Get<TokenSettings>(); | |
var authSettings = new JwtSettings | |
{ | |
ClockSkewInMinutes = tokenSettings.ClockSkewInMinutes, | |
Issuer = tokenSettings.Issuer, | |
SigningKey = tokenSettings.SigningKey | |
}; | |
services.AddLaMotteAuthorization(authSettings, !_env.IsProduction(), _env.IsDevelopment()); | |
services.AddAutoMapper(); | |
services.AddMvc() | |
.AddJsonOptions(opts => opts.SerializerSettings.NullValueHandling = NullValueHandling.Ignore); | |
} | |
private void ConfigureMartenDatabase(IServiceProvider applicationServices) | |
{ | |
var settings = applicationServices.GetRequiredService<IOptions<MartenConnectionSettings>>(); | |
var success = MartenSessionFactory.Instance.Initialize(settings.Value); | |
if(!success) | |
Log.Logger.Error("Failed to initialize marten database"); | |
} | |
/// <summary> | |
/// This method is used by Lamar to configure it's container | |
/// </summary> | |
public void ConfigureContainer(ServiceRegistry services) | |
{ | |
services.Scan(scanner => | |
{ | |
scanner.WithDefaultConventions(); | |
scanner.TheCallingAssembly(); | |
scanner.LookForRegistries(); | |
}); | |
// NOTE: This needs to be done after the scanning so that Lamar doesn't overwrite the registration | |
services.AddHttpClient<ISubscriptionsServiceClient, SubscriptionsServiceClient>() | |
.ConfigureHttpClient((sp, client) => | |
{ | |
var baseUrl = sp.GetRequiredService<IOptions<AuthServiceOptions>>().Value.SubscriptionServiceUri; | |
client.BaseAddress = new Uri(baseUrl, UriKind.Absolute); | |
}) | |
.AddHttpMessageHandler<SystemServiceAuthorizationHandler>(); | |
} | |
private void ConfigureLogging() | |
{ | |
var logConfig = _configuration.GetSection("logSettings"); | |
var customerToken = logConfig.GetValue<string>("logglyCustomerToken"); | |
var logLevel = logConfig.GetValue<LogEventLevel>("logLevel"); | |
_logLevelSwitch.MinimumLevel = logLevel; | |
var logglyConfig = new LogglyConfiguration | |
{ | |
CustomerToken = customerToken, | |
Tags = new List<string>(new[] { "MainApi" }), | |
ApplicationName = "WaterlinkSolutions" | |
}; | |
Log.Logger = new LoggerConfiguration() | |
.MinimumLevel.ControlledBy(_logLevelSwitch) | |
.MinimumLevel.Override("Microsoft", LogEventLevel.Debug) | |
.Enrich.WithProperty("LamotteServiceName", "Authentication") | |
.Enrich.FromLogContext() | |
.WriteTo.Console() | |
.WriteTo.Debug() | |
.WriteTo.Loggly(logglyConfig:logglyConfig) | |
.CreateLogger(); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
var sendDetail = !env.IsProduction(); | |
app.UseApiExceptionHandler(sendDetail); | |
ConfigureLogging(); | |
ConfigureMartenDatabase(app.ApplicationServices); | |
if(env.IsDevelopment()) | |
{ | |
var container = (Container)app.ApplicationServices; | |
Log.Logger.Debug(container.WhatDidIScan()); | |
Log.Logger.Debug(container.WhatDoIHave()); | |
} | |
app | |
.UseLaMotteAuthentication() | |
.UseMvc(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment