-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
118 lines (97 loc) · 3.4 KB
/
Program.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 Blog.Data;
using Blog.Services;
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.ResponseCompression;
using System.IO.Compression;
using Microsoft.EntityFrameworkCore;
namespace Blog;
public static class Program {
public static void Main(string[] args) {
var builder = WebApplication.CreateBuilder(args);
ConfigureAuthentication(builder);
ConfigureMvc(builder);
ConfigureServices(builder);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
LoadConfiguration(app);
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.UseResponseCompression();
app.MapControllers();
if (app.Environment.IsDevelopment()) {
Console.WriteLine("app in development");
app.UseHttpsRedirection();
app.UseSwagger();
}
app.Run();
}
private static void LoadConfiguration(WebApplication app) {
Configuration.JwtKey = app.Configuration.GetValue<string>("JwtKey");
Configuration.ApiKeyName = app.Configuration.GetValue<string>("ApiKeyName");
Configuration.ApiKey = app.Configuration.GetValue<string>("ApiKey");
var smtp = new SmptConfiguration();
app.Configuration.GetSection("Smtp").Bind(smtp);
Configuration.Smtp = smtp;
}
private static void ConfigureAuthentication(WebApplicationBuilder builder) {
var key = Encoding.ASCII.GetBytes(Configuration.JwtKey);
builder
.Services
.AddAuthentication(options => {
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters {
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
}
private static void ConfigureMvc(WebApplicationBuilder builder) {
builder
.Services
.AddControllers()
.ConfigureApiBehaviorOptions(options => {
options.SuppressModelStateInvalidFilter = true;
})
.AddJsonOptions(options => {
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault;
});
builder
.Services
.AddResponseCompression(options => {
options.Providers.Add<GzipCompressionProvider>();
});
builder
.Services
.Configure<GzipCompressionProviderOptions>(options => {
options.Level = CompressionLevel.Optimal;
});
builder
.Services
.AddMemoryCache();
}
private static void ConfigureServices(WebApplicationBuilder builder) {
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder
.Services
.AddDbContext<BlogDataContext>(options => {
options.UseSqlServer(connectionString);
});
builder
.Services
.AddTransient<TokenService>();
builder
.Services
.AddTransient<EmailService>();
}
}