Projekt

Obecné

Profil

Stáhnout (3.37 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 AutoMapper;
13
using Core.MapperProfiles;
14

    
15
var builder = WebApplication.CreateBuilder(args);
16

    
17
// Logging
18
builder.Host.UseSerilog((ctx, lc) => lc.WriteTo.Console());
19

    
20

    
21
builder.Services.AddControllers()
22
    .AddJsonOptions(options =>
23
        options.JsonSerializerOptions.Converters.Add(
24
            new JsonStringEnumConverter())); // In OpenAPI (Swagger) the enum item names will be generated (instead of numbers)
25

    
26
// Register our services
27
Registration.RegisterServices(builder);
28

    
29
// Swagger
30
builder.Services.AddEndpointsApiExplorer();
31
builder.Services.AddSwaggerGen(c =>
32
{
33
    c.SwaggerDoc("v1",
34
        new OpenApiInfo {Title = "AnnotationTool", Description = "KIV/ASWI ZČU Plzeň, 2022", Version = "0.1.1"});
35
});
36

    
37
// JWT authentication
38
var tokenValidationParams = JwtUtils.GetTokenValidationParameters(builder.Configuration);
39
builder.Services.AddSingleton(tokenValidationParams);
40
builder.Services.Configure<JwtConfig>(builder.Configuration.GetSection("JwtConfig"));
41
builder.Services.AddAuthentication(options =>
42
    {
43
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
44
        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
45
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
46
    })
47
    .AddJwtBearer(jwt =>
48
        {
49
            jwt.SaveToken = true;
50
            jwt.TokenValidationParameters = tokenValidationParams;
51
        }
52
    );
53

    
54
builder.Services.AddHttpContextAccessor();
55
builder.Services.AddScoped<ClientInfo>(provider =>
56
    ContextUtils.GetClientInfo(provider.GetRequiredService<IHttpContextAccessor>().HttpContext));
57

    
58

    
59
// Database
60
builder.Services.AddDbContext<DatabaseContext>();
61

    
62
// AutoMapper
63
RegisterMappings.Register(builder);
64
var app = builder.Build();
65

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

    
69

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

    
77
app.UseHttpsRedirection();
78

    
79
// CORS
80
app.UseCors(x => x.WithOrigins(
81
        "http://localhost",
82
        "https://localhost:3000",
83
        "http://localhost:3000",
84
        "http://localhost:5007")
85
    .SetIsOriginAllowedToAllowWildcardSubdomains()
86
    .AllowAnyHeader()
87
    .AllowAnyMethod()
88
    .AllowCredentials());
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.Migrate();
117
        Seeder.SeedDevelopment(context);
118
    }
119
    else
120
    {
121
        // In production we seed administrator 
122
        Seeder.SeedProduction(context);
123
    }
124
}
125

    
126
app.Run();
(2-2/5)