sillifactoring

This commit is contained in:
femsci 2024-06-14 13:49:30 +02:00
parent a31458f415
commit 1205bbe365
Signed by: femsci
GPG key ID: 21AC2CC03E5BBCD6
8 changed files with 126 additions and 44 deletions

View file

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace DOIOU.Server.Controllers
{
[Route("/info")]
public class InfoController : Controller
{
private readonly ILogger<InfoController> _logger;
public InfoController(ILogger<InfoController> logger)
{
_logger = logger;
}
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpGet("faq")]
public IActionResult FAQ()
{
return View("FAQ");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View("Error!");
}
}
}

View file

@ -1,4 +1,5 @@
using System.Diagnostics; using System.Diagnostics;
using System.Text.RegularExpressions;
using DOIOU.Server.Models; using DOIOU.Server.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -21,28 +22,34 @@ public class MainController : Controller
} }
[HttpGet("{*doiou}")] [HttpGet("{*doiou}")]
public IActionResult Get(string doiou) => Resolve(doiou); public async Task<IActionResult> Get(string doiou) => await Resolve(doiou);
[HttpPost] [HttpPost]
public IActionResult Post([FromForm] string doiou) => Resolve(doiou); public async Task<IActionResult> Post([FromForm] string doiou) => await Resolve(doiou);
private const string DOI_REGEX = @"^((?<scheme>[a-z]+):(\/\/)?)?(?<domain>[a-z0-9.\-]+\/)?((?<prefix>\d{1,3})\.(?<publisher>\d{4,5})\/(?<id>[\S]+[^;,.\s]))$";
[NonAction] [NonAction]
private IActionResult Resolve(string doiou) private async Task<IActionResult> Resolve(string doiou)
{ {
int i = doiou.IndexOf('.'), prefix = 69; var match = Regex.Match(doiou, DOI_REGEX);
if (i != -1 && !int.TryParse(doiou[..i], out prefix)) if (!match.Success)
{ {
return BadRequest("Invalid format"); return BadRequest("Invalid format");
} }
// TODO further validation int prefix = int.Parse(match.Groups["prefix"].Value);
int publisher = int.Parse(match.Groups["publisher"].Value);
string id = match.Groups["id"].Value;
// Maybe add other providers if they exist? string query = $"{prefix}.{publisher}/{id}";
if (prefix == 69) // TODO further validation (use schemes?)
if (prefix == 7 || prefix == 69)
{ {
string? q = _data.Query(doiou); string? q = _data.Query(query);
return q != null ? Redirect(q) : NotFound("DOIOU not found"); return q != null ? Redirect(q) : NotFound("DOIOU not found");
} }
@ -52,7 +59,22 @@ public class MainController : Controller
_ => null _ => null
}; };
return urlBase != null ? Redirect($"{urlBase}/{doiou}") : NotFound($"Unsupported prefix: {prefix}"); if (urlBase == null)
return BadRequest($"Unsupported prefix: {prefix}");
using HttpClient client = new();
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "DOIOU v1.0");
var response = await client.GetAsync($"{urlBase}/{doiou}");
if (((int)response.StatusCode / 100) == 3)
{
string location = response.Headers!.Location!.ToString();
return Redirect(location);
}
//TODO: cache
return NotFound("DOIOU not found");
} }
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>

View file

@ -0,0 +1,8 @@
@{
ViewData["Title"] = "FAQ";
}
<div class="text-center">
<h1 class="display-4">FAQ</h1>
<p>something something estradiol</p>
</div>

View file

@ -0,0 +1,8 @@
@{
ViewData["Title"] = "Info";
}
<div class="text-center">
<h1 class="display-4">Info</h1>
<p>idk</p>
</div>

View file

@ -12,7 +12,6 @@
<header> <header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container-fluid"> <div class="container-fluid">
<a class="navbar-brand" asp-area="" asp-controller="Main" asp-action="Index">DOIOU</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent" <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation"> aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span> <span class="navbar-toggler-icon"></span>
@ -22,6 +21,12 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Main" asp-action="Index">Home</a> <a class="nav-link text-dark" asp-area="" asp-controller="Main" asp-action="Index">Home</a>
</li> </li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Info" asp-action="FAQ">FAQ</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="User" asp-action="Dashboard">Members</a>
</li>
</ul> </ul>
</div> </div>
</div> </div>

View file

@ -1,4 +1,4 @@
# DOIOU # DOIOU
*DOI but free* *DOI but free...* \
A simple extension of the DOI system for arbitrary documents. A simple extension of the DOI system for arbitrary documents.