Skip to content

Commit a34d8f9

Browse files
committed
updated to dotnet 9, used new mooncore logger, used intelligent config loading
1 parent dbdc91d commit a34d8f9

7 files changed

Lines changed: 98 additions & 43 deletions

File tree

LinkRouter/App/Configuration/Config.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ public class Config
88
[JsonProperty("RouteOn/")]
99
public string RootRoute { get; set; } = "https://example.com";
1010

11-
public List<RedirectRoute> Routes { get; set; } = new (
12-
[
11+
public RedirectRoute[] Routes { get; set; } = [
1312
new RedirectRoute()
1413
{
1514
Route = "/instagram",
@@ -20,5 +19,5 @@ public class Config
2019
Route = "/example",
2120
RedirectUrl = "https://example.com"
2221
},
23-
]);
22+
];
2423
}

LinkRouter/App/Http/Controllers/RedirectController.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,29 @@ namespace LinkRouter.App.Http.Controllers;
1010
public class RedirectController : Controller
1111
{
1212

13-
private readonly ConfigService<Config> ConfigService;
13+
private readonly Config Config;
1414

15-
public RedirectController(ConfigService<Config> configService)
15+
public RedirectController(Config config)
1616
{
17-
ConfigService = configService;
17+
Config = config;
1818
}
1919

2020
[HttpGet("/{*path}")]
2121
public IActionResult RedirectToExternalUrl(string path)
2222
{
23-
var configPath = Path.Combine("data", "config.json");
24-
25-
if (!System.IO.File.Exists(configPath) || string.IsNullOrEmpty(System.IO.File.ReadAllText(configPath)))
26-
{
27-
return NotFound();
28-
}
29-
30-
var config = JsonSerializer.Deserialize<Config>(System.IO.File.ReadAllText(configPath));
31-
32-
if (config == null)
33-
return NotFound();
34-
35-
var redirectRoute = config.Routes.FirstOrDefault(x => x.Route == path || x.Route == path + "/");
23+
var redirectRoute = Config.Routes.FirstOrDefault(x => x.Route == path || x.Route == path + "/" || x.Route == "/" + path);
3624

3725
if (redirectRoute == null)
3826
return NotFound();
3927

40-
4128
return Redirect(redirectRoute.RedirectUrl);
4229
}
4330

4431
[HttpGet("/")]
4532
public IActionResult GetRootRoute()
4633
{
47-
string url = ConfigService.Get().RootRoute;
34+
string url = Config.RootRoute;
35+
4836
return Redirect(url);
4937
}
5038
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Text.Json;
2+
using LinkRouter.App.Configuration;
3+
4+
namespace LinkRouter.App.Services;
5+
6+
public class ConfigWatcher : BackgroundService
7+
{
8+
private string ConfigPath => Path.Combine("data", "config.json");
9+
private ILogger<ConfigWatcher> Logger;
10+
private Config Config;
11+
private FileSystemWatcher Watcher;
12+
13+
public ConfigWatcher(ILogger<ConfigWatcher> logger, Config config)
14+
{
15+
Logger = logger;
16+
Config = config;
17+
}
18+
19+
protected override Task ExecuteAsync(CancellationToken stoppingToken)
20+
{
21+
if (!File.Exists(ConfigPath))
22+
{
23+
Logger.LogWarning("Watched file does not exist: {FilePath}", ConfigPath);
24+
File.WriteAllText(ConfigPath, "Initial content");
25+
}
26+
27+
Watcher = new FileSystemWatcher(Path.GetDirectoryName(ConfigPath) ?? throw new InvalidOperationException())
28+
{
29+
Filter = Path.GetFileName(ConfigPath),
30+
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.CreationTime
31+
};
32+
33+
Watcher.Changed += OnChanged;
34+
35+
Watcher.EnableRaisingEvents = true;
36+
37+
return Task.CompletedTask;
38+
}
39+
40+
private void OnChanged(object sender, FileSystemEventArgs e)
41+
{
42+
try
43+
{
44+
var content = File.ReadAllText(ConfigPath);
45+
46+
var config = JsonSerializer.Deserialize<Config>(content);
47+
48+
Config.Routes = config?.Routes ?? [];
49+
Config.RootRoute = config?.RootRoute ?? "https://example.com";
50+
51+
Logger.LogInformation("Config file changed.");
52+
}
53+
catch (IOException ex)
54+
{
55+
}
56+
}
57+
}

LinkRouter/LinkRouter.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
@@ -21,4 +21,8 @@
2121
</Content>
2222
</ItemGroup>
2323

24+
<ItemGroup>
25+
<Folder Include="data\" />
26+
</ItemGroup>
27+
2428
</Project>

LinkRouter/Program.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

22

3+
using System.Text.Json;
34
using LinkRouter.App.Configuration;
5+
using LinkRouter.App.Services;
6+
using MoonCore.Extensions;
47
using MoonCore.Helpers;
58
using MoonCore.Services;
69

@@ -22,7 +25,30 @@ public static void Main(string[] args)
2225
{
2326
serverOptions.ListenAnyIP(80);
2427
});
28+
29+
var loggerProviders = LoggerBuildHelper.BuildFromConfiguration(configuration =>
30+
{
31+
configuration.Console.Enable = true;
32+
configuration.Console.EnableAnsiMode = true;
33+
});
34+
35+
builder.Logging.ClearProviders();
36+
builder.Logging.AddProviders(loggerProviders);
2537

38+
builder.Services.AddHostedService<ConfigWatcher>();
39+
40+
var configPath = Path.Combine("data", "config.json");
41+
42+
if (!File.Exists(configPath))
43+
File.WriteAllText(
44+
configPath,
45+
JsonSerializer.Serialize(new Config(), new JsonSerializerOptions {WriteIndented = true}
46+
));
47+
48+
Config? config = JsonSerializer.Deserialize<Config>(File.ReadAllText(configPath)) ?? new Config();
49+
50+
builder.Services.AddSingleton(config);
51+
2652
var app = builder.Build();
2753

2854
app.MapControllers();

LinkRouter/Properties/launchSettings.json

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,11 @@
1212
"http": {
1313
"commandName": "Project",
1414
"dotnetRunMessages": true,
15-
"launchBrowser": true,
16-
"launchUrl": "swagger",
15+
"launchBrowser": false,
1716
"applicationUrl": "http://localhost:5058",
1817
"environmentVariables": {
1918
"ASPNETCORE_ENVIRONMENT": "Development"
2019
}
21-
},
22-
"https": {
23-
"commandName": "Project",
24-
"dotnetRunMessages": true,
25-
"launchBrowser": true,
26-
"launchUrl": "swagger",
27-
"applicationUrl": "https://localhost:7002;http://localhost:5058",
28-
"environmentVariables": {
29-
"ASPNETCORE_ENVIRONMENT": "Development"
30-
}
31-
},
32-
"IIS Express": {
33-
"commandName": "IISExpress",
34-
"launchBrowser": true,
35-
"launchUrl": "swagger",
36-
"environmentVariables": {
37-
"ASPNETCORE_ENVIRONMENT": "Development"
38-
}
3920
}
4021
}
4122
}

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "8.0.0",
3+
"version": "9.0.0",
44
"rollForward": "latestMinor",
55
"allowPrerelease": false
66
}

0 commit comments

Comments
 (0)