-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
102 lines (99 loc) · 4.04 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
95
96
97
98
99
100
101
102
global using RPG_dotnet.Models;
global using RPG_dotnet.Services.CharactersService;
global using RPG_dotnet.Dtos.Characters;
global using AutoMapper;
global using RPG_dotnet.Middleware;
global using System.Net;
global using Newtonsoft.Json;
global using System.Data.SqlClient;
global using Serilog;
global using Serilog.Formatting.Elasticsearch;
global using Microsoft.EntityFrameworkCore;
global using RPG_dotnet.Data;
global using Microsoft.AspNetCore.Mvc;
global using FluentValidation;
global using RPG_dotnet.Validations;
global using FluentValidation.Results;
global using Microsoft.AspNetCore.Mvc.ModelBinding;
global using FluentValidation.AspNetCore;
global using RPG_dotnet.Helpers;
global using System.ComponentModel.DataAnnotations;
global using System.ComponentModel.DataAnnotations.Schema;
global using RPG_dotnet.Dtos.User;
global using System.Security.Claims;
global using Microsoft.IdentityModel.Tokens;
global using System.IdentityModel.Tokens.Jwt;
global using Microsoft.AspNetCore.Authentication.JwtBearer;
global using Microsoft.AspNetCore.Authorization;
global using Swashbuckle.AspNetCore.Filters;
global using RPG_dotnet.Services.TeamService;
global using Microsoft.AspNetCore.Mvc.Filters;
global using RPG_dotnet.Controllers;
using Microsoft.OpenApi.Models;
using dotenv.net;
var builder = WebApplication.CreateBuilder(args);
DotEnv.AutoConfig();
string connString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING");
string token = Environment.GetEnvironmentVariable("TOKEN");
if(connString is null || token is null)
throw new NotFoundException("Missing environment variables");
// Add services to the container.
builder.Services.AddDbContext<DataContext>(options =>
options.UseSqlServer(connString));
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c=>{
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme{
Description = """Standard Authorization header using the Bearer scheme. Example: "bearer {token}" """,
In= ParameterLocation.Header,
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
c.OperationFilter<SecurityRequirementsOperationFilter>();
});
builder.Services.AddAutoMapper(typeof(Program).Assembly);
builder.Services.AddScoped<ICharacterService, CharacterService>();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<ITeamService, TeamService>();
builder.Services.AddScoped<SetUserIdFilterAttribute>();
builder.Services.AddScoped<AllowUnauthenticatedAttribute>();
builder.Services.AddValidatorsFromAssemblyContaining<CreateCharacterReqValidator>();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options=>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8
.GetBytes(token)),
ValidateIssuer = false,
ValidateAudience = false
};
options.Events = new JwtBearerEvents
{
OnChallenge = async context =>
{
await Task.Run(() => throw new AuthException());
}
};
});
// TODO: implement logic to write error logs to file
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console(new ElasticsearchJsonFormatter(), restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Warning)
.CreateLogger();
builder.Logging.AddSerilog();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseMiddleware<ExceptionHandlingMiddleware>();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();