source: Farmatiko/Startup.cs@ 1f4846d

Last change on this file since 1f4846d was 6f203af, checked in by DimitarSlezenkovski <dslezenkovski@…>, 3 years ago

Change methods & add error controller

  • Property mode set to 100644
File size: 3.8 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 FarmatikoServices;
10using FarmatikoData.FarmatikoRepoInterfaces;
11using FarmatikoData.FarmatikoRepo;
12using FarmatikoServices.FarmatikoServiceInterfaces;
13using FarmatikoServices.Services;
14using Microsoft.Extensions.Logging;
15
16namespace Farmatiko
17{
18 public class Startup
19 {
20 readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
21
22 public Startup(IConfiguration configuration)
23 {
24 Configuration = configuration;
25 }
26
27 public IConfiguration Configuration { get; }
28
29 // This method gets called by the runtime. Use this method to add services to the container.
30 public void ConfigureServices(IServiceCollection services)
31 {
32 services.AddCors(options =>
33 {
34 options.AddPolicy(name: MyAllowSpecificOrigins,
35 builder =>
36 {
37 builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
38 });
39 });
40
41 services.AddControllersWithViews();
42 // In production, the Angular files will be served from this directory
43 services.AddSpaStaticFiles(configuration =>
44 {
45 configuration.RootPath = "ClientApp/dist";
46 });
47 var connectionString = Configuration.GetSection("ConnectionStrings").GetValue<string>("FarmatikoConnection");
48 services.AddEntityFrameworkNpgsql().AddDbContext<FarmatikoDataContext>(opt => opt.UseNpgsql(connectionString));
49
50 services.AddTransient<IPHRepo, PHRepo>();
51 services.AddTransient<IRepository, Repository>();
52 services.AddTransient<IAdminRepo, AdminRepo>();
53
54 services.AddTransient<IPHService, PHService>();
55 services.AddTransient<IAdminService, AdminService>();
56 services.AddTransient<IService, Service>();
57
58 services.AddTransient<IProcessJSONService, ProcessJSONService>();
59
60 services.AddTransient<ILogger, Logger<ProcessJSONService>>();
61 }
62
63 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
64 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
65 {
66 if (env.IsDevelopment())
67 {
68 app.UseDeveloperExceptionPage();
69 }
70 else
71 {
72 app.UseExceptionHandler("/Error");
73 // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
74 app.UseHsts();
75 }
76
77 app.UseHttpsRedirection();
78 app.UseStaticFiles();
79 if (!env.IsDevelopment())
80 {
81 app.UseSpaStaticFiles();
82 }
83
84 app.UseRouting();
85
86 app.UseCors(MyAllowSpecificOrigins);
87
88 app.UseEndpoints(endpoints =>
89 {
90 endpoints.MapControllerRoute(
91 name: "default",
92 pattern: "{controller}/{action=Index}/{id?}");
93 });
94
95 app.UseSpa(spa =>
96 {
97 // To learn more about options for serving an Angular SPA from ASP.NET Core,
98 // see https://go.microsoft.com/fwlink/?linkid=864501
99
100 spa.Options.SourcePath = "ClientApp";
101
102 if (env.IsDevelopment())
103 {
104 spa.UseAngularCliServer(npmScript: "start");
105 }
106 });
107 }
108 }
109}
Note: See TracBrowser for help on using the repository browser.