-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
47 lines (33 loc) · 1.43 KB
/
Program.cs
File metadata and controls
47 lines (33 loc) · 1.43 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
using LiteDB;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("Db");
builder.Services.AddSingleton<ILiteDatabase, LiteDatabase>(x => new LiteDatabase(connectionString));
var app = builder.Build();
app.MapGet("/", async (HttpContext ctx) =>
{
ctx.Response.Headers.ContentType = new Microsoft.Extensions.Primitives.StringValues("text/html; charset=UTF-8");
await ctx.Response.SendFileAsync("wwwroot/index.html");
});
app.MapGet("/{chunck}", (string chunck, ILiteDatabase db) =>
db.GetCollection<ShortUrl>().FindOne(x => x.Chunck == chunck)
is ShortUrl url
? Results.Redirect(url.Url)
: Results.NotFound());
app.MapPost("/urls", (ShortUrl shortUrl, HttpContext ctx, ILiteDatabase db) =>
{
if (Uri.TryCreate(shortUrl.Url, UriKind.RelativeOrAbsolute, out Uri? parsedUri))
{
shortUrl.Chunck = Nanoid.Nanoid.Generate(size: 9);
db.GetCollection<ShortUrl>(BsonAutoId.Guid).Insert(shortUrl);
var rawShortUrl = $"{ctx.Request.Scheme}://{ctx.Request.Host}/{shortUrl.Chunck}";
return Results.Ok(new { ShortUrl = rawShortUrl });
}
return Results.BadRequest(new { ErrorMessage = "Invalid Url" });
});
app.MapGet("/urls", (ILiteDatabase db) => Results.Ok(db.GetCollection<ShortUrl>().FindAll()));
app.Run();
public record class ShortUrl(string Url)
{
public Guid Id { get; set; }
public string? Chunck { get; set; }
}