-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
228 lines (194 loc) · 9.37 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using AspNet.Security.OAuth.GitHub;
using AspNet.Security.OAuth.LinkedIn;
using AspNetCoreSpa.Server;
using AspNetCoreSpa.Server.Entities;
using AspNetCoreSpa.Server.Repositories;
using AspNetCoreSpa.Server.Repositories.Abstract;
using AspNetCoreSpa.Server.Services;
using AspNetCoreSpa.Server.Services.Abstract;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Serialization;
using Serilog;
using Serilog.Events;
using Swashbuckle.Swagger.Model;
namespace AspNetCoreSpa
{
public class Startup
{
// Order or run
//1) Constructor
//2) Configure services
//3) Configure
private IHostingEnvironment _hostingEnv;
public Startup(IHostingEnvironment env)
{
_hostingEnv = env;
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.MinimumLevel
.Information()
// .WriteTo.RollingFile("log-{Date}.txt", LogEventLevel.Information) // Uncomment if logging required on text file
.WriteTo.Seq("http://localhost:5341/")
.CreateLogger();
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
Configuration = builder.Build();
}
public static IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// New instance every time, only configuration class needs so its ok
services.AddScoped<ILoggingRepository, LoggingRepository>();
services.AddTransient<IEmailSender, EmailSender>();
services.AddTransient<ISmsSender, SmsSender>();
services.AddTransient<SeedDbData>();
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
services.AddSwaggerGen();
// Add the detail information for the API.
services.ConfigureSwaggerGen(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "AspNetCoreSpa Api",
Description = "Here is the api used in this application of different functionality",
TermsOfService = "None",
Contact = new Contact { Name = "Asad Sahi", Email = "", Url = "http://twitter.com/asadsahi" },
License = new License { Name = "Use under MIT", Url = "https://opensource.org/licenses/MIT" }
});
// //Determine base path for the application.
// var basePath = PlatformServices.Default.Application.ApplicationBasePath;
// //Set the comments path for the swagger json and ui.
// options.IncludeXmlComments(basePath + "\\TodoApi.xml");
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public async void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IAntiforgery antiforgery, SeedDbData seedData)
{
if (env.IsDevelopment())
{
loggerFactory.AddSerilog();
// loggerFactory.AddConsole(Configuration.GetSection("Logging"));
// loggerFactory.AddDebug();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true,
ConfigFile = "config/webpack.config.js"
});
// NOTE: For SPA swagger needs adding before MVC
// Enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger();
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
app.UseSwaggerUi();
}
else
{
// For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
try
{
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
serviceScope.ServiceProvider.GetService<ApplicationDbContext>().Database.Migrate();
}
}
catch (Exception) { }
}
// Configure XSRF middleware, This pattern is for SPA style applications where XSRF token is added on Index page
// load and passed back token on every subsequent async request
app.Use(async (context, next) =>
{
if (string.Equals(context.Request.Path.Value, "/", StringComparison.OrdinalIgnoreCase))
{
var tokens = antiforgery.GetAndStoreTokens(context);
context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions() { HttpOnly = false });
}
await next.Invoke();
});
app.UseStaticFiles();
app.UseIdentity();
// Facebook Auth
// app.UseFacebookAuthentication(new FacebookOptions()
// {
// AppId = Configuration["Authentication:Facebook:AppId"],
// AppSecret = Configuration["Authentication:Facebook:AppSecret"]
// });
// // Google Auth
// app.UseGoogleAuthentication(new GoogleOptions()
// {
// ClientId = Configuration["Authentication:Google:ClientId"],
// ClientSecret = Configuration["Authentication:Google:ClientSecret"]
// });
// // Twitter Auth
// // https://apps.twitter.com/
// app.UseTwitterAuthentication(new TwitterOptions()
// {
// ConsumerKey = Configuration["Authentication:Twitter:ConsumerKey"],
// ConsumerSecret = Configuration["Authentication:Twitter:ConsumerSecret"]
// });
// // Microsoft Auth
// // https://apps.dev.microsoft.com/?mkt=en-us#/appList
// app.UseMicrosoftAccountAuthentication(new MicrosoftAccountOptions()
// {
// ClientId = Configuration["Authentication:Microsoft:ClientId"],
// ClientSecret = Configuration["Authentication:Microsoft:ClientSecret"]
// });
// // Note: Below social providers are supported through this open source library:
// // https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
// // Github Auth
// // https://github.com/settings/developers
// app.UseGitHubAuthentication(new GitHubAuthenticationOptions
// {
// ClientId = Configuration["Authentication:Github:ClientId"],
// ClientSecret = Configuration["Authentication:Github:ClientSecret"]
// });
// app.UseLinkedInAuthentication(new LinkedInAuthenticationOptions
// {
// ClientId = Configuration["Authentication:LinkedIn:ClientId"],
// ClientSecret = Configuration["Authentication:LinkedIn:ClientSecret"]
// });
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
await seedData.EnsureSeedDataAsync();
}
}
}