修复重定向问题
This commit is contained in:
@@ -70,6 +70,9 @@ func main() {
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
defer event.Close()
|
defer event.Close()
|
||||||
|
if config.Filters == nil {
|
||||||
|
config.Filters = map[string]map[string]any{}
|
||||||
|
}
|
||||||
pageServer, err := pkg.NewPageServer(
|
pageServer, err := pkg.NewPageServer(
|
||||||
http.DefaultClient,
|
http.DefaultClient,
|
||||||
backend,
|
backend,
|
||||||
|
|||||||
12
config.yaml
12
config.yaml
@@ -6,6 +6,9 @@ domain: example.com
|
|||||||
database:
|
database:
|
||||||
# 持久化存储配置
|
# 持久化存储配置
|
||||||
url: "memory://"
|
url: "memory://"
|
||||||
|
# 事件传递配置
|
||||||
|
event:
|
||||||
|
url: "memory://"
|
||||||
auth:
|
auth:
|
||||||
type: gitea
|
type: gitea
|
||||||
server: https://gitea.com
|
server: https://gitea.com
|
||||||
@@ -29,12 +32,3 @@ page:
|
|||||||
404: /path/to/html.gotmpl
|
404: /path/to/html.gotmpl
|
||||||
# 默认 500 页面模板
|
# 默认 500 页面模板
|
||||||
500: /path/to/html.gotmpl
|
500: /path/to/html.gotmpl
|
||||||
|
|
||||||
# 渲染器配置
|
|
||||||
render:
|
|
||||||
enable: false
|
|
||||||
# 反向代理配置
|
|
||||||
proxy:
|
|
||||||
enable: false
|
|
||||||
# 静态资源路径,可供任意网站使用 /.well-known/page-server/ 路径拉取文件
|
|
||||||
static: /path/to/dir/
|
|
||||||
@@ -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 {
|
func (s *ServerMeta) parsePageConfig(ctx context.Context, meta *PageMetaContent, vfs *PageVFS) error {
|
||||||
alias := make([]string, 0)
|
defer func() {
|
||||||
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,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
meta.Filters = append(meta.Filters, Filter{
|
meta.Filters = append(meta.Filters, Filter{
|
||||||
Path: "**",
|
Path: "**",
|
||||||
Type: "direct",
|
Type: "direct",
|
||||||
@@ -170,7 +158,8 @@ func (s *ServerMeta) parsePageConfig(ctx context.Context, meta *PageMetaContent,
|
|||||||
"prefix": "",
|
"prefix": "",
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}(&alias)
|
}()
|
||||||
|
alias := make([]string, 0)
|
||||||
cname, err := vfs.ReadString(ctx, "CNAME")
|
cname, err := vfs.ReadString(ctx, "CNAME")
|
||||||
if cname != "" && err == nil {
|
if cname != "" && err == nil {
|
||||||
if al, ok := s.aliasCheck(cname); ok {
|
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)
|
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 _, r := range cfg.Routes {
|
||||||
for _, item := range strings.Split(r.Path, ",") {
|
for _, item := range strings.Split(r.Path, ",") {
|
||||||
|
|||||||
@@ -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/index.html", "hello world")
|
||||||
server.AddFile("org1/repo1/gh-pages/.pages.yaml", `
|
server.AddFile("org1/repo1/gh-pages/.pages.yaml", `
|
||||||
alias:
|
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)
|
assert.Equal(t, 404, resp.StatusCode)
|
||||||
|
|
||||||
_, resp, _ = server.OpenFile("https://org1.example.com/repo1/")
|
_, resp, _ = server.OpenFile("https://org1.example.com/repo1/")
|
||||||
assert.Equal(t, 302, resp.StatusCode)
|
assert.Equal(t, 302, resp.StatusCode)
|
||||||
assert.Equal(t, "https://www.example.org/", resp.Header.Get("Location"))
|
assert.Equal(t, "https://gopkg.d7z.net/", resp.Header.Get("Location"))
|
||||||
data, _, err := server.OpenFile("https://www.example.org")
|
data, _, err := server.OpenFile("https://gopkg.d7z.net")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "hello world", string(data))
|
assert.Equal(t, "hello world", string(data))
|
||||||
|
|
||||||
@@ -42,11 +42,11 @@ alias:
|
|||||||
alias:
|
alias:
|
||||||
- zzz.example.top
|
- 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, 302, resp.StatusCode)
|
||||||
assert.Equal(t, "https://zzz.example.top/", resp.Header.Get("Location"))
|
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)
|
assert.Equal(t, 404, resp.StatusCode)
|
||||||
|
|
||||||
_, resp, _ = server.OpenFile("https://org1.example.com/repo1/")
|
_, resp, _ = server.OpenFile("https://org1.example.com/repo1/")
|
||||||
|
|||||||
Reference in New Issue
Block a user