Make portal and proxy settings configurable
This commit is contained in:
+82
-50
@@ -33,12 +33,17 @@ public sealed class NginxManager
|
||||
_config = LoadOrCreateConfig();
|
||||
ConfigureBundledNginx();
|
||||
SaveConfig();
|
||||
RenderConfig();
|
||||
}
|
||||
|
||||
public string ConfigPath => _configPath;
|
||||
|
||||
public IReadOnlyList<RouteEntry> Routes => _config.Routes.AsReadOnly();
|
||||
|
||||
public string PortalDomain => _config.PortalDomain;
|
||||
|
||||
public int PortalListenPort => _config.PortalListenPort;
|
||||
|
||||
public string NginxExePath
|
||||
{
|
||||
get => _config.NginxExePath;
|
||||
@@ -51,6 +56,24 @@ public sealed class NginxManager
|
||||
|
||||
public bool IsRunning => Process.GetProcessesByName("nginx").Any();
|
||||
|
||||
public void UpdatePortalSettings(string domain, int listenPort)
|
||||
{
|
||||
domain = NormalizeOptionalDomain(domain);
|
||||
listenPort = NormalizePort(listenPort, "入口页监听端口");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(domain) &&
|
||||
_config.Routes.Any(route =>
|
||||
route.Domain.Equals(domain, StringComparison.OrdinalIgnoreCase) &&
|
||||
route.ListenPort == listenPort))
|
||||
{
|
||||
throw new InvalidOperationException("入口页域名和端口不能与现有代理映射完全相同。");
|
||||
}
|
||||
|
||||
_config.PortalDomain = domain;
|
||||
_config.PortalListenPort = listenPort;
|
||||
SaveConfig();
|
||||
}
|
||||
|
||||
public void AddOrUpdateRoute(
|
||||
string domain,
|
||||
string upstream,
|
||||
@@ -60,8 +83,10 @@ public sealed class NginxManager
|
||||
{
|
||||
domain = NormalizeDomain(domain);
|
||||
upstream = NormalizeUpstream(upstream);
|
||||
listenPort = NormalizePort(listenPort, "监听端口");
|
||||
websiteName = NormalizeRequiredText(websiteName, "网站名称");
|
||||
description = NormalizeRequiredText(description, "网站简介");
|
||||
EnsureRouteDoesNotConflictWithPortal(domain, listenPort);
|
||||
|
||||
var index = _config.Routes.FindIndex(x => x.Domain.Equals(domain, StringComparison.OrdinalIgnoreCase));
|
||||
if (index >= 0)
|
||||
@@ -101,8 +126,10 @@ public sealed class NginxManager
|
||||
oldDomain = NormalizeDomain(oldDomain);
|
||||
newDomain = NormalizeDomain(newDomain);
|
||||
upstream = NormalizeUpstream(upstream);
|
||||
listenPort = NormalizePort(listenPort, "监听端口");
|
||||
websiteName = NormalizeRequiredText(websiteName, "网站名称");
|
||||
description = NormalizeRequiredText(description, "网站简介");
|
||||
EnsureRouteDoesNotConflictWithPortal(newDomain, listenPort);
|
||||
|
||||
var oldIndex = _config.Routes.FindIndex(x => x.Domain.Equals(oldDomain, StringComparison.OrdinalIgnoreCase));
|
||||
if (oldIndex < 0)
|
||||
@@ -149,11 +176,6 @@ public sealed class NginxManager
|
||||
|
||||
public string RenderConfig()
|
||||
{
|
||||
if (_config.Routes.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException("至少需要 1 条域名映射才能生成配置。");
|
||||
}
|
||||
|
||||
var confPath = ResolvePath(_config.NginxConfPath);
|
||||
EnsureDirectory(Path.GetDirectoryName(confPath));
|
||||
|
||||
@@ -206,9 +228,10 @@ public sealed class NginxManager
|
||||
return DefaultConfig();
|
||||
}
|
||||
|
||||
if (config.Routes is null || config.Routes.Count == 0)
|
||||
config.Routes ??= [];
|
||||
if (config.PortalListenPort is < 1 or > 65535)
|
||||
{
|
||||
config.Routes = DefaultConfig().Routes;
|
||||
config.PortalListenPort = 80;
|
||||
}
|
||||
|
||||
NormalizeDataPaths(config);
|
||||
@@ -278,17 +301,20 @@ public sealed class NginxManager
|
||||
sb.AppendLine(" keepalive_timeout 65;");
|
||||
sb.AppendLine();
|
||||
|
||||
var portalRoot = Path.Combine(_dataDirectory, "www").Replace('\\', '/');
|
||||
sb.AppendLine(" server {");
|
||||
sb.AppendLine(" listen 80;");
|
||||
sb.AppendLine(" server_name www.m3frame.com;");
|
||||
sb.AppendLine($" root \"{portalRoot}\";");
|
||||
sb.AppendLine(" index index.html;");
|
||||
sb.AppendLine(" location / {");
|
||||
sb.AppendLine(" try_files $uri $uri/ /index.html;");
|
||||
sb.AppendLine(" }");
|
||||
sb.AppendLine(" }");
|
||||
sb.AppendLine();
|
||||
if (!string.IsNullOrWhiteSpace(_config.PortalDomain))
|
||||
{
|
||||
var portalRoot = Path.Combine(_dataDirectory, "www").Replace('\\', '/');
|
||||
sb.AppendLine(" server {");
|
||||
sb.AppendLine($" listen {_config.PortalListenPort};");
|
||||
sb.AppendLine($" server_name {_config.PortalDomain};");
|
||||
sb.AppendLine($" root \"{portalRoot}\";");
|
||||
sb.AppendLine(" index index.html;");
|
||||
sb.AppendLine(" location / {");
|
||||
sb.AppendLine(" try_files $uri $uri/ /index.html;");
|
||||
sb.AppendLine(" }");
|
||||
sb.AppendLine(" }");
|
||||
sb.AppendLine();
|
||||
}
|
||||
|
||||
foreach (var route in _config.Routes)
|
||||
{
|
||||
@@ -316,25 +342,9 @@ public sealed class NginxManager
|
||||
NginxConfPath = Path.Combine("data", "nginx.conf"),
|
||||
AccessLogPath = Path.Combine("data", "logs", "access.log"),
|
||||
ErrorLogPath = Path.Combine("data", "logs", "error.log"),
|
||||
Routes =
|
||||
[
|
||||
new RouteEntry
|
||||
{
|
||||
Domain = "team.m3frame.com",
|
||||
Upstream = "http://127.0.0.1:803",
|
||||
ListenPort = 80,
|
||||
WebsiteName = "Team Workspace",
|
||||
Description = "团队协作、项目推进与知识沉淀,让成员始终连接在同一节奏。"
|
||||
},
|
||||
new RouteEntry
|
||||
{
|
||||
Domain = "git.m3frame.com",
|
||||
Upstream = "http://127.0.0.1:3062",
|
||||
ListenPort = 80,
|
||||
WebsiteName = "Git Forge",
|
||||
Description = "代码托管、版本审查与持续交付,把每一次提交变成可靠的软件。"
|
||||
}
|
||||
]
|
||||
PortalDomain = string.Empty,
|
||||
PortalListenPort = 80,
|
||||
Routes = []
|
||||
};
|
||||
|
||||
private void ConfigureBundledNginx()
|
||||
@@ -382,22 +392,12 @@ public sealed class NginxManager
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(route.WebsiteName))
|
||||
{
|
||||
route.WebsiteName = route.Domain.ToLowerInvariant() switch
|
||||
{
|
||||
"team.m3frame.com" => "Team Workspace",
|
||||
"git.m3frame.com" => "Git Forge",
|
||||
_ => route.Domain
|
||||
};
|
||||
route.WebsiteName = route.Domain;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(route.Description))
|
||||
{
|
||||
route.Description = route.Domain.ToLowerInvariant() switch
|
||||
{
|
||||
"team.m3frame.com" => "团队协作、项目推进与知识沉淀,让成员始终连接在同一节奏。",
|
||||
"git.m3frame.com" => "代码托管、版本审查与持续交付,把每一次提交变成可靠的软件。",
|
||||
_ => $"访问 {route.Domain} 服务。"
|
||||
};
|
||||
route.Description = $"访问 {route.Domain} 服务。";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -435,7 +435,37 @@ public sealed class NginxManager
|
||||
throw new ArgumentException("域名不能为空");
|
||||
}
|
||||
|
||||
return domain.Trim().TrimEnd('.');
|
||||
domain = domain.Trim().TrimEnd('.');
|
||||
if (domain.Contains("://", StringComparison.Ordinal) || domain.Contains('/') ||
|
||||
domain.Any(char.IsWhiteSpace) || Uri.CheckHostName(domain) == UriHostNameType.Unknown)
|
||||
{
|
||||
throw new ArgumentException("请输入有效域名,例如 app.example.com。");
|
||||
}
|
||||
|
||||
return domain;
|
||||
}
|
||||
|
||||
private static string NormalizeOptionalDomain(string domain) =>
|
||||
string.IsNullOrWhiteSpace(domain) ? string.Empty : NormalizeDomain(domain);
|
||||
|
||||
private static int NormalizePort(int port, string fieldName)
|
||||
{
|
||||
if (port is < 1 or > 65535)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(port), $"{fieldName}必须在 1 到 65535 之间。");
|
||||
}
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
private void EnsureRouteDoesNotConflictWithPortal(string domain, int listenPort)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(_config.PortalDomain) &&
|
||||
_config.PortalDomain.Equals(domain, StringComparison.OrdinalIgnoreCase) &&
|
||||
_config.PortalListenPort == listenPort)
|
||||
{
|
||||
throw new InvalidOperationException("代理映射不能与入口页使用相同的域名和监听端口。");
|
||||
}
|
||||
}
|
||||
|
||||
private static string NormalizeUpstream(string upstream)
|
||||
@@ -472,6 +502,8 @@ public sealed class NginxManagerConfig
|
||||
public string NginxConfPath { get; set; } = "nginx.conf";
|
||||
public string AccessLogPath { get; set; } = Path.Combine("logs", "access.log");
|
||||
public string ErrorLogPath { get; set; } = Path.Combine("logs", "error.log");
|
||||
public string PortalDomain { get; set; } = string.Empty;
|
||||
public int PortalListenPort { get; set; } = 80;
|
||||
public List<RouteEntry> Routes { get; set; } = [];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user