-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
87 lines (72 loc) · 2.63 KB
/
Program.cs
File metadata and controls
87 lines (72 loc) · 2.63 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
using System.Reflection;
using Microsoft.OpenApi.Models;
using MySqlConnector;
using SurfTimer.Api.Middleware;
using SurfTimer.Shared.Data;
using SurfTimer.Shared.Data.MySql;
using SurfTimer.Shared.JsonConverters;
var assembly = Assembly.GetExecutingAssembly();
var title = assembly.GetName().Name;
var version = assembly.GetName().Version?.ToString() ?? "6.6.6";
// Environment Variables
var dbHost =
Environment.GetEnvironmentVariable("DB_HOST")
?? throw new InvalidOperationException("DB_HOST is not set.");
var dbPort = Environment.GetEnvironmentVariable("DB_PORT") ?? "3306";
var dbUser =
Environment.GetEnvironmentVariable("DB_USER")
?? throw new InvalidOperationException("DB_USER is not set.");
var dbPassword =
Environment.GetEnvironmentVariable("DB_PASSWORD")
?? throw new InvalidOperationException("DB_PASSWORD is not set.");
var dbName =
Environment.GetEnvironmentVariable("DB_NAME")
?? throw new InvalidOperationException("DB_NAME is not set.");
var connectionString =
$"Server={dbHost};Port={dbPort};User={dbUser};Password={dbPassword};Database={dbName};Allow User Variables=true;";
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddMySqlDataSource(connectionString);
// IDbConnectionFactory -> using the already registered MySqlDataSource
builder.Services.AddSingleton<IDbConnectionFactory>(sp =>
{
var ds = sp.GetRequiredService<MySqlDataSource>();
return new MySqlDataSourceConnectionFactory(ds);
});
// User the SurfTimer.Shared Dapper integration
builder.Services.AddScoped<SurfTimer.Shared.Data.IDatabaseService, DapperDatabaseService>();
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc(
"v1",
new OpenApiInfo
{
Title = title,
Version = version,
Description = "by [tslashd](https://github.com/tslashd)",
}
);
});
builder
.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new ReplayFramesStringConverter());
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Dapper bootstrap (snake_case mapping + type handlers)
DapperBootstrapper.Init();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment() || app.Environment.IsProduction())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseMiddleware<RequestTimingMiddleware>();
app.UseAuthorization();
app.MapControllers();
await app.RunAsync();