Skip to content

Commit 909b648

Browse files
committed
refactored redirection
1 parent cae5134 commit 909b648

File tree

4 files changed

+144
-83
lines changed

4 files changed

+144
-83
lines changed
Lines changed: 7 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,30 @@
11
using LinkRouter.App.Configuration;
2+
using LinkRouter.App.Services;
23
using Microsoft.AspNetCore.Mvc;
3-
using Prometheus;
4-
54

65
namespace LinkRouter.App.Http.Controllers;
76

87
[ApiController]
98
public class RedirectController : Controller
109
{
11-
1210
private readonly Config Config;
13-
14-
private readonly Counter RouteCounter = Metrics.CreateCounter(
15-
"linkrouter_requests",
16-
"Counts the number of requests to the link router",
17-
new CounterConfiguration
18-
{
19-
LabelNames = new[] { "route" }
20-
}
21-
);
22-
23-
24-
private readonly Counter NotFoundCounter = Metrics.CreateCounter(
25-
"linkrouter_404_requests",
26-
"Counts the number of not found requests to the link router",
27-
new CounterConfiguration
28-
{
29-
LabelNames = new[] { "route" }
30-
}
31-
);
11+
private readonly RedirectionService RedirectionService;
3212

33-
public RedirectController(Config config)
13+
public RedirectController(Config config, RedirectionService redirectionService)
3414
{
3515
Config = config;
16+
RedirectionService = redirectionService;
3617
}
3718

3819
[HttpGet("/{*path}")]
3920
public async Task<ActionResult> RedirectToExternalUrl(string path)
4021
{
41-
if (!path.EndsWith("/"))
42-
path += "/";
43-
44-
path = "/" + path;
45-
46-
Console.WriteLine(path);
47-
48-
var redirectRoute = Config.CompiledRoutes?.FirstOrDefault(x => x.CompiledPattern.IsMatch(path));
49-
50-
if (redirectRoute == null)
51-
{
52-
NotFoundCounter
53-
.WithLabels(path)
54-
.Inc();
55-
56-
if (Config.NotFoundBehavior.RedirectOn404)
57-
if (Config.ErrorCodePattern.IsMatch(Config.NotFoundBehavior.RedirectUrl))
58-
{
59-
var errorCodeMatch = Config.ErrorCodePattern.Match(Config.NotFoundBehavior.RedirectUrl);
60-
var errorCode = int.Parse(errorCodeMatch.Groups[1].Value);
61-
return StatusCode(errorCode);
62-
} else
63-
return Redirect(Config.NotFoundBehavior.RedirectUrl);
64-
65-
return NotFound();
66-
}
67-
68-
var match = redirectRoute.CompiledPattern.Match(path);
69-
70-
string redirectUrl = redirectRoute.RedirectUrl;
71-
72-
if (Config.ErrorCodePattern.IsMatch(redirectUrl))
73-
{
74-
var errorCodeMatch = Config.ErrorCodePattern.Match(redirectUrl);
75-
var errorCode = int.Parse(errorCodeMatch.Groups[1].Value);
76-
return StatusCode(errorCode);
77-
}
78-
79-
foreach (var placeholder in redirectRoute.Placeholders)
80-
{
81-
var value = match.Groups[placeholder.Value].Value;
82-
redirectUrl = redirectUrl.Replace("{" + placeholder.Key + "}", value);
83-
}
84-
85-
return Redirect(redirectUrl);
22+
return await RedirectionService.GetRedirect(path);
8623
}
8724

8825
[HttpGet("/")]
89-
public IActionResult GetRootRoute()
26+
public async Task<ActionResult> GetRootRoute()
9027
{
91-
RouteCounter
92-
.WithLabels("/")
93-
.Inc();
94-
95-
string url = Config.RootRoute;
96-
97-
if (Config.ErrorCodePattern.IsMatch(url))
98-
{
99-
var errorCodeMatch = Config.ErrorCodePattern.Match(url);
100-
var errorCode = int.Parse(errorCodeMatch.Groups[1].Value);
101-
return StatusCode(errorCode);
102-
}
103-
104-
return Redirect(url);
28+
return await RedirectionService.GetRedirect(string.Empty);
10529
}
10630
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using MoonCore.Attributes;
2+
using Prometheus;
3+
4+
namespace LinkRouter.App.Services;
5+
6+
[Singleton]
7+
public class MetricsService
8+
{
9+
private readonly Counter RouteCounter = Metrics.CreateCounter(
10+
"linkrouter_requests",
11+
"Counts the number of requests to the link router",
12+
new CounterConfiguration
13+
{
14+
LabelNames = new[] { "route" }
15+
}
16+
);
17+
18+
19+
private readonly Counter NotFoundCounter = Metrics.CreateCounter(
20+
"linkrouter_404_requests",
21+
"Counts the number of not found requests to the link router",
22+
new CounterConfiguration
23+
{
24+
LabelNames = new[] { "route" }
25+
}
26+
);
27+
28+
public Task IncrementNotFound(string path)
29+
{
30+
NotFoundCounter
31+
.WithLabels(path)
32+
.Inc();
33+
34+
return Task.CompletedTask;
35+
}
36+
37+
public Task IncrementFound(string path)
38+
{
39+
RouteCounter
40+
.WithLabels(path)
41+
.Inc();
42+
43+
return Task.CompletedTask;
44+
}
45+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using LinkRouter.App.Configuration;
2+
using Microsoft.AspNetCore.Mvc;
3+
using MoonCore.Attributes;
4+
5+
namespace LinkRouter.App.Services;
6+
7+
[Singleton]
8+
public class RedirectionService
9+
{
10+
private readonly Config Config;
11+
private readonly MetricsService MetricsService;
12+
13+
public RedirectionService(Config config, MetricsService metricsService)
14+
{
15+
Config = config;
16+
MetricsService = metricsService;
17+
}
18+
19+
public async Task<ActionResult> GetRedirect(string path)
20+
{
21+
if (path == "")
22+
{
23+
var url = Config.RootRoute;
24+
25+
if (Config.ErrorCodePattern.IsMatch(url))
26+
{
27+
var errorCodeMatch = Config.ErrorCodePattern.Match(url);
28+
var errorCode = int.Parse(errorCodeMatch.Groups[1].Value);
29+
return new StatusCodeResult(errorCode);
30+
}
31+
32+
await MetricsService.IncrementFound("/");
33+
34+
return new RedirectResult(url);
35+
}
36+
37+
if (!path.EndsWith("/"))
38+
path += "/";
39+
40+
path = "/" + path;
41+
42+
43+
var redirectRoute = Config.CompiledRoutes?.FirstOrDefault(x => x.CompiledPattern.IsMatch(path));
44+
45+
46+
if (redirectRoute == null)
47+
{
48+
await MetricsService.IncrementNotFound(path);
49+
50+
if (!Config.NotFoundBehavior.RedirectOn404)
51+
return new NotFoundResult();
52+
53+
54+
if (TryGetErrorCode(Config.NotFoundBehavior.RedirectUrl, out var notFoundStatusCode))
55+
return new StatusCodeResult(notFoundStatusCode);
56+
57+
return new RedirectResult(Config.NotFoundBehavior.RedirectUrl);
58+
}
59+
60+
var match = redirectRoute.CompiledPattern.Match(path);
61+
62+
if (TryGetErrorCode(redirectRoute.RedirectUrl, out var statusCode))
63+
return new StatusCodeResult(statusCode);
64+
65+
66+
foreach (var placeholder in redirectRoute.Placeholders)
67+
{
68+
var value = match.Groups[placeholder.Value].Value;
69+
redirectRoute.RedirectUrl = redirectRoute.RedirectUrl.Replace("{" + placeholder.Key + "}", value);
70+
}
71+
72+
await MetricsService.IncrementFound(path);
73+
74+
return new RedirectResult(redirectRoute.RedirectUrl);
75+
}
76+
77+
private bool TryGetErrorCode(string url, out int code)
78+
{
79+
if (Config.ErrorCodePattern.IsMatch(url))
80+
{
81+
var errorCodeMatch = Config.ErrorCodePattern.Match(url);
82+
code = int.Parse(errorCodeMatch.Groups[1].Value);
83+
return true;
84+
}
85+
86+
code = 0;
87+
88+
return false;
89+
}
90+
}

LinkRouter/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public static void Main(string[] args)
4242
JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }));
4343

4444
builder.Services.AddSingleton(config);
45+
46+
builder.Services.AutoAddServices<Program>();
4547

4648
builder.Services.AddMetricServer(options => { options.Port = 5000; });
4749

0 commit comments

Comments
 (0)