1
|
using System.Text.Json.Serialization;
|
2
|
using Core.Authentication;
|
3
|
using Core.Contexts;
|
4
|
using Core.Seeding;
|
5
|
using Core.Services;
|
6
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
7
|
using Microsoft.EntityFrameworkCore;
|
8
|
using Microsoft.OpenApi.Models;
|
9
|
using RestAPI.Middleware;
|
10
|
using RestAPI.Utils;
|
11
|
using Serilog;
|
12
|
using Core.MapperProfiles;
|
13
|
|
14
|
var builder = WebApplication.CreateBuilder(args);
|
15
|
|
16
|
// Logging
|
17
|
builder.Host.UseSerilog((ctx, lc) => lc.WriteTo.Console());
|
18
|
|
19
|
|
20
|
builder.Services.AddControllers()
|
21
|
.AddJsonOptions(options =>
|
22
|
options.JsonSerializerOptions.Converters.Add(
|
23
|
new JsonStringEnumConverter())); // In OpenAPI (Swagger) the enum item names will be generated (instead of numbers)
|
24
|
|
25
|
// Register our services
|
26
|
Registration.RegisterServices(builder);
|
27
|
|
28
|
// Swagger
|
29
|
builder.Services.AddEndpointsApiExplorer();
|
30
|
builder.Services.AddSwaggerGen(c =>
|
31
|
{
|
32
|
c.SwaggerDoc("v1",
|
33
|
new OpenApiInfo {Title = "AnnotationTool", Description = "KIV/ASWI ZČU Plzeň, 2022", Version = "0.1.1"});
|
34
|
});
|
35
|
|
36
|
// JWT authentication
|
37
|
var tokenValidationParams = JwtUtils.GetTokenValidationParameters(builder.Configuration);
|
38
|
builder.Services.AddSingleton(tokenValidationParams);
|
39
|
builder.Services.Configure<JwtConfig>(builder.Configuration.GetSection("JwtConfig"));
|
40
|
builder.Services.AddAuthentication(options =>
|
41
|
{
|
42
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
43
|
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
44
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
45
|
})
|
46
|
.AddJwtBearer(jwt =>
|
47
|
{
|
48
|
jwt.SaveToken = true;
|
49
|
jwt.TokenValidationParameters = tokenValidationParams;
|
50
|
}
|
51
|
);
|
52
|
|
53
|
builder.Services.AddHttpContextAccessor();
|
54
|
builder.Services.AddScoped<ClientInfo>(provider =>
|
55
|
ContextUtils.GetClientInfo(provider.GetRequiredService<IHttpContextAccessor>().HttpContext));
|
56
|
|
57
|
|
58
|
// Database
|
59
|
builder.Services.AddDbContext<DatabaseContext>();
|
60
|
|
61
|
// AutoMapper
|
62
|
RegisterMappings.Register(builder);
|
63
|
var app = builder.Build();
|
64
|
|
65
|
// Enable DateTime in PostgreSQL
|
66
|
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
67
|
|
68
|
|
69
|
// Configure the HTTP request pipeline.
|
70
|
if (app.Environment.IsDevelopment())
|
71
|
{
|
72
|
app.UseSwagger();
|
73
|
app.UseSwaggerUI();
|
74
|
}
|
75
|
|
76
|
app.UseHttpsRedirection();
|
77
|
|
78
|
// CORS
|
79
|
app.UseCors(x => x.WithOrigins(
|
80
|
"http://localhost",
|
81
|
"https://localhost:3000",
|
82
|
"http://localhost:3000",
|
83
|
"http://localhost:5007")
|
84
|
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
85
|
.AllowAnyHeader()
|
86
|
.AllowAnyMethod()
|
87
|
.AllowCredentials());
|
88
|
|
89
|
// JWT Authentication
|
90
|
app.UseMiddleware<JwtMiddleware>();
|
91
|
|
92
|
// Error handling middleware
|
93
|
app.UseMiddleware<ErrorMiddleware>();
|
94
|
|
95
|
app.MapControllers();
|
96
|
|
97
|
// Logging
|
98
|
app.UseSerilogRequestLogging();
|
99
|
app.Logger.LogInformation("Starting up");
|
100
|
|
101
|
// Database seeding
|
102
|
using (var scope = app.Services.CreateScope())
|
103
|
{
|
104
|
var context = scope.ServiceProvider.GetService<DatabaseContext>();
|
105
|
if (context == null)
|
106
|
{
|
107
|
app.Logger.LogCritical("Could not initialize database context, shutting down.");
|
108
|
Environment.Exit(101);
|
109
|
}
|
110
|
|
111
|
|
112
|
// In development we seed dummy data
|
113
|
if (app.Environment.IsDevelopment())
|
114
|
{
|
115
|
//context.Database.EnsureDeleted();
|
116
|
//context.Database.EnsureCreated();
|
117
|
context.Database.Migrate();
|
118
|
Seeder.SeedDevelopment(context);
|
119
|
}
|
120
|
else
|
121
|
{
|
122
|
// In production we seed administrator
|
123
|
Seeder.SeedProduction(context);
|
124
|
}
|
125
|
}
|
126
|
|
127
|
app.Run();
|