This repository was archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartup.cs
More file actions
125 lines (107 loc) · 4.02 KB
/
Startup.cs
File metadata and controls
125 lines (107 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using musicList2.Database;
using musicList2.Filter;
using musicList2.Models;
namespace musicList2
{
public class Startup
{
// Config which is getting passed by ID from Main
public IConfiguration Configuration { get; }
private AppDbContext db;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
using (db = new AppDbContext(configuration))
{
db.Database.EnsureCreated();
db.Database.Migrate();
}
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddOptions();
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
services
.AddEntityFrameworkSqlite()
.AddDbContext<AppDbContext>();
services
.AddTransient<IKeywordAccessLayer, KeywordAccessLayer>()
.AddTransient<AuthorizeMasterKey>();
services
.AddSingleton<IConfiguration>(Configuration);
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(opt => {
opt.Events = new CookieAuthenticationEvents
{
OnRedirectToLogin = async context =>
{
context.Response.StatusCode = 401;
var errorData = Encoding.UTF8.GetBytes("{\"code\": 401,\"message\": \"unauthorized\"}");
await context.Response.Body.WriteAsync(errorData);
}
};
opt.LogoutPath = opt.LoginPath;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseAuthentication();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.Use(async (ctx, next) =>
{
ctx.Response.Headers.Add("Server", "Music List");
await next();
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}");
});
app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
}
}
}