Projekt

Obecné

Profil

Stáhnout (3.42 KB) Statistiky
| Větev: | Tag: | Revize:
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

    
37
builder.Services.AddCors();
38

    
39
// 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
builder.Services.AddScoped<ClientInfo>(provider =>
58
    ContextUtils.GetClientInfo(provider.GetRequiredService<IHttpContextAccessor>().HttpContext));
59

    
60

    
61
// Database
62
builder.Services.AddDbContext<DatabaseContext>();
63

    
64
// AutoMapper
65
RegisterMappings.Register(builder);
66
var app = builder.Build();
67

    
68
// Enable DateTime in PostgreSQL
69
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
70

    
71

    
72
// Configure the HTTP request pipeline.
73
if (app.Environment.IsDevelopment())
74
{
75
    app.UseSwagger();
76
    app.UseSwaggerUI();
77
}
78

    
79
//app.UseHttpsRedirection();
80

    
81
// CORS
82
app.UseCors(builder => builder
83
                .AllowAnyHeader()
84
                .AllowAnyMethod()
85
                .SetIsOriginAllowed(_ => true)
86
                .AllowCredentials()
87
            );
88

    
89

    
90
// JWT Authentication
91
app.UseMiddleware<JwtMiddleware>();
92

    
93
// Error handling middleware
94
app.UseMiddleware<ErrorMiddleware>();
95

    
96
app.MapControllers();
97

    
98
// Logging
99
app.UseSerilogRequestLogging();
100
app.Logger.LogInformation("Starting up");
101

    
102
// Database seeding
103
using (var scope = app.Services.CreateScope())
104
{
105
    var context = scope.ServiceProvider.GetService<DatabaseContext>();
106
    if (context == null)
107
    {
108
        app.Logger.LogCritical("Could not initialize database context, shutting down.");
109
        Environment.Exit(101);
110
    }
111

    
112

    
113
    // In development we seed dummy data
114
    if (app.Environment.IsDevelopment())
115
    {
116
        //context.Database.EnsureDeleted();
117
        //context.Database.EnsureCreated();
118
        //context.Database.Migrate();
119
        Seeder.SeedDevelopment(context);
120
    }
121
    else
122
    {
123
        // In production we seed administrator 
124
        //context.Database.Migrate();
125
        Seeder.SeedProduction(context);
126
    }
127
}
128

    
129
app.Run();
(3-3/6)