Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace MannsBlog
- {
- /// <summary>
- /// Startup Class.
- /// </summary>
- public class Startup
- {
- /// <summary>
- /// The cors policy name.
- /// </summary>
- public const string CorsPolicyName = "_saigkills_cors";
- /// <summary>
- /// Initializes a new instance of the <see cref="Startup"/> class.
- /// </summary>
- /// <param name="configuration">The configuration.</param>
- /// <param name="environment">The environment.</param>
- public Startup(IConfiguration configuration, IHostEnvironment environment, ILoggerFactory loggerFactory, IMailService mailService, IOptions<AppSettings> settings, IHttpContextAccessor contextAccessor)
- {
- this.ConfigRoot = configuration;
- this.Env = environment;
- this.LoggerFactory = loggerFactory;
- this.MailService = mailService;
- this.Settings = settings;
- this.ContextAccessor = contextAccessor;
- }
- /// <summary>
- /// Gets the configuration root.
- /// </summary>
- /// <value>
- /// The configuration root.
- /// </value>
- public IConfiguration ConfigRoot
- {
- get;
- }
- /// <summary>
- /// Gets the env.
- /// </summary>
- /// <value>
- /// The env.
- /// </value>
- public IHostEnvironment Env
- {
- get;
- }
- public ILoggerFactory LoggerFactory
- {
- get;
- }
- public IMailService MailService
- {
- get;
- }
- public IOptions<AppSettings> Settings
- {
- get;
- }
- public IHttpContextAccessor ContextAccessor
- {
- get;
- }
- /// <summary>
- /// Configures the services.
- /// </summary>
- /// <param name="services">The services.</param>
- public void ConfigureServices(IServiceCollection services)
- {
- // Add services to the container.
- services.Configure<AppSettings>(this.ConfigRoot);
- if (this.Env.IsDevelopment() && this.ConfigRoot.GetValue<bool>("MailService:TestInDev") == false)
- {
- services.AddTransient<IMailService, LoggingMailService>();
- }
- else
- {
- services.AddTransient<IMailService, SendgridMailService>();
- }
- services.AddTransient<GoogleCaptchaService>();
- services.AddIdentity<MannsUser, IdentityRole>().AddEntityFrameworkStores<MannsContext>();
- if (this.ConfigRoot.GetValue<bool>("MannsDb:TestData"))
- {
- services.AddScoped<IMannsRepository, MemoryRepository>();
- }
- else
- {
- services.AddScoped<IMannsRepository, MannsRepository>();
- }
- services.AddCors(setup =>
- {
- setup.AddPolicy(CorsPolicyName, cfg =>
- {
- if (this.Env.IsDevelopment())
- {
- cfg.AllowAnyMethod();
- cfg.AllowAnyOrigin();
- cfg.AllowAnyHeader();
- }
- else
- {
- cfg.WithMethods("POST");
- cfg.WithOrigins(
- "https://saschamanns.de",
- "http://saschamanns.de");
- cfg.AllowAnyHeader();
- }
- });
- });
- services.ConfigureHealthChecks(this.ConfigRoot);
- services.AddTransient<SeedData>();
- services.AddScoped<AdService>();
- // Non EF data.
- services.AddScoped<CalendarProvider>();
- services.AddScoped<PublicationProvider>();
- services.AddScoped<TalksProvider>();
- services.AddScoped<VideosProvider>();
- services.AddScoped<JobsProvider>();
- services.AddScoped<TestimonialsProvider>();
- services.AddScoped<CertsProvider>();
- services.AddScoped<ProjectsProvider>();
- if ((this.Env.IsDevelopment() && this.ConfigRoot.GetValue<bool>("BlobStorage:TestInDev") == false) || this.ConfigRoot.GetValue<string>("BlobStorage:Account") == "FOO")
- {
- services.AddTransient<IAzureImageStorageService, FakeAzureImageService>();
- }
- else
- {
- services.AddAzureImageStorageService(this.ConfigRoot.GetValue<string>("BlobStorage:Account"), this.ConfigRoot.GetValue<string>("BlobStorage:Key"), this.ConfigRoot.GetValue<string>("BlobStorage:StorageUrl"));
- }
- services.AddMetaWeblog<MannsWeblogProvider>();
- services.AddMemoryCache(opt => opt.ExpirationScanFrequency = TimeSpan.FromMinutes(5));
- // Add MVC to the container
- services.AddRazorPages();
- services.AddServerSideBlazor();
- services.AddMvc(); // TODO: Add Localisation
- }
- /// <summary>
- /// Configures the specified application.
- /// </summary>
- /// <param name="app">The application.</param>
- /// <param name="env">The env.</param>
- public void Configure(WebApplication app, IWebHostEnvironment env)
- {
- // Configure the HTTP request pipeline.
- if (!app.Environment.IsDevelopment())
- {
- // Early so we can catch the StatusCode error.
- app.UseStatusCodePagesWithReExecute("/Error/{0}");
- app.UseExceptionHandler("/Exception");
- // Support for logging to email.
- this.LoggerFactory.AddEmail(this.MailService, this.ContextAccessor, LogLevel.Critical);
- // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
- app.UseHsts();
- }
- else
- {
- app.UseDeveloperExceptionPage();
- app.UseBrowserLink();
- }
- app.UseHttpsRedirection();
- // Support Metaweblog API.
- app.UseMetaWeblog("/livewriter");
- // Rewrite old Urls.
- app.UseUrlRewriter();
- app.UseStaticFiles();
- // Email Uncaught Exceptions
- if (this.Settings.Value.Exceptions.TestEmailExceptions || !app.Environment.IsDevelopment())
- {
- app.UseMiddleware<EmailExceptionMiddleware>();
- }
- app.UseRouting();
- app.UseCors();
- app.UseAuthentication();
- app.UseAuthorization();
- app.MapControllers();
- app.MapBlazorHub();
- app.MapFallbackToPage("/_Host");
- app.MapHealthChecks("/health", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions
- {
- ResultStatusCodes =
- {
- [HealthStatus.Healthy] = StatusCodes.Status200OK,
- [HealthStatus.Degraded] = StatusCodes.Status200OK,
- [HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,
- },
- Predicate = _ => true,
- ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse,
- });
- // Initialize the database
- var scopeFactory = app.Services.GetRequiredService<IServiceScopeFactory>();
- using (var scope = scopeFactory.CreateScope())
- {
- var ctx = scope.ServiceProvider.GetRequiredService<MannsContext>();
- if (ctx.Database.EnsureCreated())
- {
- SeedData.UserAsync();
- SeedData.StoriesAsync();
- }
- }
- app.Run();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement