forked from mudroljub/programming-quotes-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
118 lines (107 loc) · 3.99 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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using ProgrammingQuotesApi.Helpers;
using ProgrammingQuotesApi.Services;
using System.IO;
using System;
namespace ProgrammingQuotesApi
{
public class Startup
{
public IConfiguration Configuration { get; }
private readonly string version = "v2";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// called by the runtime, use to configure services
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>();
services.AddCors();
services.AddControllers().AddNewtonsoftJson();
// services.AddMvc()
// .AddJsonOptions(options =>
// {
// options.JsonSerializerOptions.IgnoreNullValues = true;
// });
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(version, new OpenApiInfo
{
Title = "ProgrammingQuotesApi",
Description = "Programming Quotes API for open source projects.",
Contact = new OpenApiContact
{
Name = "Damjan Pavlica",
Email = "[email protected]",
Url = new Uri("https://github.com/mudroljub"),
},
Version = version
});
// generate documentation file
string xmlPath = Path.Combine(AppContext.BaseDirectory, "ProgrammingQuotesApi.xml");
c.IncludeXmlComments(xmlPath);
}).AddSwaggerGenNewtonsoftSupport();
// Dependency Injection
services.AddScoped<UserService>();
services.AddScoped<QuoteService>();
services.AddScoped<AuthorService>();
// authentication
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(Settings.Secret),
ValidateIssuer = false,
ValidateAudience = false
};
});
}
// called by the runtime. use to configure HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger(c =>
{
c.SerializeAsV2 = true;
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"ProgrammingQuotesApi {version}");
c.RoutePrefix = string.Empty;
});
app.UseHttpsRedirection();
app.UseRouting();
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}