-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
193 lines (161 loc) · 5.44 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
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
using System.Reflection;
using Asp.Versioning;
using Asp.Versioning.ApiExplorer;
using CityInfo.API;
using CityInfo.API.DbContexts;
using CityInfo.API.Services;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Serilog;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File("logs/cityinfo.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
var builder = WebApplication.CreateBuilder(args);
// Customize Logger
// builder.Logging.ClearProviders();
// builder.Logging.AddConsole();
builder.Host.UseSerilog();
// Add services to the container.
builder.Services.AddControllers(options =>
{
options.ReturnHttpNotAcceptable = true;
})
.AddNewtonsoftJson()
.AddXmlDataContractSerializerFormatters();
// add additional details when returning error status code
builder.Services.AddProblemDetails(options =>
{
options.CustomizeProblemDetails = context =>
{
context.ProblemDetails.Extensions.Add("additionalInfo", "Additional Info Example");
context.ProblemDetails.Extensions.Add("server", Environment.MachineName); // add server name to the error detail
};
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSingleton<FileExtensionContentTypeProvider>();
// Register custom service to container
#if DEBUG
builder.Services.AddTransient<IMailService, LocalMailService>();
#else
builder.Services.AddTransient<IMailService, CloudMailService>();
#endif
builder.Services.AddSingleton<CitiesDataStore>();
// Register DbContext
builder.Services.AddDbContext<CityInfoContext>(dbContextOptions =>
{
dbContextOptions.UseSqlite(builder.Configuration["ConnectionStrings:CityInfoDBConnectionString"]);
});
// add repository
builder.Services.AddScoped<ICityInfoRepository, CityInfoRepository>();
// add automapper
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
// add token authentication
builder.Services.AddAuthentication("Bearer").AddJwtBearer(options =>
{
options.TokenValidationParameters = new ()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Authentication:Issuer"],
ValidAudience = builder.Configuration["Authentication:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Convert.FromBase64String(builder.Configuration["Authentication:SecretForKey"]))
};
});
// Add Authorization Policies
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("MustBeFromParis", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim("City", "Paris");
});
});
// add api versioning
builder.Services.AddApiVersioning(setupAction =>
{
setupAction.ReportApiVersions = true;
setupAction.AssumeDefaultVersionWhenUnspecified = true;
setupAction.DefaultApiVersion = new ApiVersion(1, 0);
}).AddMvc()
.AddApiExplorer(setupAction =>
{
setupAction.SubstituteApiVersionInUrl = true;
});
var apiVersionDescriptionProvider = builder.Services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddSwaggerGen(setupAction =>
{
// Add XML description
foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
{
setupAction.SwaggerDoc($"{description.GroupName}",
new ()
{
Title = "City Info API",
Version = description.ApiVersion.ToString(),
Description = "Through this API you can access cities and their points of interest."
});
}
var xmlCommentsFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlCommentsFullPath = Path.Combine(AppContext.BaseDirectory, xmlCommentsFile);
setupAction.IncludeXmlComments(xmlCommentsFullPath);
// Add token authentication in Swagger
setupAction.AddSecurityDefinition("CityInfoBearerAuth", new ()
{
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
Description = "Input a valid token to access this API"
});
// Use token authentication in Swagger
setupAction.AddSecurityRequirement(new ()
{
{
new ()
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "CityInfoBearerAuth"
}
},
new List<string>()
}
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler();
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(setupAction =>
{
var descriptions = app.DescribeApiVersions();
foreach (var description in descriptions)
{
setupAction.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}
});
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run();
// Always show Hello World no matter the endpoint
// app.Run(async (context) =>
// {
// await context.Response.WriteAsync("Hello World");
// });