Skip to content

Commit dbdc91d

Browse files
committed
refractored config loading
1 parent a51b4f6 commit dbdc91d

3 files changed

Lines changed: 17 additions & 15 deletions

File tree

.idea/.idea.LinkRouter/.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LinkRouter/App/Http/Controllers/RedirectController.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using LinkRouter.App.Configuration;
1+
using System.Text.Json;
2+
using System.IO;
3+
using LinkRouter.App.Configuration;
24
using Microsoft.AspNetCore.Mvc;
35
using MoonCore.Services;
46

57
namespace LinkRouter.App.Http.Controllers;
68

79
[ApiController]
8-
[Route("[controller]")]
910
public class RedirectController : Controller
1011
{
1112

@@ -19,14 +20,23 @@ public RedirectController(ConfigService<Config> configService)
1920
[HttpGet("/{*path}")]
2021
public IActionResult RedirectToExternalUrl(string path)
2122
{
22-
path = "/" + path.Trim('/');
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));
2331

24-
var redirectRoute = ConfigService.Get().Routes.FirstOrDefault(x => x.Route == path || x.Route == path + "/");
32+
if (config == null)
33+
return NotFound();
34+
35+
var redirectRoute = config.Routes.FirstOrDefault(x => x.Route == path || x.Route == path + "/");
2536

2637
if (redirectRoute == null)
27-
{
2838
return NotFound();
29-
}
39+
3040

3141
return Redirect(redirectRoute.RedirectUrl);
3242
}

LinkRouter/Program.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ public static void Main(string[] args)
1515
// Add services to the container.
1616

1717
Directory.CreateDirectory(PathBuilder.Dir("data"));
18-
19-
var configFilePath = PathBuilder.File("data", "config.json");
20-
21-
if (!File.Exists(configFilePath))
22-
File.WriteAllText(configFilePath, "{}");
23-
24-
ConfigService<Config> configService = new(configFilePath);
25-
26-
builder.Services.AddSingleton(configService);
2718

2819
builder.Services.AddControllers();
2920

0 commit comments

Comments
 (0)