-
Notifications
You must be signed in to change notification settings - Fork 28
/
Startup.cs
178 lines (146 loc) · 6.13 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
using System;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Cors.Infrastructure;
using Microsoft.AspNet.Hosting;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Serialization;
using Swashbuckle.SwaggerGen;
using Swashbuckle.SwaggerGen.XmlComments;
namespace CustomerWebApi
{
public class Startup
{
private const string CORS_POLICY_NAME = "allowAll";
private IHostingEnvironment _hostingEnvironment;
public Startup(IHostingEnvironment env)
{
_hostingEnvironment = env;
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddWebEncoders();
// Configure CORS
ConfigureCors(services);
// Configure Web API
ConfigureMvc(services);
// Configure DI
ConfigureDI(services);
// Configure Database & EntityFramework
ConfigureDatabase(services);
// Configure Swagger API documentation
ConfigureSwaggerApiDocumentation(services);
}
private void ConfigureSwaggerApiDocumentation(IServiceCollection services)
{
string pathToXmlDoc = Path.Combine(_hostingEnvironment.WebRootPath, "bin", "Debug", "dnxcore50", "WebAPI.xml");
Console.WriteLine(pathToXmlDoc);
if (!File.Exists(pathToXmlDoc))
{
pathToXmlDoc = String.Empty;
}
services.AddSwaggerGen();
services.ConfigureSwaggerDocument(options =>
{
options.SingleApiVersion(new Info()
{
Version = "v1",
Title = "Sample ASP.NET Core 1.0 API",
Description = "Sample Web API to show case ASP.NET Core 1.0 compared to Node.js",
Contact = new Contact()
{
Name = "Thinktecture AG",
Email = "[email protected]",
Url = "http://thinktecture.com"
}
});
if (!String.IsNullOrWhiteSpace(pathToXmlDoc))
{
options.OperationFilter(new ApplyXmlActionComments(pathToXmlDoc));
}
});
services.ConfigureSwaggerSchema(options =>
{
if (!String.IsNullOrWhiteSpace(pathToXmlDoc))
{
options.ModelFilter(new ApplyXmlTypeComments(pathToXmlDoc));
}
});
}
private void ConfigureDatabase(IServiceCollection services)
{
// Configures EntityFramework with PostgreSQL
services.AddEntityFramework()
.AddNpgsql()
.AddDbContext<CustomerContext>(options =>
options.UseNpgsql("Server=127.0.0.1;Port=5432;Database=CustomerSampleVNext;User Id=CustomerSample;Password=CustomerSample;"));
}
private void ConfigureDI(IServiceCollection services)
{
// Either use this or the other customer service by switching the comments
//services.AddSingleton<ICustomerService, InMemoryCustomerService>();
services.AddSingleton<ICustomerService, DatabaseCustomerService>();
}
private void ConfigureCors(IServiceCollection services)
{
// For this demo allow everything so we don't have to hastle around
var corsBuilder = new CorsPolicyBuilder();
corsBuilder.AllowAnyHeader();
corsBuilder.AllowAnyMethod();
corsBuilder.AllowAnyOrigin();
corsBuilder.AllowCredentials();
services.AddCors(options =>
{
options.AddPolicy(CORS_POLICY_NAME, corsBuilder.Build());
});
}
private void ConfigureMvc(IServiceCollection services)
{
var mvcCore = services.AddMvcCore();
mvcCore.AddApiExplorer();
mvcCore.AddAuthorization();
mvcCore.AddFormatterMappings();
// Razor is only needed for token things.
mvcCore.AddRazorViewEngine();
mvcCore.AddDataAnnotations();
mvcCore.AddJsonFormatters(options => options.ContractResolver = new CamelCasePropertyNamesContractResolver());
}
// 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)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseCors(CORS_POLICY_NAME);
app.UseIISPlatformHandler();
UseIdentityServerSecurity(app);
app.UseMvc();
app.UseSwaggerGen();
app.UseSwaggerUi(baseRoute: "docs");
}
private void UseIdentityServerSecurity(IApplicationBuilder app)
{
JwtSecurityTokenHandler.DefaultInboundClaimFilter.Clear();
app.UseIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5001/";
options.ScopeName = "api";
options.ScopeSecret = "apisecret";
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
});
}
// Entry point for the application.
public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
}
}