-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
79 lines (70 loc) · 2.49 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
using Microsoft.OpenApi.Models;
using urlshrink.Data;
// Create app builder
var builder = WebApplication.CreateBuilder(args);
// Configure app builder to use Swagger
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "URLShrink API", Description = "Shortens arbitrary URLs", Version = "v1" });
});
// Create app from builder factory
var app = builder.Build();
// Add Swagger endpoints to app
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "URLShrink API");
});
// Add API routes
app.MapPost("/api/shrink", (OriginalUrl data) => {
// Create a new short URL
if(!data.url.StartsWith("http://") && !data.url.StartsWith("https://"))
{
data.url = "http://" + data.url;
}
try
{
string urlShrunk = UrlData.AddUrl(data.url);
return Results.Ok(new ShrunkUrl(urlShrunk));
}
catch(TimeoutException te)
{
Console.WriteLine(te);
return Results.Problem(statusCode: 500);
}
})
.Produces<ShrunkUrl>(StatusCodes.Status200OK)
.Produces(StatusCodes.Status500InternalServerError);
app.MapGet("/api/lookup/{data}", (string data) => {
// Return original URL as JSON
var shrunk = Uri.UnescapeDataString(data);
var lastSlash = shrunk.LastIndexOf('/');
shrunk = shrunk.Substring(lastSlash + 1, shrunk.Length - (lastSlash + 1));
var original = UrlData.GetOriginal(shrunk);
if(original == null) return Results.NotFound();
else return Results.Ok(new OriginalUrl(original));
})
.Produces<OriginalUrl>(StatusCodes.Status200OK)
.Produces(StatusCodes.Status404NotFound);
app.MapGet("/", (HttpContext http) => {
// Return the homepage
var homePage = File.ReadAllText(@"./public/index.html");
http.Response.Headers.ContentType = "text/html";
return homePage;
})
.Produces(StatusCodes.Status200OK, contentType: "text/html");
app.MapGet("/{*rest}", (HttpContext http, string rest) => {
// Return original URL as plaintext
bool viewOnly = rest.EndsWith('-');
if(viewOnly) rest = rest.Substring(0, rest.Length - 1);
var original = UrlData.GetOriginal(rest);
if(original == null) return Results.NotFound();
if(viewOnly) return Results.Text(original);
else return Results.Redirect(original, false, false);
})
.Produces(StatusCodes.Status200OK, contentType: "text/plain")
.Produces(StatusCodes.Status404NotFound)
.Produces(StatusCodes.Status302Found);
// Start listening for HTTP traffic
app.Run();