[30a465f] | 1 | using Microsoft.AspNetCore.Builder;
|
---|
| 2 | using Microsoft.AspNetCore.Hosting;
|
---|
| 3 | using Microsoft.AspNetCore.SpaServices.AngularCli;
|
---|
| 4 | using Microsoft.Extensions.Configuration;
|
---|
| 5 | using Microsoft.Extensions.DependencyInjection;
|
---|
| 6 | using Microsoft.Extensions.Hosting;
|
---|
| 7 | using FarmatikoData;
|
---|
| 8 | using Microsoft.EntityFrameworkCore;
|
---|
[d2e69be] | 9 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
| 10 | using FarmatikoData.FarmatikoRepo;
|
---|
[e42f61a] | 11 | using FarmatikoServices.FarmatikoServiceInterfaces;
|
---|
| 12 | using FarmatikoServices.Services;
|
---|
[c406ae5] | 13 | using Microsoft.Extensions.Logging;
|
---|
[d23bf72] | 14 | using Microsoft.AspNetCore.Authentication.JwtBearer;
|
---|
| 15 | using Microsoft.IdentityModel.Tokens;
|
---|
| 16 | using System.Text;
|
---|
| 17 | using FarmatikoServices.Auth;
|
---|
| 18 | using FarmatikoServices.Infrastructure;
|
---|
| 19 | using System;
|
---|
[1db5673] | 20 | using Newtonsoft.Json.Serialization;
|
---|
[30a465f] | 21 | namespace Farmatiko
|
---|
| 22 | {
|
---|
| 23 | public class Startup
|
---|
| 24 | {
|
---|
[e42f61a] | 25 | readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
---|
| 26 |
|
---|
[30a465f] | 27 | public Startup(IConfiguration configuration)
|
---|
| 28 | {
|
---|
| 29 | Configuration = configuration;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | public IConfiguration Configuration { get; }
|
---|
| 33 |
|
---|
| 34 | // This method gets called by the runtime. Use this method to add services to the container.
|
---|
| 35 | public void ConfigureServices(IServiceCollection services)
|
---|
| 36 | {
|
---|
[e42f61a] | 37 | services.AddCors(options =>
|
---|
| 38 | {
|
---|
| 39 | options.AddPolicy(name: MyAllowSpecificOrigins,
|
---|
| 40 | builder =>
|
---|
| 41 | {
|
---|
| 42 | builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
|
---|
| 43 | });
|
---|
| 44 | });
|
---|
| 45 |
|
---|
[30a465f] | 46 | services.AddControllersWithViews();
|
---|
[1db5673] | 47 | services.AddControllersWithViews().AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
|
---|
[30a465f] | 48 | // In production, the Angular files will be served from this directory
|
---|
| 49 | services.AddSpaStaticFiles(configuration =>
|
---|
| 50 | {
|
---|
| 51 | configuration.RootPath = "ClientApp/dist";
|
---|
| 52 | });
|
---|
| 53 | var connectionString = Configuration.GetSection("ConnectionStrings").GetValue<string>("FarmatikoConnection");
|
---|
| 54 | services.AddEntityFrameworkNpgsql().AddDbContext<FarmatikoDataContext>(opt => opt.UseNpgsql(connectionString));
|
---|
| 55 |
|
---|
[1db5673] | 56 | services.AddScoped<IPHRepo, PHRepo>();
|
---|
[6f203af] | 57 | services.AddTransient<IRepository, Repository>();
|
---|
| 58 | services.AddTransient<IAdminRepo, AdminRepo>();
|
---|
[4e72684] | 59 |
|
---|
[6f203af] | 60 | services.AddTransient<IPHService, PHService>();
|
---|
| 61 | services.AddTransient<IAdminService, AdminService>();
|
---|
| 62 | services.AddTransient<IService, Service>();
|
---|
[a55ef91] | 63 |
|
---|
| 64 | services.AddTransient<IProcessJSONService, ProcessJSONService>();
|
---|
[c406ae5] | 65 |
|
---|
| 66 | services.AddTransient<ILogger, Logger<ProcessJSONService>>();
|
---|
[d23bf72] | 67 |
|
---|
| 68 | // services.AddTransient<ISystemService, SystemService>();
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | var jwtTokenConfig = Configuration.GetSection("jwtTokenConfig").Get<JwtTokenConfig>();
|
---|
| 72 | services.AddSingleton(jwtTokenConfig);
|
---|
| 73 |
|
---|
| 74 | services.AddAuthentication(o =>
|
---|
| 75 | {
|
---|
| 76 | o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
---|
| 77 | o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
---|
| 78 | o.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
|
---|
| 79 | }).AddJwtBearer(x =>
|
---|
| 80 | {
|
---|
| 81 | x.RequireHttpsMetadata = true;
|
---|
| 82 | x.SaveToken = true;
|
---|
| 83 | x.TokenValidationParameters = new TokenValidationParameters
|
---|
| 84 | {
|
---|
| 85 | ValidateIssuer = true,
|
---|
| 86 | ValidIssuer = jwtTokenConfig.Issuer,
|
---|
| 87 | ValidateIssuerSigningKey = true,
|
---|
| 88 | IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtTokenConfig.Secret)),
|
---|
| 89 | ValidAudience = jwtTokenConfig.Audience,
|
---|
| 90 | ValidateAudience = true,
|
---|
| 91 | ValidateLifetime = true,
|
---|
| 92 | ClockSkew = TimeSpan.FromMinutes(1)
|
---|
| 93 | };
|
---|
| 94 | });
|
---|
| 95 |
|
---|
| 96 | /*.AddJwtBearer(cfg =>
|
---|
| 97 | {
|
---|
| 98 | cfg.RequireHttpsMetadata = false;
|
---|
| 99 | cfg.SaveToken = true;
|
---|
| 100 | cfg.IncludeErrorDetails = true;
|
---|
| 101 | cfg.TokenValidationParameters = new TokenValidationParameters()
|
---|
| 102 | {
|
---|
| 103 | ValidIssuer = Configuration.GetSection("TokenIssuer").Value,
|
---|
| 104 | ValidAudience = Configuration.GetSection("TokenIssuer").Value,
|
---|
| 105 | IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("SecretKey").Value))
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 | });
|
---|
| 109 | */
|
---|
| 110 | services.AddSingleton<IJwtAuthManager, JwtAuthManager>();
|
---|
| 111 | services.AddHostedService<JwtRefreshTokenCache>();
|
---|
| 112 | services.AddScoped<IAuthService, AuthService>();
|
---|
| 113 | //If we add imgs
|
---|
| 114 | /*services.Configure<FormOptions>(o => {
|
---|
| 115 | o.ValueLengthLimit = int.MaxValue;
|
---|
| 116 | o.MultipartBodyLengthLimit = int.MaxValue;
|
---|
| 117 | o.MemoryBufferThreshold = int.MaxValue;
|
---|
| 118 | });*/
|
---|
| 119 |
|
---|
[30a465f] | 120 | }
|
---|
| 121 |
|
---|
| 122 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
---|
| 123 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
---|
| 124 | {
|
---|
| 125 | if (env.IsDevelopment())
|
---|
| 126 | {
|
---|
| 127 | app.UseDeveloperExceptionPage();
|
---|
| 128 | }
|
---|
| 129 | else
|
---|
| 130 | {
|
---|
| 131 | app.UseExceptionHandler("/Error");
|
---|
| 132 | // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
---|
| 133 | app.UseHsts();
|
---|
| 134 | }
|
---|
[d23bf72] | 135 | app.UseExceptionHandler("/Error");
|
---|
[30a465f] | 136 | app.UseHttpsRedirection();
|
---|
| 137 | app.UseStaticFiles();
|
---|
[d23bf72] | 138 |
|
---|
| 139 | // if we add imgs
|
---|
| 140 | /*app.UseStaticFiles(new StaticFileOptions()
|
---|
| 141 | {
|
---|
| 142 | FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot")),
|
---|
| 143 | RequestPath = new PathString("/wwwroot")
|
---|
| 144 | });*/
|
---|
| 145 |
|
---|
[30a465f] | 146 | if (!env.IsDevelopment())
|
---|
| 147 | {
|
---|
| 148 | app.UseSpaStaticFiles();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | app.UseRouting();
|
---|
| 152 |
|
---|
[d23bf72] | 153 | app.UseAuthentication();
|
---|
| 154 | app.UseAuthorization();
|
---|
| 155 |
|
---|
[e42f61a] | 156 | app.UseCors(MyAllowSpecificOrigins);
|
---|
| 157 |
|
---|
[30a465f] | 158 | app.UseEndpoints(endpoints =>
|
---|
| 159 | {
|
---|
| 160 | endpoints.MapControllerRoute(
|
---|
| 161 | name: "default",
|
---|
[d23bf72] | 162 | pattern: "api/{controller}/{action=Index}/{id?}");
|
---|
[30a465f] | 163 | });
|
---|
| 164 |
|
---|
| 165 | app.UseSpa(spa =>
|
---|
| 166 | {
|
---|
| 167 | // To learn more about options for serving an Angular SPA from ASP.NET Core,
|
---|
| 168 | // see https://go.microsoft.com/fwlink/?linkid=864501
|
---|
| 169 |
|
---|
| 170 | spa.Options.SourcePath = "ClientApp";
|
---|
| 171 |
|
---|
| 172 | if (env.IsDevelopment())
|
---|
| 173 | {
|
---|
| 174 | spa.UseAngularCliServer(npmScript: "start");
|
---|
| 175 | }
|
---|
| 176 | });
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|