-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
94 lines (76 loc) · 2.87 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
using System.Text.Json;
using FastEndpoints.Swagger;
using Microsoft.AspNetCore.Authentication;
using NSwag;
using Serilog;
using TNRD.Zeepkist.WorkshopApi.Backend.Authentication;
using TNRD.Zeepkist.WorkshopApi.Backend.Extensions;
using TNRD.Zeepkist.WorkshopApi.Backend.Google;
using TNRD.Zeepkist.WorkshopApi.Database;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(options =>
{
options.AddServerHeader = false;
options.AllowSynchronousIO = false;
});
builder.Host.UseConsoleLifetime(options => options.SuppressStatusMessages = true);
builder.Host.UseSerilog((context, configuration) =>
{
configuration
.Enrich.FromLogContext()
.Enrich.WithProperty("Source", "Zworpshop.Api")
.MinimumLevel.Debug()
.WriteTo.Seq(context.Configuration["Seq:Url"], apiKey: context.Configuration["Seq:Key"])
.WriteTo.Console();
});
builder.Services.AddNpgsql<ZworpshopContext>(builder.Configuration["Database:ConnectionString"]);
builder.Services.AddAuthentication(ApiKeyAuthentication.SCHEME)
.AddScheme<AuthenticationSchemeOptions, ApiKeyAuthentication>(ApiKeyAuthentication.SCHEME, null);
builder.Services.AddAuthorization();
builder.Services.AddFastEndpoints(options =>{});
// builder.Services.AddFastEndpoints(options => { options.SourceGeneratorDiscoveredTypes = new Type[] { }; });
builder.Services.SwaggerDocument(o =>
{
o.EnableJWTBearerAuth = false;
o.DocumentSettings = s =>
{
s.Title = "Zworpshop API";
s.AddAuth(ApiKeyAuthentication.SCHEME,
new()
{
Name = ApiKeyAuthentication.HEADER,
In = OpenApiSecurityApiKeyLocation.Header,
Type = OpenApiSecuritySchemeType.ApiKey
});
};
});
builder.Services.Configure<GoogleOptions>(builder.Configuration.GetSection("Google"));
builder.Services.AddSingleton<IUploadService, CloudStorageUploadService>();
builder.Services.AddCors();
WebApplication app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDefaultExceptionHandler();
}
app.UseAuthentication();
app.UseAuthorization();
app.UseFastEndpoints(options =>
{
options.Errors.ResponseBuilder = (errors, _, _) => errors.ToResponse();
options.Errors.StatusCode = StatusCodes.Status422UnprocessableEntity;
options.Serializer.Options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
app.UseCors(policyBuilder => policyBuilder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseOpenApi();
// app.UseSwaggerUi3()
app.UseSwaggerUi(x => x.ConfigureDefaults());
// app.UseSwaggerUi3(x => x.ConfigureDefaults());
using (IServiceScope serviceScope = app.Services.CreateScope())
{
ZworpshopContext workshopApiContext = serviceScope.ServiceProvider.GetRequiredService<ZworpshopContext>();
workshopApiContext.Database.EnsureCreated();
}
await app.RunAsync();