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