diff --git a/MainForm.cs b/MainForm.cs index 4db2726..26c56ae 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -32,6 +32,7 @@ public sealed class MainForm : Form private Button _btnImport = null!; private Button _btnRefreshStatus = null!; private Button _btnSavePortal = null!; + private Button _btnEditPortalContent = null!; private string? _selectedDomain; private bool _closingBackupCreated; @@ -133,6 +134,8 @@ public sealed class MainForm : Form _btnRefreshStatus.Click += (s, e) => RefreshStatus(); _btnSavePortal = CreateButton("保存入口设置", 96); _btnSavePortal.Click += async (s, e) => await SavePortalSettingsAsync(); + _btnEditPortalContent = CreateButton("主页内容", 96); + _btnEditPortalContent.Click += (s, e) => EditPortalContent(); panel.Controls.Add(CreateLabel("配置文件"), 0, 0); panel.Controls.Add(_txtConfigPath, 1, 0); @@ -153,7 +156,7 @@ public sealed class MainForm : Form panel.Controls.Add(_lblStatus, 1, 3); panel.SetColumnSpan(_lblStatus, 2); panel.Controls.Add(_btnRefreshStatus, 3, 3); - panel.SetColumnSpan(_btnRefreshStatus, 2); + panel.Controls.Add(_btnEditPortalContent, 4, 3); return panel; } @@ -467,6 +470,32 @@ public sealed class MainForm : Form } } + private void EditPortalContent() + { + using var dialog = new PortalContentDialog(_manager.PortalContent); + if (dialog.ShowDialog(this) != DialogResult.OK) + { + return; + } + + try + { + _manager.UpdatePortalContent( + dialog.SiteName, + dialog.Headline, + dialog.Introduction); + MessageBox.Show( + "主页内容已保存并生成。刷新浏览器即可看到新内容。", + "NginxMan", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "NginxMan", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + } + private void RefreshStatus() { if (!HasConfiguredNginxExe()) @@ -481,6 +510,7 @@ public sealed class MainForm : Form _btnStop.Enabled = false; _btnBackup.Enabled = true; _btnImport.Enabled = true; + _btnEditPortalContent.Enabled = true; return; } @@ -495,6 +525,7 @@ public sealed class MainForm : Form _btnStop.Enabled = isRunning; _btnBackup.Enabled = true; _btnImport.Enabled = true; + _btnEditPortalContent.Enabled = true; } private async Task RunNginxAsync(string actionName, Func action, bool expectedRunning) @@ -546,6 +577,7 @@ public sealed class MainForm : Form _btnStop.Enabled = enabled; _btnBackup.Enabled = enabled; _btnImport.Enabled = enabled; + _btnEditPortalContent.Enabled = enabled; _btnRefreshStatus.Enabled = enabled; } diff --git a/NginxManager.cs b/NginxManager.cs index 1d3b2bd..643dfd3 100644 --- a/NginxManager.cs +++ b/NginxManager.cs @@ -53,6 +53,11 @@ public sealed class NginxManager public int PortalListenPort => _config.PortalListenPort; + public PortalContentSettings PortalContent => new( + _config.PortalSiteName, + _config.PortalHeadline, + _config.PortalIntroduction); + public string NginxExePath { get => _config.NginxExePath; @@ -83,6 +88,14 @@ public sealed class NginxManager SaveConfig(); } + public void UpdatePortalContent(string siteName, string headline, string introduction) + { + _config.PortalSiteName = NormalizeLimitedText(siteName, "入口页名称", 80); + _config.PortalHeadline = NormalizeLimitedText(headline, "主页标题", 120); + _config.PortalIntroduction = NormalizeLimitedText(introduction, "主页简介", 500); + SaveConfig(); + } + public void AddOrUpdateRoute( string domain, string upstream, @@ -218,7 +231,7 @@ public sealed class NginxManager public void SaveConfig() { File.WriteAllText(_configPath, JsonSerializer.Serialize(_config, JsonOptions), Encoding.UTF8); - WebPortal.RenderTo(_dataDirectory, _config.Routes); + WebPortal.RenderTo(_dataDirectory, PortalContent, _config.Routes); } public string CreateBackup(string destinationPath) @@ -367,6 +380,7 @@ public sealed class NginxManager } NormalizeDataPaths(config); + NormalizePortalContent(config); NormalizeRouteMetadata(config.Routes); return config; @@ -486,6 +500,10 @@ public sealed class NginxManager config.ErrorLogPath = NormalizePathSetting(config.ErrorLogPath, defaults.ErrorLogPath); config.PortalDomain = NormalizeOptionalDomain(config.PortalDomain ?? string.Empty); config.PortalListenPort = NormalizePort(config.PortalListenPort, "入口页监听端口"); + NormalizePortalContent(config); + config.PortalSiteName = NormalizeLimitedText(config.PortalSiteName, "入口页名称", 80); + config.PortalHeadline = NormalizeLimitedText(config.PortalHeadline, "主页标题", 120); + config.PortalIntroduction = NormalizeLimitedText(config.PortalIntroduction, "主页简介", 500); config.Routes ??= []; NormalizeDataPaths(config); NormalizeRouteMetadata(config.Routes); @@ -624,6 +642,9 @@ public sealed class NginxManager ErrorLogPath = Path.Combine("data", "logs", "error.log"), PortalDomain = string.Empty, PortalListenPort = 80, + PortalSiteName = PortalContentSettings.DefaultSiteName, + PortalHeadline = PortalContentSettings.DefaultHeadline, + PortalIntroduction = PortalContentSettings.DefaultIntroduction, Routes = [] }; @@ -682,6 +703,19 @@ public sealed class NginxManager } } + private static void NormalizePortalContent(NginxManagerConfig config) + { + config.PortalSiteName = string.IsNullOrWhiteSpace(config.PortalSiteName) + ? PortalContentSettings.DefaultSiteName + : config.PortalSiteName.Trim(); + config.PortalHeadline = string.IsNullOrWhiteSpace(config.PortalHeadline) + ? PortalContentSettings.DefaultHeadline + : config.PortalHeadline.Trim(); + config.PortalIntroduction = string.IsNullOrWhiteSpace(config.PortalIntroduction) + ? PortalContentSettings.DefaultIntroduction + : config.PortalIntroduction.Trim(); + } + private static bool IsLegacyPath(string path, string expectedPath) => string.Equals( path.Replace('/', '\\'), @@ -782,6 +816,17 @@ public sealed class NginxManager return value.Trim(); } + + private static string NormalizeLimitedText(string value, string fieldName, int maximumLength) + { + var normalized = NormalizeRequiredText(value, fieldName); + if (normalized.Length > maximumLength) + { + throw new ArgumentException($"{fieldName}不能超过 {maximumLength} 个字符。"); + } + + return normalized; + } } public sealed record BackupImportResult( @@ -800,9 +845,24 @@ public sealed class NginxManagerConfig public string ErrorLogPath { get; set; } = Path.Combine("logs", "error.log"); public string PortalDomain { get; set; } = string.Empty; public int PortalListenPort { get; set; } = 80; + public string PortalSiteName { get; set; } = PortalContentSettings.DefaultSiteName; + public string PortalHeadline { get; set; } = PortalContentSettings.DefaultHeadline; + public string PortalIntroduction { get; set; } = PortalContentSettings.DefaultIntroduction; public List Routes { get; set; } = []; } +public sealed record PortalContentSettings(string SiteName, string Headline, string Introduction) +{ + public const string DefaultSiteName = "服务导航"; + public const string DefaultHeadline = "欢迎访问统一服务入口"; + public const string DefaultIntroduction = "请选择下方网站或服务,点击卡片即可开始访问。"; + + public static PortalContentSettings Default { get; } = new( + DefaultSiteName, + DefaultHeadline, + DefaultIntroduction); +} + public sealed class RouteEntry { public string Domain { get; set; } = string.Empty; diff --git a/PortalContentDialog.cs b/PortalContentDialog.cs new file mode 100644 index 0000000..a23a28f --- /dev/null +++ b/PortalContentDialog.cs @@ -0,0 +1,152 @@ +namespace NginxMan; + +public sealed class PortalContentDialog : Form +{ + private readonly TextBox _txtSiteName; + private readonly TextBox _txtHeadline; + private readonly TextBox _txtIntroduction; + + public PortalContentDialog(PortalContentSettings content) + { + Text = "主页内容设置"; + StartPosition = FormStartPosition.CenterParent; + FormBorderStyle = FormBorderStyle.FixedDialog; + MaximizeBox = false; + MinimizeBox = false; + ShowInTaskbar = false; + ClientSize = new Size(610, 360); + Font = new Font("Microsoft YaHei UI", 9F); + + var root = new TableLayoutPanel + { + Dock = DockStyle.Fill, + ColumnCount = 2, + RowCount = 6, + Padding = new Padding(18), + BackColor = Color.White + }; + root.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 100)); + root.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100)); + root.RowStyles.Add(new RowStyle(SizeType.Absolute, 42)); + root.RowStyles.Add(new RowStyle(SizeType.Absolute, 44)); + root.RowStyles.Add(new RowStyle(SizeType.Absolute, 44)); + root.RowStyles.Add(new RowStyle(SizeType.Percent, 100)); + root.RowStyles.Add(new RowStyle(SizeType.Absolute, 34)); + root.RowStyles.Add(new RowStyle(SizeType.Absolute, 42)); + + var description = new Label + { + Text = "这些文字会展示在统一入口页上。可以直接修改,也可以恢复默认示例。", + Dock = DockStyle.Fill, + TextAlign = ContentAlignment.MiddleLeft, + ForeColor = Color.FromArgb(70, 85, 96) + }; + root.Controls.Add(description, 0, 0); + root.SetColumnSpan(description, 2); + + _txtSiteName = CreateTextBox(content.SiteName, 80, "例如:服务导航"); + _txtHeadline = CreateTextBox(content.Headline, 120, "例如:欢迎访问统一服务入口"); + _txtIntroduction = CreateTextBox( + content.Introduction, + 500, + "例如:请选择下方网站或服务,点击卡片即可开始访问。"); + _txtIntroduction.Multiline = true; + _txtIntroduction.ScrollBars = ScrollBars.Vertical; + + root.Controls.Add(CreateLabel("入口页名称"), 0, 1); + root.Controls.Add(_txtSiteName, 1, 1); + root.Controls.Add(CreateLabel("主页标题"), 0, 2); + root.Controls.Add(_txtHeadline, 1, 2); + root.Controls.Add(CreateLabel("主页简介"), 0, 3); + root.Controls.Add(_txtIntroduction, 1, 3); + + var hint = new Label + { + Text = "入口页名称同时用于浏览器标签、页头名称和页脚名称。", + Dock = DockStyle.Fill, + TextAlign = ContentAlignment.MiddleLeft, + ForeColor = Color.FromArgb(100, 110, 120) + }; + root.Controls.Add(hint, 1, 4); + + var buttons = new FlowLayoutPanel + { + Dock = DockStyle.Fill, + FlowDirection = FlowDirection.RightToLeft, + WrapContents = false, + Padding = new Padding(0, 6, 0, 0) + }; + var saveButton = CreateButton("保存", 86); + var cancelButton = CreateButton("取消", 86); + var defaultsButton = CreateButton("恢复默认示例", 116); + saveButton.Click += (s, e) => SaveAndClose(); + cancelButton.DialogResult = DialogResult.Cancel; + defaultsButton.Click += (s, e) => LoadDefaults(); + buttons.Controls.Add(saveButton); + buttons.Controls.Add(cancelButton); + buttons.Controls.Add(defaultsButton); + root.Controls.Add(buttons, 0, 5); + root.SetColumnSpan(buttons, 2); + + AcceptButton = saveButton; + CancelButton = cancelButton; + Controls.Add(root); + } + + public string SiteName => _txtSiteName.Text.Trim(); + + public string Headline => _txtHeadline.Text.Trim(); + + public string Introduction => _txtIntroduction.Text.Trim(); + + private void SaveAndClose() + { + if (string.IsNullOrWhiteSpace(SiteName) || + string.IsNullOrWhiteSpace(Headline) || + string.IsNullOrWhiteSpace(Introduction)) + { + MessageBox.Show( + "入口页名称、主页标题和主页简介都不能为空。", + "NginxMan", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + return; + } + + DialogResult = DialogResult.OK; + Close(); + } + + private void LoadDefaults() + { + _txtSiteName.Text = PortalContentSettings.DefaultSiteName; + _txtHeadline.Text = PortalContentSettings.DefaultHeadline; + _txtIntroduction.Text = PortalContentSettings.DefaultIntroduction; + _txtSiteName.Focus(); + } + + private static TextBox CreateTextBox(string value, int maxLength, string placeholder) => new() + { + Text = value, + MaxLength = maxLength, + PlaceholderText = placeholder, + Dock = DockStyle.Fill, + BorderStyle = BorderStyle.FixedSingle + }; + + private static Label CreateLabel(string text) => new() + { + Text = text, + Dock = DockStyle.Fill, + TextAlign = ContentAlignment.MiddleLeft + }; + + private static Button CreateButton(string text, int width) => new() + { + Text = text, + Width = width, + Height = 28, + Margin = new Padding(8, 0, 0, 0), + FlatStyle = FlatStyle.Flat + }; +} diff --git a/README.md b/README.md index 97cbebe..ec11bd8 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ NginxMan 主要用于: - **内置 Nginx**:首次运行时自动释放完整的 Windows Nginx 运行文件。 - **入口页设置**:用户可以设置或关闭入口页域名,并为入口页指定独立的监听端口。 +- **主页内容设置**:通过弹窗维护入口页名称、主页标题和主页简介,并提供可恢复的默认文字示例。 - **映射管理**:支持新增、修改和删除域名反向代理映射。 - **可配置端口**:入口页和每条代理映射均可使用 `1–65535` 范围内的端口。 - **配置生成**:根据界面数据自动生成 `data/nginx.conf`。 @@ -40,6 +41,9 @@ NginxMan 主要用于: | --- | --- | --- | | 入口页域名 | 用于访问统一入口页;留空表示关闭入口站点 | `portal.example.com` | | 入口页端口 | 入口页对外监听的端口 | `8088` | +| 入口页名称 | 显示在浏览器标签、页头和页脚 | `服务导航` | +| 主页标题 | 入口页最醒目的标题 | `欢迎访问统一服务入口` | +| 主页简介 | 标题下方的说明文字 | `请选择下方网站或服务,点击卡片即可开始访问。` | 入口页域名和端口不会写死在源码中,也不需要通过重新编译程序来修改。入口页会作为其监听端口的默认站点,避免未匹配请求误入某条代理映射。 @@ -76,6 +80,7 @@ bin\NginxMan.exe 1. 入口页域名,例如 `portal.example.com`。 2. 入口页端口,例如 `8088`。 3. 点击“保存入口设置”。 +4. 点击“主页内容”,填写入口页名称、主页标题和主页简介;也可以点击“恢复默认示例”。 若不需要统一入口页,将入口页域名留空并保存即可。此时程序不会在 Nginx 配置中生成入口站点,但代理映射仍可正常使用。 @@ -135,6 +140,7 @@ app.example.com -> 服务器 IP | 按钮 | 作用 | | --- | --- | | 保存入口设置 | 保存入口页域名与监听端口;Nginx 运行时自动重新加载;域名留空表示关闭入口站点 | +| 主页内容 | 弹窗设置入口页名称、主页标题和主页简介,并可恢复默认文字示例 | | 新增 | 使用当前表单内容创建一条新映射 | | 保存修改 | 保存列表中当前选中映射的修改 | | 删除 | 删除当前选中的映射 | @@ -214,6 +220,9 @@ data\backups\年年年年月月日日-时时分分秒秒-毫秒-auto-close.zip "errorLogPath": "data\\logs\\error.log", "portalDomain": "portal.example.com", "portalListenPort": 8088, + "portalSiteName": "服务导航", + "portalHeadline": "欢迎访问统一服务入口", + "portalIntroduction": "请选择下方网站或服务,点击卡片即可开始访问。", "routes": [ { "domain": "app.example.com", @@ -236,13 +245,16 @@ data\backups\年年年年月月日日-时时分分秒秒-毫秒-auto-close.zip data\www\index.template.html ``` -可以直接修改页面标题、颜色、布局和其他静态内容。程序生成入口页时,会将所有网站卡片插入以下占位符: +可以直接修改颜色、布局和其他静态内容。程序生成入口页时,会填充以下占位符: ```text +{{NGINXMAN_PORTAL_SITE_NAME}} +{{NGINXMAN_PORTAL_HEADLINE}} +{{NGINXMAN_PORTAL_INTRODUCTION}} {{NGINXMAN_SERVICE_CARDS}} ``` -请勿删除或修改该占位符,否则程序无法生成入口页。网站名称和简介在写入 HTML 前会自动转义,避免特殊字符破坏页面结构。 +请勿删除或修改这些占位符,否则程序无法生成入口页。主页文字、网站名称和简介在写入 HTML 前会自动转义,避免特殊字符破坏页面结构。旧版本模板会在程序运行时自动升级到当前格式。 ## 从源码构建 diff --git a/WebPortal.cs b/WebPortal.cs index 77cc063..4443325 100644 --- a/WebPortal.cs +++ b/WebPortal.cs @@ -10,7 +10,10 @@ internal static class WebPortal private const string TemplateFileName = "index.template.html"; private const string OutputFileName = "index.html"; private const string ServiceCardsToken = "{{NGINXMAN_SERVICE_CARDS}}"; - private const string TemplateVersionMarker = ""; + private const string SiteNameToken = "{{NGINXMAN_PORTAL_SITE_NAME}}"; + private const string HeadlineToken = "{{NGINXMAN_PORTAL_HEADLINE}}"; + private const string IntroductionToken = "{{NGINXMAN_PORTAL_INTRODUCTION}}"; + private const string TemplateVersionMarker = ""; private static readonly UTF8Encoding Utf8WithoutBom = new(false); public static void ExtractTo(string dataDirectory) @@ -58,26 +61,42 @@ internal static class WebPortal } } - public static string RenderTo(string dataDirectory, IReadOnlyList routes) + public static string RenderTo( + string dataDirectory, + PortalContentSettings portalContent, + IReadOnlyList routes) { var targetDirectory = Path.Combine(dataDirectory, "www"); var templatePath = Path.Combine(targetDirectory, TemplateFileName); - if (!File.Exists(templatePath)) + if (!File.Exists(templatePath) || + !File.ReadAllText(templatePath, Encoding.UTF8).Contains( + TemplateVersionMarker, + StringComparison.Ordinal)) { ExtractTo(dataDirectory); } var template = File.ReadAllText(templatePath, Encoding.UTF8); - if (!template.Contains(ServiceCardsToken, StringComparison.Ordinal)) + var requiredTokens = new[] + { + ServiceCardsToken, + SiteNameToken, + HeadlineToken, + IntroductionToken + }; + var missingToken = requiredTokens.FirstOrDefault( + token => !template.Contains(token, StringComparison.Ordinal)); + if (missingToken is not null) { throw new InvalidOperationException( - $"网页模板缺少入口占位符 {ServiceCardsToken}: {templatePath}"); + $"网页模板缺少占位符 {missingToken}: {templatePath}"); } - var content = template.Replace( - ServiceCardsToken, - BuildServiceCards(routes), - StringComparison.Ordinal); + var content = template + .Replace(SiteNameToken, Encode(portalContent.SiteName), StringComparison.Ordinal) + .Replace(HeadlineToken, Encode(portalContent.Headline), StringComparison.Ordinal) + .Replace(IntroductionToken, Encode(portalContent.Introduction), StringComparison.Ordinal) + .Replace(ServiceCardsToken, BuildServiceCards(routes), StringComparison.Ordinal); var outputPath = Path.Combine(targetDirectory, OutputFileName); File.WriteAllText(outputPath, content, Utf8WithoutBom); return outputPath; diff --git a/nginxman.example.json b/nginxman.example.json index 125aaa3..9733a90 100644 --- a/nginxman.example.json +++ b/nginxman.example.json @@ -5,5 +5,8 @@ "errorLogPath": "logs/error.log", "portalDomain": "", "portalListenPort": 80, + "portalSiteName": "服务导航", + "portalHeadline": "欢迎访问统一服务入口", + "portalIntroduction": "请选择下方网站或服务,点击卡片即可开始访问。", "routes": [] } diff --git a/web/index.html b/web/index.html index b0b8f4d..5f54768 100644 --- a/web/index.html +++ b/web/index.html @@ -1,12 +1,12 @@ - + - + - Service Portal | 服务入口 + {{NGINXMAN_PORTAL_SITE_NAME}}