Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ obj/
riderModule.iml
/_ReSharper.Caches/
/LinkRouter/data
*.DotSettings.user
37 changes: 36 additions & 1 deletion LinkRouter/App/Http/Controllers/RedirectController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using LinkRouter.App.Configuration;
using LinkRouter.App.Configuration;
using Microsoft.AspNetCore.Mvc;
using Prometheus;


namespace LinkRouter.App.Http.Controllers;

Expand All @@ -8,6 +10,26 @@ public class RedirectController : Controller
{

private readonly Config Config;


private readonly Counter RouteCounter = Metrics.CreateCounter(
"linkrouter_requests",
"Counts the number of requests to the link router",
new CounterConfiguration
{
LabelNames = new[] { "route" }
}
);


private readonly Counter NotFoundCounter = Metrics.CreateCounter(
"linkrouter_404_requests",
"Counts the number of not found requests to the link router",
new CounterConfiguration
{
LabelNames = new[] { "route" }
}
);

public RedirectController(Config config)
{
Expand All @@ -20,8 +42,17 @@ public IActionResult RedirectToExternalUrl(string path)
var redirectRoute = Config.Routes.FirstOrDefault(x => x.Route == path || x.Route == path + "/" || x.Route == "/" + path);

if (redirectRoute != null)
{
RouteCounter
.WithLabels(redirectRoute.Route)
.Inc();

return Redirect(redirectRoute.RedirectUrl);
}

NotFoundCounter
.WithLabels(path)
.Inc();

if (Config.NotFoundBehavior.RedirectOn404)
return Redirect(Config.NotFoundBehavior.RedirectUrl);
Expand All @@ -32,6 +63,10 @@ public IActionResult RedirectToExternalUrl(string path)
[HttpGet("/")]
public IActionResult GetRootRoute()
{
RouteCounter
.WithLabels("/")
.Inc();

string url = Config.RootRoute;

return Redirect(url);
Expand Down
1 change: 1 addition & 0 deletions LinkRouter/LinkRouter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.20"/>
<PackageReference Include="MoonCore" Version="1.5.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
</ItemGroup>

Expand Down
7 changes: 7 additions & 0 deletions LinkRouter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using LinkRouter.App.Services;
using MoonCore.Extensions;
using MoonCore.Helpers;
using Prometheus;

namespace LinkRouter;

Expand Down Expand Up @@ -45,8 +46,14 @@ public static void Main(string[] args)

builder.Services.AddSingleton(config);

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

var app = builder.Build();

app.UseMetricServer();
app.MapControllers();

app.Run();
Expand Down