source: Farmatiko/Startup.cs

Last change on this file was 472dd7e, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 4 years ago

Fix cron job bug

  • Property mode set to 100644
File size: 7.5 KB
Line 
1using Microsoft.AspNetCore.Builder;
2using Microsoft.AspNetCore.Hosting;
3using Microsoft.AspNetCore.SpaServices.AngularCli;
4using Microsoft.Extensions.Configuration;
5using Microsoft.Extensions.DependencyInjection;
6using Microsoft.Extensions.Hosting;
7using FarmatikoData;
8using Microsoft.EntityFrameworkCore;
9using FarmatikoData.FarmatikoRepoInterfaces;
10using FarmatikoData.FarmatikoRepo;
11using FarmatikoServices.FarmatikoServiceInterfaces;
12using FarmatikoServices.Services;
13using Microsoft.Extensions.Logging;
14using Microsoft.AspNetCore.Authentication.JwtBearer;
15using Microsoft.IdentityModel.Tokens;
16using System.Text;
17using FarmatikoServices.Auth;
18using FarmatikoServices.Infrastructure;
19using System;
20using Quartz;
21using Quartz.Impl;
22using Quartz.Spi;
23using FarmatikoServices.Services.JobDTO;
24
25namespace Farmatiko
26{
27 public class Startup
28 {
29 readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
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 {
40 services.AddCors(options =>
41 {
42 options.AddPolicy(name: MyAllowSpecificOrigins,
43 builder =>
44 {
45 builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
46 });
47 });
48
49 services.AddControllersWithViews();
50 services.AddControllersWithViews().AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
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
59
60
61 services.AddScoped<IPHRepo, PHRepo>();
62 services.AddScoped<IRepository, Repository>();
63 services.AddScoped<IAdminRepo, AdminRepo>();
64 services.AddTransient<IUpdateDataRepo, UpdateDataRepo>();
65
66 services.AddTransient<IPHService, PHService>();
67 services.AddTransient<IAdminService, AdminService>();
68 services.AddTransient<IService, Service>();
69
70 services.AddTransient<IProcessJSONService, ProcessJSONService>();
71
72 services.AddTransient<ILogger, Logger<ProcessJSONService>>();
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*/
114 services.AddSingleton<IJwtAuthManager>(new JwtAuthManager(jwtTokenConfig));
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
124 services.AddScoped<UpdateDataJob>();
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
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 }
153 app.UseExceptionHandler("/Error");
154 app.UseHttpsRedirection();
155 app.UseStaticFiles();
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
164 if (!env.IsDevelopment())
165 {
166 app.UseSpaStaticFiles();
167 }
168
169 app.UseRouting();
170
171 app.UseAuthentication();
172 app.UseAuthorization();
173
174 app.UseCors(MyAllowSpecificOrigins);
175
176 app.UseEndpoints(endpoints =>
177 {
178 endpoints.MapControllerRoute(
179 name: "default",
180 pattern: "api/{controller}/{action=Index}/{id?}");
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}
Note: See TracBrowser for help on using the repository browser.