feat(pages): 认证页与门户页文案可配置,不再写死
需求:登录页与门户页的内容应可自定义。 实现(配置存数据卷、后台可改、留空回落内置默认,不配置行为不变): - pages.go:pageStr 读取 pageInfo;renderPortalPage 优先数据卷 <data>/portal/index.html 整页覆盖,否则用内置模板替换占位符; authPageInfo 提供登录页字段 - auth.go:登录页标题/副标题/徽标/页脚改为模板数据(含备案号) - gateway.go:kind=portal 的首页改由 admin-server /portal-page 渲染 (静态资源仍由 nginx 提供),使文案可配置 - portal/index.html:加入 <!--PAGE_TITLE--> / <!--HERO_TITLE--> / <!--HERO_SUBTITLE--> / <!--FOOTER--> 占位符 - 后台「站点信息」新增六项文案输入(含回填与保存),并提示整页覆盖方式 - README 新增「认证页与门户页文案(可配置,不写死)」含字段表与自检命令 测试:默认回落、配置取值去空白、认证页字段读取、数据卷覆盖优先、空白覆盖忽略。
This commit is contained in:
25
README.md
25
README.md
@ -412,6 +412,31 @@ NAVI_SSH_PASS='<密码>' bash deploy/zd-host.sh v50
|
||||
- 配置:`portal/nginx.conf.bak-<时间戳>`;`conf.yml` 的改动另有 `conf.yml.bak-*`
|
||||
- 数据:`conf.yml`、`history.json`、`gateway.yml`、`portal-auth.json` 都在数据卷,容器重建不丢
|
||||
|
||||
### 认证页与门户页文案(可配置,不写死)
|
||||
|
||||
原先登录页与门户页的标题/副标题/徽标/页脚都硬编码在代码与镜像里。现在统一从 `conf.yml`
|
||||
的 `pageInfo` 读取,**留空即回落内置默认值**(不配置时行为与之前一致):
|
||||
|
||||
| 字段 | 作用 | 默认 |
|
||||
| --- | --- | --- |
|
||||
| `authTitle` / `authSubtitle` / `authLogo` | 登录页标题、副标题、徽标文字 | 统一门户 / 登录后访问受保护的内部服务 / JF |
|
||||
| `portalTitle` | 门户页浏览器标题 | JianF Git - 个人开发环境 |
|
||||
| `heroTitle` / `heroSubtitle` | 门户页主标题、副标题 | 代码、博客与灵感之站 / … |
|
||||
| `icp` / `gongan` | 两页页脚备案号(与导航页共用) | — |
|
||||
|
||||
入口:管理后台 →「站点信息」→「认证页与门户页文案」。
|
||||
|
||||
**整页完全自定义**:把 `index.html` 放进数据卷的 `portal/` 目录即生效(优先于上述字段,
|
||||
用于改版式而非只改文案);`portal/` 下的 `portal.css`、favicon 等静态资源同样可覆盖。
|
||||
门户首页由 admin-server 渲染(`/portal-page`),静态资源仍由 nginx 提供。
|
||||
|
||||
自检(发布前确认占位符已被替换、页面无残留标记):
|
||||
|
||||
```bash
|
||||
curl -s https://<域名>/ | grep -c '<!--HERO' || echo '门户页占位符已替换'
|
||||
curl -s https://<域名>/login | grep -o '<h1>[^<]*</h1>' # 应为配置的标题或默认值
|
||||
```
|
||||
|
||||
## 部署需要的全部自定义文件
|
||||
|
||||
仓库即完整部署物料,没有额外依赖仓库外的文件:
|
||||
|
||||
@ -139,7 +139,7 @@ func safePortalRedirect(raw string) string {
|
||||
|
||||
var portalLoginTemplate = template.Must(template.New("login").Parse(`<!doctype html>
|
||||
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>统一门户登录</title>
|
||||
<title>{{.Title}} - 登录</title>
|
||||
<script>(function(){var s=localStorage.getItem('theme');var p=window.matchMedia('(prefers-color-scheme:dark)').matches;document.documentElement.setAttribute('data-theme',s||(p?'dark':'light'))})()</script>
|
||||
<style>
|
||||
/* 深浅色:默认跟随系统(prefers-color-scheme),手动切换后写入 localStorage。
|
||||
@ -161,16 +161,19 @@ button[type=submit]{width:100%;margin-top:18px;padding:11px;border:0;border-radi
|
||||
@media(prefers-reduced-motion:reduce){*{transition:none!important}}
|
||||
</style></head><body><main class="card">
|
||||
<button class="theme" type="button" onclick="(function(){var n=document.documentElement.getAttribute('data-theme')==='dark'?'light':'dark';document.documentElement.setAttribute('data-theme',n);localStorage.setItem('theme',n)})()" title="切换深浅色" aria-label="切换深浅色">◐</button>
|
||||
<div class="logo">JF</div><h1>统一门户</h1><p class="sub">登录后访问受保护的内部服务</p>
|
||||
<div class="logo">{{.Logo}}</div><h1>{{.Title}}</h1><p class="sub">{{.Subtitle}}</p>
|
||||
{{if .Error}}<div class="err">{{.Error}}</div>{{end}}
|
||||
<form method="post" action="/login?redirect={{.Redirect}}"><label for="u">用户名</label><input id="u" name="username" autocomplete="username" required autofocus><label for="p">密码</label><input id="p" type="password" name="password" autocomplete="current-password" required><button type="submit">登录</button></form>
|
||||
<div class="foot"><a href="https://beian.miit.gov.cn" target="_blank" rel="noopener">京ICP备00000000号-1</a></div></main></body></html>`))
|
||||
<div class="foot">{{if .Footer}}{{.Footer}}{{end}}</div></main></body></html>`))
|
||||
|
||||
func renderPortalLogin(w http.ResponseWriter, redirect, errText string) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
title, subtitle, logo, icp, gongan := authPageInfo()
|
||||
_ = portalLoginTemplate.Execute(w, map[string]string{
|
||||
"Redirect": url.QueryEscape(redirect), "Error": errText,
|
||||
"Title": title, "Subtitle": subtitle, "Logo": logo,
|
||||
"Footer": beianFooter(icp, gongan),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -877,6 +877,13 @@ func handlePageInfo(w http.ResponseWriter, r *http.Request) {
|
||||
ICP *string `json:"icp"`
|
||||
Gongan *string `json:"gongan"`
|
||||
NavLinks *[]map[string]any `json:"navLinks"`
|
||||
// 认证页与门户页的可配置文案(原先写死在代码/镜像里)
|
||||
AuthTitle *string `json:"authTitle"`
|
||||
AuthSubtitle *string `json:"authSubtitle"`
|
||||
AuthLogo *string `json:"authLogo"`
|
||||
PortalTitle *string `json:"portalTitle"`
|
||||
HeroTitle *string `json:"heroTitle"`
|
||||
HeroSubtitle *string `json:"heroSubtitle"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
jsonErr(w, 400, err.Error())
|
||||
@ -901,7 +908,23 @@ func handlePageInfo(w http.ResponseWriter, r *http.Request) {
|
||||
// 页脚为空(留空 = 交给模板)时,按备案号重新生成。
|
||||
if strings.TrimSpace(mapString(cfg.PageInfo, "footer")) == "" {
|
||||
cfg.PageInfo["footer"] = beianFooter(mapString(cfg.PageInfo, "icp"), mapString(cfg.PageInfo, "gongan"))
|
||||
} // 认证页 / 门户页文案:空串表示回落到内置默认值
|
||||
setPageStr := func(k string, v *string) {
|
||||
if v != nil {
|
||||
if strings.TrimSpace(*v) == "" {
|
||||
delete(cfg.PageInfo, k)
|
||||
} else {
|
||||
cfg.PageInfo[k] = strings.TrimSpace(*v)
|
||||
}
|
||||
}
|
||||
}
|
||||
setPageStr("authTitle", body.AuthTitle)
|
||||
setPageStr("authSubtitle", body.AuthSubtitle)
|
||||
setPageStr("authLogo", body.AuthLogo)
|
||||
setPageStr("portalTitle", body.PortalTitle)
|
||||
setPageStr("heroTitle", body.HeroTitle)
|
||||
setPageStr("heroSubtitle", body.HeroSubtitle)
|
||||
|
||||
if body.NavLinks != nil {
|
||||
cfg.PageInfo["navLinks"] = *body.NavLinks
|
||||
}
|
||||
@ -1206,6 +1229,8 @@ func main() {
|
||||
http.HandleFunc("/api/gateway/import", cors(handleGatewayImport))
|
||||
// 统一登录的账号设置(存数据卷,改完立即生效,不用重新部署)
|
||||
http.HandleFunc("/api/portal-auth", cors(handlePortalAuthSettings))
|
||||
// 门户首页渲染(可配置文案 + 数据卷整页覆盖),供 gateway 的 kind=portal 反代
|
||||
http.HandleFunc("/portal-page", handlePortalPage)
|
||||
// 历史记录管理:列表 / 清空全部 / 删除单条
|
||||
http.HandleFunc("/api/history", cors(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
|
||||
89
admin-server/pages.go
Normal file
89
admin-server/pages.go
Normal file
@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"html"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ── 认证页 / 门户页的可配置渲染 ──
|
||||
//
|
||||
// 原先这两页的文案写死在代码与镜像里(登录页标题、门户页标题/副标题/页脚…),
|
||||
// 改一次就得改代码重新部署。现在统一从 conf.yml 的 pageInfo 读取,
|
||||
// 缺省时回落到当前默认文案(即"不配置也照旧")。
|
||||
//
|
||||
// 覆盖优先级(门户页):
|
||||
// 1. <data>/portal/index.html —— 完全自定义整页(放进去即生效)
|
||||
// 2. 内置模板 + pageInfo 字段替换
|
||||
//
|
||||
// 门户页的静态资源(/css/portal.css、favicon)仍由 nginx 提供,
|
||||
// 因此自定义整页时把它们放在 <data>/portal/ 下即可覆盖。
|
||||
|
||||
const (
|
||||
defaultPortalTitle = "JianF Git - 个人开发环境"
|
||||
defaultPortalHero = "代码、博客与灵感之站"
|
||||
defaultPortalHeroSub = "JianF Git · 代码托管 · 技术博客 · 私有服务入口"
|
||||
defaultAuthTitle = "统一门户"
|
||||
defaultAuthSubtitle = "登录后访问受保护的内部服务"
|
||||
defaultAuthLogo = "JF"
|
||||
)
|
||||
|
||||
// pageStr 读取 pageInfo 中的字符串,缺失时用兜底值。
|
||||
func pageStr(info map[string]any, key, fallback string) string {
|
||||
if v := strings.TrimSpace(mapString(info, key)); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
// portalOverridePath 数据卷里的整页覆盖文件。
|
||||
func portalOverridePath() string {
|
||||
return filepath.Join(filepath.Dir(confFile), "portal", "index.html")
|
||||
}
|
||||
|
||||
// renderPortalPage 渲染门户首页:优先数据卷覆盖,其次内置模板 + pageInfo 替换。
|
||||
func renderPortalPage() []byte {
|
||||
if b, err := os.ReadFile(portalOverridePath()); err == nil && len(strings.TrimSpace(string(b))) > 0 {
|
||||
return b
|
||||
}
|
||||
builtin, err := os.ReadFile("/app/portal/index.html")
|
||||
if err != nil {
|
||||
return []byte("<!doctype html><meta charset=utf-8><title>Portal</title><h1>Portal</h1>")
|
||||
}
|
||||
cfg, _ := loadConfig()
|
||||
info := map[string]any{}
|
||||
if cfg != nil {
|
||||
info = cfg.PageInfo
|
||||
}
|
||||
rep := strings.NewReplacer(
|
||||
"<!--PAGE_TITLE-->", html.EscapeString(pageStr(info, "portalTitle", defaultPortalTitle)),
|
||||
"<!--HERO_TITLE-->", html.EscapeString(pageStr(info, "heroTitle", defaultPortalHero)),
|
||||
"<!--HERO_SUBTITLE-->", html.EscapeString(pageStr(info, "heroSubtitle", defaultPortalHeroSub)),
|
||||
"<!--FOOTER-->", beianFooter(mapString(info, "icp"), mapString(info, "gongan")),
|
||||
)
|
||||
out := rep.Replace(string(builtin))
|
||||
// 未做占位替换的旧模板(例如自备的 index.html)原样返回
|
||||
return []byte(out)
|
||||
}
|
||||
|
||||
// handlePortalPage 供 nginx 的 kind=portal 路由反代使用。
|
||||
func handlePortalPage(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
_, _ = w.Write(renderPortalPage())
|
||||
}
|
||||
|
||||
// authPageInfo 返回登录页所需字段(供 auth.go 渲染)。
|
||||
func authPageInfo() (title, subtitle, logo, icp, gongan string) {
|
||||
cfg, err := loadConfig()
|
||||
info := map[string]any{}
|
||||
if err == nil && cfg != nil {
|
||||
info = cfg.PageInfo
|
||||
}
|
||||
return pageStr(info, "authTitle", defaultAuthTitle),
|
||||
pageStr(info, "authSubtitle", defaultAuthSubtitle),
|
||||
pageStr(info, "authLogo", defaultAuthLogo),
|
||||
mapString(info, "icp"), mapString(info, "gongan")
|
||||
}
|
||||
67
admin-server/pages_test.go
Normal file
67
admin-server/pages_test.go
Normal file
@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 未配置任何文案时应回落到内置默认值(不配置也照旧)。
|
||||
func TestPageStrFallbacks(t *testing.T) {
|
||||
empty := map[string]any{}
|
||||
if got := pageStr(empty, "authTitle", defaultAuthTitle); got != defaultAuthTitle {
|
||||
t.Fatalf("空配置应回落默认,得到 %q", got)
|
||||
}
|
||||
info := map[string]any{"authTitle": " 内网入口 ", "heroTitle": ""}
|
||||
if got := pageStr(info, "authTitle", defaultAuthTitle); got != "内网入口" {
|
||||
t.Fatalf("应取配置值并去空白,得到 %q", got)
|
||||
}
|
||||
// 空串视为未配置
|
||||
if got := pageStr(info, "heroTitle", defaultPortalHero); got != defaultPortalHero {
|
||||
t.Fatalf("空串应回落默认,得到 %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// 认证页字段从 pageInfo 读取,缺失时回落。
|
||||
func TestAuthPageInfoFallback(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
old := confFile
|
||||
confFile = filepath.Join(dir, "conf.yml")
|
||||
defer func() { confFile = old }()
|
||||
if err := os.WriteFile(confFile, []byte("pageInfo:\n authTitle: 我的门户\n authLogo: AB\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
title, subtitle, logo, _, _ := authPageInfo()
|
||||
if title != "我的门户" || logo != "AB" {
|
||||
t.Fatalf("未读到配置值: %q %q", title, logo)
|
||||
}
|
||||
if subtitle != defaultAuthSubtitle {
|
||||
t.Fatalf("未配置的字段应回落默认,得到 %q", subtitle)
|
||||
}
|
||||
}
|
||||
|
||||
// 数据卷里的整页覆盖优先于内置模板。
|
||||
func TestPortalOverrideWins(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
old := confFile
|
||||
confFile = filepath.Join(dir, "conf.yml")
|
||||
defer func() { confFile = old }()
|
||||
if err := os.MkdirAll(filepath.Join(dir, "portal"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
custom := "<!doctype html><title>我的门户</title>"
|
||||
if err := os.WriteFile(filepath.Join(dir, "portal", "index.html"), []byte(custom), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := string(renderPortalPage()); !strings.Contains(got, "我的门户") {
|
||||
t.Fatalf("数据卷覆盖未生效: %q", got)
|
||||
}
|
||||
// 空白覆盖文件应被忽略(回落到内置渲染)
|
||||
if err := os.WriteFile(filepath.Join(dir, "portal", "index.html"), []byte(" \n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := string(renderPortalPage()); strings.Contains(got, "我的门户") {
|
||||
t.Fatalf("空白覆盖文件应被忽略")
|
||||
}
|
||||
}
|
||||
@ -168,6 +168,20 @@ html[data-theme="dark"] .toast{ box-shadow:0 10px 30px rgba(0,0,0,.5) }
|
||||
<div class="fg"><label>ICP 备案号</label><input id="p-icp" placeholder="京ICP备00000000号-1"></div>
|
||||
<div class="fg"><label>公安备案号(可选)</label><input id="p-gongan" placeholder="京公网安备00000000000000号"></div>
|
||||
</div>
|
||||
<details style="margin:12px 0">
|
||||
<summary style="cursor:pointer;font-size:13px;color:#475569">认证页与门户页文案(留空=用内置默认)</summary>
|
||||
<div class="row" style="margin-top:8px">
|
||||
<div class="fg"><label>登录页标题</label><input id="p-auth-title" placeholder="统一门户"></div>
|
||||
<div class="fg"><label>登录页副标题</label><input id="p-auth-sub" placeholder="登录后访问受保护的内部服务"></div>
|
||||
<div class="fg" style="max-width:160px"><label>登录页徽标</label><input id="p-auth-logo" placeholder="JF"></div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="fg"><label>门户页浏览器标题</label><input id="p-portal-title" placeholder="JianF Git - 个人开发环境"></div>
|
||||
<div class="fg"><label>门户页主标题</label><input id="p-hero-title" placeholder="代码、博客与灵感之站"></div>
|
||||
<div class="fg"><label>门户页副标题</label><input id="p-hero-sub" placeholder="JianF Git · 代码托管 · 技术博客"></div>
|
||||
</div>
|
||||
<p class="hint" style="margin:6px 0 0">整页完全自定义:把 <code>index.html</code> 放进数据卷的 <code>portal/</code> 目录即生效(优先于以上字段)。</p>
|
||||
</details>
|
||||
<details style="margin:12px 0">
|
||||
<summary style="cursor:pointer;font-size:13px;color:#475569">高级:自定义页脚 HTML</summary>
|
||||
<div class="fg" style="margin-top:8px">
|
||||
@ -468,6 +482,12 @@ async function loadPageInfo() {
|
||||
document.getElementById('p-desc').value = PAGE.description || '';
|
||||
const icp = PAGE.icp || parseIcp(PAGE.footer);
|
||||
const gongan = PAGE.gongan || parseGongan(PAGE.footer);
|
||||
const setIf = (id, v) => { const el = document.getElementById(id); if (el) el.value = v || ''; };
|
||||
|
||||
setIf('p-auth-title', PAGE.authTitle); setIf('p-auth-sub', PAGE.authSubtitle); setIf('p-auth-logo', PAGE.authLogo);
|
||||
|
||||
setIf('p-portal-title', PAGE.portalTitle); setIf('p-hero-title', PAGE.heroTitle); setIf('p-hero-sub', PAGE.heroSubtitle);
|
||||
|
||||
document.getElementById('p-icp').value = icp;
|
||||
document.getElementById('p-gongan').value = gongan;
|
||||
// 能从备案号字段还原时,高级 HTML 留空,保存时按模板重新生成
|
||||
@ -490,6 +510,18 @@ document.getElementById('save-info').addEventListener('click', async () => {
|
||||
gongan: document.getElementById('p-gongan').value,
|
||||
// 留空 = 交给服务端按备案号生成模板;填写则整段覆盖
|
||||
footer: document.getElementById('p-footer').value,
|
||||
authTitle: document.getElementById('p-auth-title').value.trim(),
|
||||
|
||||
authSubtitle: document.getElementById('p-auth-sub').value.trim(),
|
||||
|
||||
authLogo: document.getElementById('p-auth-logo').value.trim(),
|
||||
|
||||
portalTitle: document.getElementById('p-portal-title').value.trim(),
|
||||
|
||||
heroTitle: document.getElementById('p-hero-title').value.trim(),
|
||||
|
||||
heroSubtitle: document.getElementById('p-hero-sub').value.trim(),
|
||||
|
||||
navLinks: (PAGE.navLinks || []).filter(l => l && (l.name || l.url)),
|
||||
};
|
||||
const r = await fetch(A + '/pageinfo', {
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>JianF Git - 个人开发环境</title>
|
||||
<title><!--PAGE_TITLE--></title>
|
||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
|
||||
<link rel="icon" href="/favicon.ico" sizes="any">
|
||||
<link rel="stylesheet" href="/css/portal.css">
|
||||
@ -38,8 +38,8 @@
|
||||
<span class="hero-badge">
|
||||
<span class="hero-dot"></span>
|
||||
个人开发环境 </span>
|
||||
<h1 class="hero-title">代码、博客与<span class="hero-grad">灵感</span>之站</h1>
|
||||
<p class="hero-sub">JianF Git · 代码托管 · 技术博客 · 私有服务入口</p>
|
||||
<h1 class="hero-title"><!--HERO_TITLE--></h1>
|
||||
<p class="hero-sub"><!--HERO_SUBTITLE--></p>
|
||||
<div class="hero-actions">
|
||||
<a href="https://gitea.example.com" class="btn btn-primary btn-lg">
|
||||
<svg viewBox="0 0 24 24" width="17" height="17" fill="currentColor" aria-hidden="true"><path d="m14.6 16.6l4.6-4.6l-4.6-4.6L16 6l6 6l-6 6zm-5.2 0L4.8 12l4.6-4.6L8 6l-6 6l6 6z"/></svg>
|
||||
@ -86,10 +86,6 @@
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div class="footer">
|
||||
<a href="https://beian.miit.gov.cn" target="_blank">京ICP备00000000号-1</a>
|
||||
|
|
||||
<a href="http://www.beian.gov.cn/portal/registerSystemInfo" target="_blank">豫公网安备 00000000000000号</a>
|
||||
</div>
|
||||
<div class="footer"><!--FOOTER--></div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user