-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·83 lines (68 loc) · 2.62 KB
/
Program.cs
File metadata and controls
executable file
·83 lines (68 loc) · 2.62 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Reflection;
using Microsoft.OpenApi.Models;
using PowerplantCodingChallenge.Application.Interfaces;
using PowerplantCodingChallenge.Application.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
builder.Services.AddScoped<IProductionPlanService, ProductionPlanService>();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Powerplant Coding Challenge API",
Description = "REST API to calculate the optimal powerplant production plan based on the requested load and the merit-order",
Contact = new OpenApiContact
{
Name = "SPaaS Team - GEM ENGIE",
Url = new Uri("https://github.com/gem-spaas/powerplant-coding-challenge")
}
});
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFilename);
options.IncludeXmlComments(xmlPath);
});
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenAnyIP(8888);
});
var app = builder.Build();
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
context.Response.ContentType = "application/json";
var error = context.Features.Get<Microsoft.AspNetCore.Diagnostics.IExceptionHandlerFeature>();
if (error != null)
{
var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
logger.LogError(error.Error, "Unhandled exception occurred");
await context.Response.WriteAsJsonAsync(new
{
error = "An error occurred while processing your request.",
details = app.Environment.IsDevelopment() ? error.Error.Message : null
});
}
});
});
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapControllers();
app.Run();