-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
153 lines (129 loc) · 5.71 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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AllNewsServer.Configurations;
using AllNewsServer.Data;
using AllNewsServer.Data.Constants;
using AllNewsServer.Data.Models;
using AllNewsServer.Helpers;
using AllNewsServer.Helpers.Swagger;
using AllNewsServer.Services;
using AllNewsServer.Services.Extensions;
using AutoMapper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using Npgsql.EntityFrameworkCore.PostgreSQL;
using Swashbuckle.AspNetCore.Swagger;
namespace AllNewsServer {
public class Startup {
public Startup (IConfiguration configuration) {
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.AddDbContext<ApplicationDbContext> (options =>
options.UseNpgsql (Configuration.GetConnectionString ("DefaultConnection"), x => x.UseNetTopologySuite ()));
services.Configure<MongoSettings> (
Configuration.GetSection (nameof (MongoSettings)));
services.AddSingleton<MongoSettings> (sp =>
sp.GetRequiredService<IOptions<MongoSettings>> ().Value);
services
.AddAuthentication (JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer (options => {
//options.RequireHttpsMetadata = true; // commented out for development
options.TokenValidationParameters = new TokenValidationParameters {
ValidateIssuer = true,
ValidIssuer = AuthConfiguration.TOKEN_ISSUER,
ValidateAudience = true,
ValidAudience = AuthConfiguration.TOKEN_AUDIENCE,
ValidateLifetime = true,
IssuerSigningKey = AuthConfiguration.GetSymmetricSecurityKey (),
ValidateIssuerSigningKey = true
};
});
services.AddAutoMapper ();
services.RegisterServices ();
services.Configure<RequestLocalizationOptions> (
options => {
List<CultureInfo> supportedCultures = TypeHelpers.getTypeValues (typeof (CultureKeys)).Select (x => new CultureInfo (x.Value)).ToList ();
options.DefaultRequestCulture = new RequestCulture (culture: CultureKeys.English, uiCulture: CultureKeys.English);
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
services.AddMvc ()
.AddViewLocalization ()
.AddDataAnnotationsLocalization ()
.SetCompatibilityVersion (CompatibilityVersion.Version_2_2)
.AddJsonOptions (options => {
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
});
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen (c => {
c.SwaggerDoc ("v1", new Info { Title = "AllNews API", Version = "v1" });
c.AddSecurityDefinition ("Bearer", new ApiKeyScheme {
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = "header",
Type = "apiKey"
});
c.AddSecurityRequirement (new Dictionary<string, IEnumerable<string>> { { "Bearer", new string[] { } } });
c.OperationFilter<AcceptLanguageHeaderFilter> ();
var app = Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application;
var xmlPath = System.IO.Path.Combine (app.ApplicationBasePath, "AllNews.Server.xml");
c.IncludeXmlComments (xmlPath);
});
// services.AddLocalization(o =>
// {
// o.ResourcesPath = "Resources";
// });
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure (IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime, IDistributedCache cache) {
if (env.IsDevelopment ()) {
app.UseDeveloperExceptionPage ();
} else {
app.UseExceptionHandler ("/error");
}
app.UseCors (builder => builder
.AllowAnyOrigin ()
.AllowAnyMethod ()
.AllowAnyHeader ()
.AllowCredentials ());
app.UseForwardedHeaders (new ForwardedHeadersOptions {
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseAuthentication ();
app.UseRequestLocalization ();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger ();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI (c => {
c.SwaggerEndpoint ("/swagger/v1/swagger.json", "AllNews API V1");
c.RoutePrefix = "swagger";
});
app.UseMvc ();
}
}
}