mirror of
https://github.com/yawaflua/yaflay.ru.git
synced 2026-02-05 11:24:13 +02:00
make backend for my site
This commit is contained in:
34
Controllers/RedirectController.cs
Normal file
34
Controllers/RedirectController.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using yawaflua.ru.Models;
|
||||
using yawaflua.ru.Models.Tables;
|
||||
using yawaflua.ru.Utilities;
|
||||
|
||||
namespace yawaflua.ru.Controllers
|
||||
{
|
||||
[Route("/r/")]
|
||||
[Authorize]
|
||||
public class RedirectController : ControllerBase
|
||||
{
|
||||
private AppDbContext ctx;
|
||||
private MemoryCache cache;
|
||||
public RedirectController(AppDbContext ctx, MemoryCache cache)
|
||||
{
|
||||
this.ctx = ctx;
|
||||
this.cache = cache;
|
||||
}
|
||||
[HttpGet("{uri}")]
|
||||
public async Task<IActionResult> FromGitHub(string uri)
|
||||
{
|
||||
Console.WriteLine(uri);
|
||||
Redirects redirects;
|
||||
if (!cache.TryGetValue($"redirectsWithUrl-{uri}", out redirects) || ctx.Redirects.TryGetValue(k => k.uri == uri, out redirects))
|
||||
cache.Set($"redirectsWithUrl-{uri}", redirects, DateTime.Now.AddMinutes(10));
|
||||
|
||||
return Redirect(redirects?.redirectTo ?? "/404");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user