-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
59 lines (49 loc) · 1.79 KB
/
Copy pathProgram.cs
File metadata and controls
59 lines (49 loc) · 1.79 KB
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
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Diagnostics;
using Wikipedia.Settings;
using Wikipedia.DTOs;
using Wikipedia.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http.Features;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
// builder.Services.AddOpenApi();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "API", Version = "v1" });
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.Configure<WikipediaApiSettings>(builder.Configuration.GetSection("WikipediaApiSettings"));
builder.Services.AddControllers();
builder.Services.AddHttpClient<IWikipediaService, WikipediaService>();
var app = builder.Build();
app.UseExceptionHandler(appError =>
{
appError.Run(async context =>
{
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
context.Response.ContentType = "application/json";
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature != null)
{
await context.Response.WriteAsJsonAsync(new ApiErrorDto(
$"An unexpected error occurred: {contextFeature.Error.Message}"));
}
});
});
if (app.Environment.IsDevelopment())
{
// app.MapOpenApi();
app.UseStaticFiles();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.InjectStylesheet("/swagger-ui/SwaggerDark.css");
options.RoutePrefix = string.Empty;
});
}
app.UseHttpsRedirection();
app.MapControllers();
app.Run();