diff options
Diffstat (limited to 'Timeline/Startup.cs')
-rw-r--r-- | Timeline/Startup.cs | 71 |
1 files changed, 44 insertions, 27 deletions
diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 88348892..0c8d7052 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -1,26 +1,31 @@ +using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.SpaServices.AngularCli; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; using System.Text; using Timeline.Configs; -using Timeline.Services; -using Microsoft.AspNetCore.HttpOverrides; using Timeline.Formatters; +using Timeline.Models; +using Timeline.Services; namespace Timeline { public class Startup { - public Startup(IConfiguration configuration) + private const string corsPolicyName = "MyPolicy"; + + public Startup(IConfiguration configuration, IHostingEnvironment environment) { + Environment = environment; Configuration = configuration; } + public IHostingEnvironment Environment { get; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. @@ -31,14 +36,29 @@ namespace Timeline options.InputFormatters.Add(new StringInputFormatter()); }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); - // In production, the Angular files will be served from this directory - services.AddSpaStaticFiles(configuration => + if (Environment.IsDevelopment()) { - configuration.RootPath = "ClientApp/dist"; - }); + services.AddCors(options => + { + options.AddPolicy(corsPolicyName, builder => + { + builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials(); + }); + }); + } + else + { + services.AddCors(options => + { + options.AddPolicy(corsPolicyName, builder => + { + builder.WithOrigins("https://www.crupest.xyz", "https://crupest.xyz").AllowAnyMethod().AllowAnyHeader().AllowCredentials(); + }); + }); + } - services.Configure<JwtConfig>(Configuration.GetSection("JwtConfig")); - var jwtConfig = Configuration.GetSection("JwtConfig").Get<JwtConfig>(); + services.Configure<JwtConfig>(Configuration.GetSection(nameof(JwtConfig))); + var jwtConfig = Configuration.GetSection(nameof(JwtConfig)).Get<JwtConfig>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(o => @@ -52,14 +72,22 @@ namespace Timeline o.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtConfig.SigningKey)); }); - services.AddSingleton<IUserService, UserService>(); - services.AddSingleton<IJwtService, JwtService>(); + services.AddScoped<IUserService, UserService>(); + services.AddScoped<IJwtService, JwtService>(); + services.AddTransient<IPasswordService, PasswordService>(); + + var databaseConfig = Configuration.GetSection(nameof(DatabaseConfig)).Get<DatabaseConfig>(); + + services.AddDbContext<DatabaseContext>(options => + { + options.UseMySql(databaseConfig.ConnectionString); + }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app) { - if (env.IsDevelopment()) + if (Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } @@ -68,8 +96,7 @@ namespace Timeline app.UseExceptionHandler("/Error"); } - app.UseStaticFiles(); - app.UseSpaStaticFiles(); + app.UseCors(corsPolicyName); app.UseForwardedHeaders(new ForwardedHeadersOptions { @@ -84,16 +111,6 @@ namespace Timeline name: "default", template: "{controller}/{action=Index}/{id?}"); }); - - app.UseSpa(spa => - { - spa.Options.SourcePath = "ClientApp"; - - if (env.IsDevelopment()) - { - spa.UseAngularCliServer(npmScript: "start"); - } - }); } } } |