-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
102 lines (81 loc) · 2.78 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
using Microsoft.EntityFrameworkCore;
using UrlSh.Data;
using UrlSh.Data.Models;
using Sqids;
using MySql.Data.MySqlClient;
using UrlSh;
using Microsoft.AspNetCore.HttpOverrides;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var connectionStringBuilder = new MySqlConnectionStringBuilder(builder.Configuration.GetConnectionString("UrlSh"));
var dbName = builder.Configuration.GetValue<string>("DB_NAME");
if (!String.IsNullOrEmpty(dbName))
{
connectionStringBuilder.Database = dbName;
}
var dbHost = builder.Configuration.GetValue<string>("DB_HOST");
if(!String.IsNullOrEmpty(dbHost))
{
connectionStringBuilder.Server = dbHost;
}
var dbPort = builder.Configuration.GetValue<uint?>("DB_PORT");
if(dbPort.HasValue)
{
connectionStringBuilder.Port = dbPort.Value;
}
var dbUser = builder.Configuration.GetValue<string>("DB_USER");
if (!String.IsNullOrEmpty(dbUser))
{
connectionStringBuilder.UserID = dbUser;
}
var dbPassword = builder.Configuration.GetValue<string>("DB_PASSWORD");
if (!String.IsNullOrEmpty(dbPassword))
{
connectionStringBuilder.Password = dbPassword;
}
var connectionString = connectionStringBuilder.ConnectionString;
if(String.IsNullOrEmpty(connectionString))
{
throw new InvalidOperationException("Unable to create connection string.");
}
builder.Services.AddDbContext<UrlShContext>(options =>
options.UseMySQL(connectionString));
builder.Services.AddTransient<SqidsService>();
builder.Services.AddHostedService<Worker>();
var app = builder.Build();
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/{code}", async (HttpContext context, string code, UrlShContext urlShContext, CancellationToken ct) =>
{
var redirect = await urlShContext.Redirects.SingleOrDefaultAsync(t => t.Code == code, ct);
if(redirect == null)
{
return Results.NotFound();
}
var redirectLog = new RedirectLog()
{
RedirectId = redirect.Id,
IPAddress = context.Connection.RemoteIpAddress?.ToString(),
Referer = context.Request.Headers["Referer"],
UserAgent = context.Request.Headers["User-Agent"]
};
urlShContext.RedirectsLogs.Add(redirectLog);
await urlShContext.SaveChangesAsync(ct);
return Results.Redirect(redirect.Url.AbsoluteUri, false, false);
})
.WithName("Redirect");
await using var scope = app.Services.CreateAsyncScope();
await using var context = scope.ServiceProvider.GetRequiredService<UrlShContext>();
{
app.Logger.LogInformation("Migrating database to latest version...");
await context.Database.MigrateAsync();
}
app.Run();