Files
NginxMan/WebPortal.cs
T
2026-08-27 09:05:06 +08:00

122 lines
5.2 KiB
C#

using System.Net;
using System.Reflection;
using System.Text;
namespace NginxMan;
internal static class WebPortal
{
private const string ResourcePrefix = "NginxMan.WebPortal/";
private const string TemplateFileName = "index.template.html";
private const string OutputFileName = "index.html";
private const string ServiceCardsToken = "{{NGINXMAN_SERVICE_CARDS}}";
private static readonly UTF8Encoding Utf8WithoutBom = new(false);
public static void ExtractTo(string dataDirectory)
{
var assembly = typeof(WebPortal).Assembly;
var resources = assembly.GetManifestResourceNames()
.Where(name => name.StartsWith(ResourcePrefix, StringComparison.Ordinal))
.ToArray();
var targetDirectory = Path.Combine(dataDirectory, "www");
var targetRoot = Path.GetFullPath(targetDirectory) + Path.DirectorySeparatorChar;
foreach (var resourceName in resources)
{
var relativePath = resourceName[ResourcePrefix.Length..]
.Replace('/', Path.DirectorySeparatorChar)
.Replace('\\', Path.DirectorySeparatorChar);
if (relativePath.Equals("index.html", StringComparison.OrdinalIgnoreCase))
{
relativePath = TemplateFileName;
}
var targetPath = Path.GetFullPath(Path.Combine(targetDirectory, relativePath));
if (!targetPath.StartsWith(targetRoot, StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("Invalid embedded web resource path.");
}
// Keep the extracted template editable. Future route changes only render
// index.html and never overwrite a customized index.template.html.
if (File.Exists(targetPath))
{
continue;
}
Directory.CreateDirectory(Path.GetDirectoryName(targetPath)!);
using var input = assembly.GetManifestResourceStream(resourceName)
?? throw new InvalidOperationException($"Cannot read embedded resource: {resourceName}");
using var output = File.Create(targetPath);
input.CopyTo(output);
}
}
public static string RenderTo(string dataDirectory, IReadOnlyList<RouteEntry> routes)
{
var targetDirectory = Path.Combine(dataDirectory, "www");
var templatePath = Path.Combine(targetDirectory, TemplateFileName);
if (!File.Exists(templatePath))
{
ExtractTo(dataDirectory);
}
var template = File.ReadAllText(templatePath, Encoding.UTF8);
if (!template.Contains(ServiceCardsToken, StringComparison.Ordinal))
{
throw new InvalidOperationException(
$"网页模板缺少入口占位符 {ServiceCardsToken}: {templatePath}");
}
var content = template.Replace(
ServiceCardsToken,
BuildServiceCards(routes),
StringComparison.Ordinal);
var outputPath = Path.Combine(targetDirectory, OutputFileName);
File.WriteAllText(outputPath, content, Utf8WithoutBom);
return outputPath;
}
private static string BuildServiceCards(IReadOnlyList<RouteEntry> routes)
{
if (routes.Count == 0)
{
return " <div class=\"empty-state\">暂时没有可用的网站入口。</div>";
}
var cards = new StringBuilder();
for (var index = 0; index < routes.Count; index++)
{
var route = routes[index];
var domain = route.Domain.Trim().TrimEnd('.');
var websiteName = string.IsNullOrWhiteSpace(route.WebsiteName) ? domain : route.WebsiteName.Trim();
var description = string.IsNullOrWhiteSpace(route.Description)
? $"访问 {domain} 服务。"
: route.Description.Trim();
var portSuffix = route.ListenPort == 80 ? string.Empty : $":{route.ListenPort}";
var href = $"http://{domain}{portSuffix}/";
var accentClass = $"accent-{index % 4}";
var delay = Math.Min(0.36 + index * 0.08, 1.2).ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
cards.AppendLine($" <a class=\"service {accentClass}\" href=\"{Encode(href)}\" style=\"--delay: {delay}s\" aria-label=\"{Encode(websiteName)}\">");
cards.AppendLine(" <div class=\"service-top\">");
cards.AppendLine($" <span class=\"service-code\">{index + 1:D2} / {Encode(domain)}</span>");
cards.AppendLine(" <span class=\"node\" aria-hidden=\"true\"></span>");
cards.AppendLine(" </div>");
cards.AppendLine($" <h2>{Encode(websiteName)}</h2>");
cards.AppendLine($" <p>{Encode(description)}</p>");
cards.AppendLine($" <span class=\"enter\">进入 {Encode(websiteName)}</span>");
cards.Append(" </a>");
if (index < routes.Count - 1)
{
cards.AppendLine();
cards.AppendLine();
}
}
return cards.ToString();
}
private static string Encode(string value) => WebUtility.HtmlEncode(value);
}