-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
100 lines (89 loc) · 3.12 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
using Api.Data;
using Api.Settings;
using Api.Services;
using AutoMapper;
namespace Api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy)); //global authorize filter
})
.AddAuthorization()
.AddJsonFormatters();
var endpointsSection = Configuration.GetSection("Endpoints");
var endpoints = endpointsSection.Get<EndpointSettings>();
services.Configure<EndpointSettings>(endpointsSection);
services.Configure<StorageSettings>(Configuration.GetSection("Storage"));
services.Configure<NotificationSettings>(Configuration.GetSection("Notifications"));
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = endpoints.Identity;
options.RequireHttpsMetadata = false;
options.ApiName = "dwk-api";
});
services.AddCors(options =>
{
options.AddPolicy("default", policy =>
{
policy.WithOrigins(endpoints.Spa)
.AllowAnyHeader()
.AllowAnyMethod();
});
});
services.AddDbContext<DogWalkerContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
x => x.UseNetTopologySuite()) // enable mapping to spatial types
);
services.AddTransient<IPlaceService, PlaceService>();
services.AddTransient<IProfileService, ProfileService>();
services.AddTransient<IFileService, FileService>();
services.AddTransient<IMeetingService, MeetingService>();
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetService<DogWalkerContext>();
if (dbContext.Database.GetPendingMigrations().Any())
{
dbContext.Database.Migrate();
DbInitializer.Seed(dbContext);
}
}
app.UseCors("default");
app.UseAuthentication();
app.UseMvc();
}
}
}