-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStartup.cs
185 lines (158 loc) · 7.25 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Aloha.Controllers;
using Aloha.Dtos;
using Aloha.Mappers;
using Aloha.Model.Contexts;
using Aloha.Model.Entities;
using Aloha.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using Swashbuckle.AspNetCore.Swagger;
namespace Aloha
{
public class Startup
{
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RouteOptions>(options =>
{
options.LowercaseUrls = true;
});
services.AddCors();
services.AddMvc(o =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
o.Filters.Add(new AuthorizeFilter(policy));
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
var key = Encoding.ASCII.GetBytes(Configuration["JwtKey"]);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
services.AddSwaggerGen(swagger =>
{
var info = new Info()
{
Title = SwaggerConfig.DocInfoTitle,
Version = SwaggerConfig.DocInfoVersion,
Description = SwaggerConfig.DocInfoDescription,
Contact = new Contact() { Name = SwaggerConfig.ContactName, Url = SwaggerConfig.ContactUrl }
};
swagger.SwaggerDoc(SwaggerConfig.DocNameV1, info);
var apiKeyScheme = new ApiKeyScheme
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Bearer {token}\"",
Name = "Authorization",
In = "header",
Type = "apiKey"
};
swagger.AddSecurityDefinition("Bearer", apiKeyScheme);
var security = new Dictionary<string, IEnumerable<string>>
{
{ "Bearer", new string[] { } },
};
swagger.AddSecurityRequirement(security);
});
services.AddDbContext<AlohaContext>(options =>
{
string connectionString = Environment.GetEnvironmentVariable("MYSQLCONNSTR_localdb");
string portNumber = Regex.Match(connectionString, @"(?<=Data Source.+:)\d+")?.Value;
if (portNumber != null && portNumber.Length > 0)
{
connectionString += ";Port=" + portNumber;
connectionString = connectionString.Replace(":" + portNumber, string.Empty);
}
options.UseMySql(connectionString);
});
// Services
services.AddScoped<ISecurityService, SecurityService>();
// Controllers
services.AddScoped<SecurityController, SecurityController>();
services.AddScoped<WorkersController, WorkersController>();
services.AddScoped<WorkstationsController, WorkstationsController>();
services.AddScoped<FloorsController, FloorsController>();
services.AddScoped<OfficesController, OfficesController>();
// Mappings
services.AddScoped<IClassMapping<User, UserDto>, UserToUserDtoMapping>();
services.AddScoped<IClassMapping<UserDto, User>, UserDtoToUserMapping>();
services.AddScoped<IClassMapping<Worker, WorkerDto>, WorkerToWorkerDtoMapping>();
services.AddScoped<IClassMapping<WorkerDto, Worker>, WorkerDtoToWorkerMapping>();
services.AddScoped<IClassMapping<Workstation, WorkstationDto>, WorkstationToWorkstationDtoMapping>();
services.AddScoped<IClassMapping<WorkstationDto, Workstation>, WorkstationDtoToWorkstationMapping>();
services.AddScoped<IClassMapping<Floor, FloorDto>, FloorToFloorDtoMapping>();
services.AddScoped<IClassMapping<FloorDto, Floor>, FloorDtoToFloorMapping>();
services.AddScoped<IClassMapping<Office, OfficeDto>, OfficeToOfficeDtoMapping>();
services.AddScoped<IClassMapping<OfficeDto, Office>, OfficeDtoToOfficeMapping>();
// Updaters
services.AddScoped<IEntityUpdater<Worker>, WorkerUpdater>();
services.AddScoped<IEntityUpdater<Workstation>, WorkstationUpdater>();
services.AddScoped<IEntityUpdater<Floor>, FloorUpdater>();
services.AddScoped<IEntityUpdater<Office>, OfficeUpdater>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint(SwaggerConfig.EndpointUrl, SwaggerConfig.EndpointDescription));
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc();
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
using (var context = serviceScope.ServiceProvider.GetService<AlohaContext>())
{
context.Database.Migrate();
}
}
}
}
}