-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
92 lines (73 loc) · 2.58 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
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.EntityFrameworkCore;
using UrlShortener;
using UrlShortener.Entities;
using UrlShortener.Extensions;
using UrlShortener.Models;
using UrlShortener.Repositories;
using UrlShortener.Repositories.Interfaces;
using UrlShortener.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<ApplicationDbContext>(o =>
o.UseNpgsql(builder.Configuration.GetConnectionString("Database")));
builder.Services.AddMemoryCache();
builder.Services.AddScoped<UrlShorteningService>();
builder.Services.AddScoped<ShortenedUrlRepository>();
builder.Services.AddScoped<IShortenedUrlRepository, CachedShortenedUrlRepository>();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.ApplyMigrations();
app.MapPost("api/shorten", async (
ShortenUrlRequest request,
UrlShorteningService urlShorteningService,
ApplicationDbContext dbContext,
HttpContext httpContext) =>
{
if (!Uri.TryCreate(request.Url, UriKind.Absolute, out _))
{
return Results.BadRequest("The specified URL is invalid.");
}
var code = await urlShorteningService.GenerateUniqueCodeAsync();
var shortenedUrl = new ShortenedUrl
{
Id = Guid.NewGuid(),
IpAddress = httpContext.Connection.RemoteIpAddress?.ToString() ?? string.Empty,
LongUrl = request.Url,
Code = code,
ShortUrl = $"{httpContext.Request.Scheme}://{httpContext.Request.Host}/api/{code}",
CreatedOnUtc = DateTime.UtcNow
};
dbContext.ShortenedUrls.Add(shortenedUrl);
await dbContext.SaveChangesAsync();
return Results.Ok(shortenedUrl.ShortUrl);
});
app.MapGet("api/{code}", async (string code, IShortenedUrlRepository shortenedUrlRepository) =>
{
const string invalidCharsPattern = $"[^{UrlShorteningService.Alphabet}]";
if (code.Length != UrlShorteningService.NumberOfCharsInShortLink || Regex.IsMatch(code, invalidCharsPattern))
{
return Results.BadRequest("The specified code is invalid.");
}
var shortenedUrl = await shortenedUrlRepository.GetByCodeAsync(code);
if (shortenedUrl is null)
{
return Results.NotFound();
}
return Results.Redirect(shortenedUrl.LongUrl);
});
if (app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}
else
{
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
}
app.Run();