mirror of
https://github.com/yawaflua/yaflay.ru.git
synced 2026-02-04 10:54:12 +02:00
add page redirector for chorten-url service, u can use this feature, using this route: DOMAIN/?uri=SHORTED_URI
fix backend controller and change route to DOMAIN/r/SHORTED_URI
This commit is contained in:
@@ -79,7 +79,7 @@ namespace yaflay.ru.Controllers
|
|||||||
}
|
}
|
||||||
string responseBody = await message.Content.ReadAsStringAsync();
|
string responseBody = await message.Content.ReadAsStringAsync();
|
||||||
JsonNode response = JsonNode.Parse(responseBody);
|
JsonNode response = JsonNode.Parse(responseBody);
|
||||||
if (response["user"] != null || Startup.ownerId.FirstOrDefault(response["user"]?["id"].ToString()) == null)
|
if (response["user"] != null || Startup.ownerId?.FirstOrDefault(response["user"]?["id"].ToString()) == null)
|
||||||
{
|
{
|
||||||
Redirects redirects = new()
|
Redirects redirects = new()
|
||||||
{
|
{
|
||||||
@@ -109,7 +109,7 @@ namespace yaflay.ru.Controllers
|
|||||||
}
|
}
|
||||||
string responseBody = await message.Content.ReadAsStringAsync();
|
string responseBody = await message.Content.ReadAsStringAsync();
|
||||||
JsonNode response = JsonNode.Parse(responseBody);
|
JsonNode response = JsonNode.Parse(responseBody);
|
||||||
if (response["user"] != null || Startup.ownerId.FirstOrDefault(response["user"]?["id"].ToString()) == null )
|
if (response["user"] != null || Startup.ownerId?.FirstOrDefault(response["user"]?["id"].ToString()) == null )
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -235,7 +235,7 @@ namespace yaflay.ru.Controllers
|
|||||||
await ctx.SaveChangesAsync();
|
await ctx.SaveChangesAsync();
|
||||||
return Ok(body.melon);
|
return Ok(body.melon);
|
||||||
}
|
}
|
||||||
[HttpGet("{uri}")]
|
[HttpGet("r/{uri}")]
|
||||||
public async Task<IActionResult> FromGitHub(string uri)
|
public async Task<IActionResult> FromGitHub(string uri)
|
||||||
{
|
{
|
||||||
Console.WriteLine(uri);
|
Console.WriteLine(uri);
|
||||||
@@ -248,7 +248,7 @@ namespace yaflay.ru.Controllers
|
|||||||
if (fromCache != null)
|
if (fromCache != null)
|
||||||
cache.Set($"redirectsWithUrl-{uri}", (object)fromCache, DateTime.Now.AddMinutes(10));
|
cache.Set($"redirectsWithUrl-{uri}", (object)fromCache, DateTime.Now.AddMinutes(10));
|
||||||
}
|
}
|
||||||
Console.WriteLine(fromCache.ToString());
|
Console.WriteLine(fromCache?.ToString());
|
||||||
return Redirect(fromCache?.redirectTo ?? "/404");
|
return Redirect(fromCache?.redirectTo ?? "/404");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
@page
|
@page "{uri?}"
|
||||||
@model IndexModel
|
@model IndexModel
|
||||||
@{
|
@{
|
||||||
ViewData["Title"] = "yawaflua";
|
ViewData["Title"] = "yawaflua";
|
||||||
|
|||||||
@@ -1,22 +1,46 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using yaflay.ru.Models;
|
||||||
|
using yaflay.ru.Models.Tables;
|
||||||
|
|
||||||
namespace yaflay.ru.Pages
|
namespace yaflay.ru.Pages
|
||||||
{
|
{
|
||||||
public class IndexModel : PageModel
|
public class IndexModel : PageModel
|
||||||
{
|
{
|
||||||
private readonly ILogger<IndexModel> _logger;
|
private readonly ILogger<IndexModel> _logger;
|
||||||
//public string? uri { get; set; } = null;
|
public IMemoryCache cache;
|
||||||
public IndexModel(ILogger<IndexModel> logger)
|
public AppDbContext ctx;
|
||||||
|
public string? uri { get; set; } = null;
|
||||||
|
public IndexModel(ILogger<IndexModel> logger, IMemoryCache cache, AppDbContext ctx)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
this.cache = cache;
|
||||||
|
this.ctx = ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnGet(/**string? uri**/)
|
public void OnGet(string? uri)
|
||||||
|
{
|
||||||
|
|
||||||
|
this.uri = uri ?? null;
|
||||||
|
Console.WriteLine(uri);
|
||||||
|
if (this.uri != null)
|
||||||
|
{
|
||||||
|
Redirects? fromCache = cache.Get<Redirects>($"redirectsWithUrl-{uri}") ?? null;
|
||||||
|
if (fromCache == null)
|
||||||
|
{
|
||||||
|
fromCache = ctx.Redirects.FirstOrDefault(k => k.uri == uri);
|
||||||
|
Console.WriteLine("Im here!");
|
||||||
|
if (fromCache != null)
|
||||||
|
cache.Set($"redirectsWithUrl-{uri}", (object)fromCache, DateTime.Now.AddMinutes(10));
|
||||||
|
}
|
||||||
|
Console.WriteLine(fromCache?.ToString());
|
||||||
|
Response.Redirect(fromCache?.redirectTo ?? "/404");
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
Page();
|
Page();
|
||||||
//this.uri = uri ?? null;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,10 @@ namespace yaflay.ru
|
|||||||
if (readmeFile == null)
|
if (readmeFile == null)
|
||||||
{
|
{
|
||||||
readmeFile = configuration.GetValue<string>("readmeFile");
|
readmeFile = configuration.GetValue<string>("readmeFile");
|
||||||
|
if (readmeFile == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("ReadmeFile link is null");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user