85 lines
2.9 KiB
C#
85 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.HttpsPolicy;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using OFBButte.Application.Configuration;
|
|
using OFBButte.Application.Database;
|
|
using OFBButte.Application.Email;
|
|
using OFBButte.Database;
|
|
using OFBButte.Infrastructure.Email;
|
|
|
|
namespace OFBButte.Api
|
|
{
|
|
public class Startup
|
|
{
|
|
private readonly string CorsPolicy = "OFBCorsPolicy";
|
|
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
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)
|
|
{
|
|
// CORS - This needs to be before services.AddMvc
|
|
services.AddCors(options =>
|
|
{
|
|
options.AddPolicy(CorsPolicy, policy =>
|
|
{
|
|
policy.WithOrigins("http://localhost:4200", "https://test.ofbbutte.com", "https://ofbbutte.com")
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
|
|
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
|
|
|
// App Settings
|
|
services.Configure<AppSettings>(Configuration.GetSection("App"));
|
|
|
|
// DB Context
|
|
services.AddDbContext<OFBContext>(o =>
|
|
{
|
|
OFBContext.UseMySql(o, Configuration.GetConnectionString("OFBContext"));
|
|
});
|
|
services.AddScoped<IOFBContext, OFBContext>(s => s.GetService<OFBContext>());
|
|
|
|
// Email Service
|
|
services.AddScoped<IEmailSender, EmailSender>(s =>
|
|
{
|
|
var options = s.GetService<IOptions<AppSettings>>();
|
|
return new EmailSender(options.Value.Environment != "Prod");
|
|
});
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
|
|
{
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
else
|
|
{
|
|
// 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.UseCors(CorsPolicy);
|
|
app.UseHttpsRedirection();
|
|
app.UseMvc();
|
|
}
|
|
}
|
|
}
|