From ff45595fd248c16952ceaf8f0a53517156ba3117 Mon Sep 17 00:00:00 2001 From: ExplodingDragon Date: Fri, 21 Nov 2025 21:29:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8D=E5=AE=9A=E5=90=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server/main.go | 3 +++ config.yaml | 12 +++--------- pkg/core/meta.go | 28 +++++++++++++--------------- tests/core_test.go | 12 ++++++------ 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 67defd6..bf7b1b4 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -70,6 +70,9 @@ func main() { log.Fatalln(err) } defer event.Close() + if config.Filters == nil { + config.Filters = map[string]map[string]any{} + } pageServer, err := pkg.NewPageServer( http.DefaultClient, backend, diff --git a/config.yaml b/config.yaml index 0f5183a..8618248 100644 --- a/config.yaml +++ b/config.yaml @@ -6,6 +6,9 @@ domain: example.com database: # 持久化存储配置 url: "memory://" + # 事件传递配置 +event: + url: "memory://" auth: type: gitea server: https://gitea.com @@ -29,12 +32,3 @@ page: 404: /path/to/html.gotmpl # 默认 500 页面模板 500: /path/to/html.gotmpl - -# 渲染器配置 -render: - enable: false -# 反向代理配置 -proxy: - enable: false -# 静态资源路径,可供任意网站使用 /.well-known/page-server/ 路径拉取文件 -static: /path/to/dir/ \ No newline at end of file diff --git a/pkg/core/meta.go b/pkg/core/meta.go index ccc9657..41c32bf 100644 --- a/pkg/core/meta.go +++ b/pkg/core/meta.go @@ -150,19 +150,7 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (* } func (s *ServerMeta) parsePageConfig(ctx context.Context, meta *PageMetaContent, vfs *PageVFS) error { - alias := make([]string, 0) - defer func(alias *[]string) { - meta.Alias = *alias - direct := *alias - if len(direct) > 0 { - meta.Filters = append(meta.Filters, Filter{ - Path: "**", - Type: "redirect", - Params: map[string]any{ - "targets": direct, - }, - }) - } + defer func() { meta.Filters = append(meta.Filters, Filter{ Path: "**", Type: "direct", @@ -170,7 +158,8 @@ func (s *ServerMeta) parsePageConfig(ctx context.Context, meta *PageMetaContent, "prefix": "", }, }) - }(&alias) + }() + alias := make([]string, 0) cname, err := vfs.ReadString(ctx, "CNAME") if cname != "" && err == nil { if al, ok := s.aliasCheck(cname); ok { @@ -201,7 +190,16 @@ func (s *ServerMeta) parsePageConfig(ctx context.Context, meta *PageMetaContent, return fmt.Errorf("invalid alias %s", item) } } - + if len(alias) > 0 { + meta.Filters = append(meta.Filters, Filter{ + Path: "**", + Type: "redirect", + Params: map[string]any{ + "targets": alias, + }, + }) + } + meta.Alias = alias // 处理自定义路由 for _, r := range cfg.Routes { for _, item := range strings.Split(r.Path, ",") { diff --git a/tests/core_test.go b/tests/core_test.go index c3de9bd..b3e321f 100644 --- a/tests/core_test.go +++ b/tests/core_test.go @@ -26,15 +26,15 @@ func Test_get_alias(t *testing.T) { server.AddFile("org1/repo1/gh-pages/index.html", "hello world") server.AddFile("org1/repo1/gh-pages/.pages.yaml", ` alias: - - www.example.org + - gopkg.d7z.net `) - _, resp, _ := server.OpenFile("https://www.example.org") + _, resp, _ := server.OpenFile("https://gopkg.d7z.net") assert.Equal(t, 404, resp.StatusCode) _, resp, _ = server.OpenFile("https://org1.example.com/repo1/") assert.Equal(t, 302, resp.StatusCode) - assert.Equal(t, "https://www.example.org/", resp.Header.Get("Location")) - data, _, err := server.OpenFile("https://www.example.org") + assert.Equal(t, "https://gopkg.d7z.net/", resp.Header.Get("Location")) + data, _, err := server.OpenFile("https://gopkg.d7z.net") assert.NoError(t, err) assert.Equal(t, "hello world", string(data)) @@ -42,11 +42,11 @@ alias: alias: - zzz.example.top `) - _, resp, _ = server.OpenFile("https://www.example.org") + _, resp, _ = server.OpenFile("https://gopkg.d7z.net") assert.Equal(t, 302, resp.StatusCode) assert.Equal(t, "https://zzz.example.top/", resp.Header.Get("Location")) - _, resp, _ = server.OpenFile("https://www.example.org") + _, resp, _ = server.OpenFile("https://gopkg.d7z.net") assert.Equal(t, 404, resp.StatusCode) _, resp, _ = server.OpenFile("https://org1.example.com/repo1/")