-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (76 loc) · 3.08 KB
/
Program.cs
File metadata and controls
106 lines (76 loc) · 3.08 KB
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
103
104
105
106
using DotnetServer.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Npgsql;
using Dapper;
using DotNetEnv;
using DotnetServer.Repositories;
using DotnetServer.Services;
using DotnetServer.Controllers;
// Load the environment to get variables
Env.Load();
// Fetch environment variables for database configuration
var host = Environment.GetEnvironmentVariable("DATABASE_HOST");
var dbName = Environment.GetEnvironmentVariable("DATABASE_NAME");
var dbUser = Environment.GetEnvironmentVariable("DATABASE_USER");
var dbPassword = Environment.GetEnvironmentVariable("DATABASE_PASSWORD");
// Fetch environment variables for SMTP Gmail sending configuration
var smtpServer = Environment.GetEnvironmentVariable("SMTP_SERVER");
var smtpPort = int.Parse(Environment.GetEnvironmentVariable("SMTP_PORT"));
var smtpUser = Environment.GetEnvironmentVariable("SMTP_USER");
var smtpPassword = Environment.GetEnvironmentVariable("SMTP_PASSWORD");
var smtpFromEmail = Environment.GetEnvironmentVariable("SMTP_FROM_EMAIL");
// Fetch JSON Web Token from environment variables
var jwtSecret = Environment.GetEnvironmentVariable("JWT_SECRET");
// Fetch the fronted url from environment variables
var frontendUrl = Environment.GetEnvironmentVariable("FRONTEND_URL");
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// CORS setup
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.WithOrigins(frontendUrl)
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// Register EmailSender and pass configuration from .env
builder.Services.AddScoped(provider =>
new EmailSender(smtpServer, smtpPort, smtpUser, smtpPassword, smtpFromEmail, frontendUrl));
// Register TokenGenerator and pass configuration from .env
builder.Services.AddScoped(provider =>
new TokenGenerator(jwtSecret, 432000));
// Register the AuthenticationRepository and inject the EmailSender
var connectionString = $"Host={host};Database={dbName};Username={dbUser};Password={dbPassword}";
builder.Services.AddScoped<IDemoAuthRepository>(provider =>
new DemoAuthRepository(
connectionString,
provider.GetRequiredService<EmailSender>()
));
builder.Services.AddScoped<IAuthenticationRepository>(provider =>
new AuthenticationRepository(
connectionString,
provider.GetRequiredService<EmailSender>()
));
builder.Services.AddScoped<IApiKeysRepository>(provider =>
new ApiKeysRepository(
connectionString
));
var app = builder.Build();
app.UseCors();
// Use custom middleware for logging requests
app.UseMiddleware<DotnetServer.Middleware.RequestLoggingMiddleware>();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
// app.UseHttpsRedirection();
app.UseAuthorization();
// Map controllers to routes
app.MapControllers();
app.Run();