11using Microsoft . AspNetCore . Authentication . JwtBearer ;
2+ using Microsoft . EntityFrameworkCore ;
23using Microsoft . IdentityModel . Tokens ;
34using sparkly_server . Enum ;
5+ using sparkly_server . Infrastructure ;
46using sparkly_server . Services . Auth ;
57using sparkly_server . Services . Users ;
68using sparkly_server . Services . UserServices ;
79using System . Text ;
10+ using Scalar . AspNetCore ;
811
912namespace sparkly_server ;
1013
1114public class Program
1215{
1316 public static void Main ( string [ ] args )
1417 {
15-
16- var jwtKey = Environment . GetEnvironmentVariable ( "SPARKLY_JWT_KEY" ) ! ;
17- var jwtIssuer = Environment . GetEnvironmentVariable ( "SPARKLY_JWT_ISSUER" ) ?? "sparkly" ;
18- var jwtAudience = Environment . GetEnvironmentVariable ( "SPARKLY_JWT_AUDIENCE" ) ?? "sparkly-api" ;
19-
2018 var builder = WebApplication . CreateBuilder ( args ) ;
19+
20+
21+ var jwtKey = builder . Configuration [ "SPARKLY_JWT_KEY" ]
22+ ?? Environment . GetEnvironmentVariable ( "SPARKLY_JWT_KEY" )
23+ ?? throw new Exception ( "JWT key missing" ) ;
24+
25+ var jwtIssuer = builder . Configuration [ "SPARKLY_JWT_ISSUER" ]
26+ ?? Environment . GetEnvironmentVariable ( "SPARKLY_JWT_ISSUER" )
27+ ?? "sparkly" ;
28+
29+ var jwtAudience = builder . Configuration [ "SPARKLY_JWT_AUDIENCE" ]
30+ ?? Environment . GetEnvironmentVariable ( "SPARKLY_JWT_AUDIENCE" )
31+ ?? "sparkly-api" ;
2132
2233 builder . Services . AddHttpContextAccessor ( ) ;
2334
@@ -32,6 +43,15 @@ public static void Main(string[] args)
3243 builder . Services . AddScoped < IJwtProvider , JwtProvider > ( ) ;
3344 builder . Services . AddScoped < IAuthService , AuthService > ( ) ;
3445 builder . Services . AddScoped < ICurrentUser , CurrentUser > ( ) ;
46+
47+ var connectionString = builder . Configuration . GetConnectionString ( "Default" )
48+ ?? Environment . GetEnvironmentVariable ( "ConnectionStrings__Default" )
49+ ?? throw new Exception ( "Connection string 'Default' not found." ) ;
50+
51+ builder . Services . AddDbContext < AppDbContext > ( options =>
52+ {
53+ options . UseNpgsql ( connectionString ) ;
54+ } ) ;
3555
3656 builder . Services . AddControllers ( ) ;
3757 // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
@@ -41,15 +61,12 @@ public static void Main(string[] args)
4161 . AddAuthentication ( JwtBearerDefaults . AuthenticationScheme )
4262 . AddJwtBearer ( options =>
4363 {
44- var key = builder . Configuration [ "SPARKLY_JWT_KEY" ]
45- ?? throw new Exception ( "JWT key missing" ) ;
46-
4764 options . TokenValidationParameters = new TokenValidationParameters
4865 {
4966 ValidateIssuer = false ,
5067 ValidateAudience = false ,
5168 ValidateLifetime = true ,
52- IssuerSigningKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( key ) ) ,
69+ IssuerSigningKey = new SymmetricSecurityKey ( Encoding . UTF8 . GetBytes ( jwtKey ) ) ,
5370 ValidateIssuerSigningKey = true ,
5471 ClockSkew = TimeSpan . FromMinutes ( 1 ) ,
5572 } ;
@@ -61,6 +78,8 @@ public static void Main(string[] args)
6178 if ( app . Environment . IsDevelopment ( ) )
6279 {
6380 app . MapOpenApi ( ) ;
81+
82+ app . MapScalarApiReference ( ) ;
6483 }
6584
6685 app . UseHttpsRedirection ( ) ;
@@ -70,6 +89,12 @@ public static void Main(string[] args)
7089
7190 app . MapControllers ( ) ;
7291
92+ using ( var scope = app . Services . CreateScope ( ) )
93+ {
94+ var db = scope . ServiceProvider . GetRequiredService < AppDbContext > ( ) ;
95+ db . Database . Migrate ( ) ;
96+ }
97+
7398 app . Run ( ) ;
7499 }
75100}
0 commit comments