修复重定向问题

This commit is contained in:
ExplodingDragon
2025-11-21 21:29:58 +08:00
parent 01500f0101
commit ff45595fd2
4 changed files with 25 additions and 30 deletions

View File

@@ -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,

View File

@@ -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/

View File

@@ -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, ",") {

View File

@@ -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/")