Projekt

Obecné

Profil

Stáhnout (3.44 KB) Statistiky
| Větev: | Tag: | Revize:
1 20f7b1c9 Lukáš Vlček
using System.Text.Json.Serialization;
2 c349bab3 Lukáš Vlček
using Core.Authentication;
3 caa28567 Vojtěch Bartička
using Core.Contexts;
4 ebb51a7f Vojtěch Bartička
using Core.Seeding;
5 897851f8 Vojtěch Bartička
using Core.Services;
6 c349bab3 Lukáš Vlček
using Microsoft.AspNetCore.Authentication.JwtBearer;
7
using Microsoft.EntityFrameworkCore;
8 20f7b1c9 Lukáš Vlček
using Microsoft.OpenApi.Models;
9 c349bab3 Lukáš Vlček
using RestAPI.Middleware;
10 3a6c189b Lukáš Vlček
using RestAPI.Utils;
11 68d7a219 Vojtěch Bartička
using Serilog;
12 f2249f10 Vojtěch Bartička
using Core.MapperProfiles;
13 ebb51a7f Vojtěch Bartička
14 68d7a219 Vojtěch Bartička
var builder = WebApplication.CreateBuilder(args);
15
16
// Logging
17
builder.Host.UseSerilog((ctx, lc) => lc.WriteTo.Console());
18
19 20f7b1c9 Lukáš Vlček
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 68d7a219 Vojtěch Bartička
25 897851f8 Vojtěch Bartička
// Register our services
26
Registration.RegisterServices(builder);
27
28 68d7a219 Vojtěch Bartička
// Swagger
29
builder.Services.AddEndpointsApiExplorer();
30 20f7b1c9 Lukáš Vlček
builder.Services.AddSwaggerGen(c =>
31
{
32
    c.SwaggerDoc("v1",
33 3a6c189b Lukáš Vlček
        new OpenApiInfo {Title = "AnnotationTool", Description = "KIV/ASWI ZČU Plzeň, 2022", Version = "0.1.1"});
34 20f7b1c9 Lukáš Vlček
});
35 68d7a219 Vojtěch Bartička
36 88d5dda9 Jaroslav Hrubý
37
builder.Services.AddCors();
38
39 c349bab3 Lukáš Vlček
// JWT authentication
40
var tokenValidationParams = JwtUtils.GetTokenValidationParameters(builder.Configuration);
41
builder.Services.AddSingleton(tokenValidationParams);
42
builder.Services.Configure<JwtConfig>(builder.Configuration.GetSection("JwtConfig"));
43
builder.Services.AddAuthentication(options =>
44
    {
45
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
46
        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
47
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
48
    })
49
    .AddJwtBearer(jwt =>
50
        {
51
            jwt.SaveToken = true;
52
            jwt.TokenValidationParameters = tokenValidationParams;
53
        }
54
    );
55
56
builder.Services.AddHttpContextAccessor();
57 3a6c189b Lukáš Vlček
builder.Services.AddScoped<ClientInfo>(provider =>
58
    ContextUtils.GetClientInfo(provider.GetRequiredService<IHttpContextAccessor>().HttpContext));
59 c349bab3 Lukáš Vlček
60 e37f5a26 Vojtěch Bartička
61 fc0e5b36 Vojtěch Bartička
// Database
62 ebb51a7f Vojtěch Bartička
builder.Services.AddDbContext<DatabaseContext>();
63 caa28567 Vojtěch Bartička
64 f2249f10 Vojtěch Bartička
// AutoMapper
65
RegisterMappings.Register(builder);
66 68d7a219 Vojtěch Bartička
67 21eac145 Lukáš Vlček
68 b6e24252 Lukáš Vlček
var app = builder.Build();
69
70
app.UsePathBase(builder.Configuration["BasePath"]);
71 21eac145 Lukáš Vlček
72 fc0e5b36 Vojtěch Bartička
// Enable DateTime in PostgreSQL
73 caa28567 Vojtěch Bartička
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
74
75
76 68d7a219 Vojtěch Bartička
// Configure the HTTP request pipeline.
77
if (app.Environment.IsDevelopment())
78
{
79
    app.UseSwagger();
80
    app.UseSwaggerUI();
81
}
82
83 29733de8 Lukáš Vlček
// CORS
84 88d5dda9 Jaroslav Hrubý
app.UseCors(builder => builder
85 21eac145 Lukáš Vlček
    .AllowAnyHeader()
86
    .AllowAnyMethod()
87
    .SetIsOriginAllowed(_ => true)
88
    .AllowCredentials()
89
);
90 88d5dda9 Jaroslav Hrubý
91 21eac145 Lukáš Vlček
app.UseRouting();
92 29733de8 Lukáš Vlček
93 c349bab3 Lukáš Vlček
// JWT Authentication
94
app.UseMiddleware<JwtMiddleware>();
95
96 e37f5a26 Vojtěch Bartička
// Error handling middleware
97
app.UseMiddleware<ErrorMiddleware>();
98 68d7a219 Vojtěch Bartička
99 21eac145 Lukáš Vlček
app.MapGet("/", () => "It works!");
100
101 68d7a219 Vojtěch Bartička
app.MapControllers();
102
103
// Logging
104
app.UseSerilogRequestLogging();
105 7f274190 Jaroslav Hrubý
app.Logger.LogInformation("Starting up");
106 68d7a219 Vojtěch Bartička
107 21eac145 Lukáš Vlček
108 ebb51a7f Vojtěch Bartička
// Database seeding
109
using (var scope = app.Services.CreateScope())
110
{
111
    var context = scope.ServiceProvider.GetService<DatabaseContext>();
112 fc0e5b36 Vojtěch Bartička
    if (context == null)
113
    {
114
        app.Logger.LogCritical("Could not initialize database context, shutting down.");
115
        Environment.Exit(101);
116
    }
117 ebb51a7f Vojtěch Bartička
118 a0d78c9b Lukáš Vlček
119 ebb51a7f Vojtěch Bartička
    // In development we seed dummy data
120
    if (app.Environment.IsDevelopment())
121
    {
122 be4deff8 Vojtěch Bartička
        //context.Database.EnsureDeleted();
123
        //context.Database.EnsureCreated();
124 5f000cb0 Vojtěch Bartička
        context.Database.Migrate();
125 ebb51a7f Vojtěch Bartička
        Seeder.SeedDevelopment(context);
126
    }
127
    else
128
    {
129
        // In production we seed administrator 
130 5f000cb0 Vojtěch Bartička
        context.Database.Migrate();
131 ebb51a7f Vojtěch Bartička
        Seeder.SeedProduction(context);
132
    }
133
}
134
135 c349bab3 Lukáš Vlček
app.Run();