-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
executable file
·131 lines (112 loc) · 4.88 KB
/
Startup.cs
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
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Music.Infrastructure.UnitsOfWork;
using Microsoft.EntityFrameworkCore;
using Npgsql;
using Serilog;
using PostgresSink;
using AspNet.Security.OpenIdConnect.Primitives;
using OpenIddict.Core;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
using AutoMapper;
using Music.Presentation;
using Music.Application;
using Music.Application.IService;
using Music.Infrastructure.Repositories;
using Music.Domain.Entities;
using Music.Domain.IRepositories;
using Music.Infrastructure.Logging;
namespace MusicDB
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.PostgreSqlServer(Configuration.GetConnectionString("gen"), "logs")
.WriteTo.LiterateConsole()
.CreateLogger();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
var connection = new NpgsqlConnection(Configuration.GetConnectionString("gen"));
services.AddDbContext<FirstGenUnitOfWork>(option => option.UseNpgsql(connection));
services.AddDbContext<SecondGenUnitOfWork>(option => option.UseNpgsql(connection));
services.AddScoped<MainUnitOfWork, MainUnitOfWork>();
services.AddScoped<IArtistService, ArtistService>();
services.AddScoped<IArtistRepository, ArtistRepository>();
services.AddScoped<IAlbumService, AlbumService>();
services.AddScoped<IAlbumRepository, AlbumRepository>();
services.AddScoped<IGenreService, GenreService>();
services.AddScoped<IGenreRepository, GenreRepository>();
services.AddScoped<ITrackService, TrackService>();
services.AddScoped<ITrackRepository, TrackRepository>();
services.AddMvc();
services.AddAutoMapper();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
Logger.LoggerFactory = loggerFactory.AddSerilog();
app.UseCors(builder =>
{
builder.WithOrigins("http://192.168.143.180:4200", "http://localhost:4200");
builder.AllowAnyHeader();
builder.AllowAnyMethod();
});
// Disable the automatic JWT -> WS-Federation claims mapping used by the JWT middleware:
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap.Clear();
// Authenticate users on a separate server
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
Audience = "FedAuth",
Authority = "http://localhost:5110/",
AutomaticAuthenticate = true,
AutomaticChallenge = true,
RequireHttpsMetadata = false,
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = OpenIdConnectConstants.Claims.Name,
RoleClaimType = OpenIdConnectConstants.Claims.Role
}
});
app.UseMvc();
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}