清理代码 & 支持 ignore

This commit is contained in:
dragon
2025-05-09 14:50:10 +08:00
parent 9413188aa9
commit ef31433d91
14 changed files with 405 additions and 118 deletions

46
pkg/core/config.go Normal file
View File

@@ -0,0 +1,46 @@
package core
import "strings"
type PageConfig struct {
Alias []string `yaml:"alias"` // 重定向地址
Render map[string]string `yaml:"templates"` // 渲染器地址
VirtualRoute bool `yaml:"v-route"` // 是否使用虚拟路由(任何路径均使用 /index.html 返回 200 响应)
ReverseProxy map[string]string `yaml:"proxy"` // 反向代理路由
Ignore string `yaml:"ignore"` // 跳过展示的内容
}
func (p *PageConfig) Ignores() []string {
i := make([]string, 0)
if p.Ignore == "" {
return i
}
for _, line := range strings.Split(p.Ignore, "\n") {
for _, item := range strings.Split(line, ",") {
item = strings.TrimSpace(item)
if item == "" {
continue
}
i = append(i, item)
}
}
return i
}
func (p *PageConfig) Renders() map[string]string {
result := make(map[string]string)
for sType, patterns := range p.Render {
for _, line := range strings.Split(patterns, "\n") {
for _, item := range strings.Split(line, ",") {
item = strings.TrimSpace(item)
if item == "" {
continue
}
result[sType] = item
}
}
}
return result
}

View File

@@ -4,9 +4,10 @@ import (
"os"
"strings"
"github.com/pkg/errors"
"gopkg.d7z.net/gitea-pages/pkg/utils"
"github.com/pkg/errors"
"go.uber.org/zap"
)
@@ -83,6 +84,10 @@ func (p *PageDomain) ReturnMeta(owner string, repo string, branch string, path [
return rel, nil
} else {
zap.L().Debug("查询错误", zap.Error(err))
if meta != nil {
// 解析错误汇报
return nil, errors.New(meta.ErrorMsg)
}
}
return nil, errors.Wrapf(os.ErrNotExist, strings.Join(path, "/"))
return nil, errors.Wrap(os.ErrNotExist, strings.Join(path, "/"))
}

View File

@@ -1,12 +1,12 @@
package core
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"
@@ -35,75 +35,12 @@ type ServerMeta struct {
locker *utils.Locker
}
type renderCompiler struct {
regex glob.Glob
Render
}
// PageConfig 配置
type PageConfig struct {
Alias []string `yaml:"required"` // 重定向地址
Renders map[string]string `yaml:"templates"` // 渲染器地址
VirtualRoute bool `yaml:"v-route"` // 是否使用虚拟路由(任何路径均使用 /index.html 返回 200 响应)
ReverseProxy map[string]string `yaml:"proxy"` // 反向代理路由
}
type PageMetaContent struct {
CommitID string `json:"commit-id"` // 提交 COMMIT ID
LastModified time.Time `json:"last-modified"` // 上次更新时间
IsPage bool `json:"is-page"` // 是否为 Page
ErrorMsg string `json:"error"` // 错误消息
VRoute bool `yaml:"v-route"` // 虚拟路由
Alias []string `yaml:"aliases"` // 重定向
Proxy map[string]string `yaml:"proxy"` // 反向代理
Renders map[string][]string `json:"renders"` // 配置的渲染器
rendersL []*renderCompiler
}
func (m *PageMetaContent) From(data string) error {
err := json.Unmarshal([]byte(data), m)
clear(m.rendersL)
for key, gs := range m.Renders {
for _, g := range gs {
m.rendersL = append(m.rendersL, &renderCompiler{
regex: glob.MustCompile(g),
Render: GetRender(key),
})
}
}
return err
}
func (m *PageMetaContent) TryRender(path ...string) Render {
for _, s := range path {
for _, compiler := range m.rendersL {
if compiler.regex.Match(s) {
return compiler.Render
}
}
}
return nil
}
func (m *PageMetaContent) String() string {
marshal, _ := json.Marshal(m)
return string(marshal)
}
func NewServerMeta(client *http.Client, backend Backend, kv utils.KVConfig, domain string, ttl time.Duration) *ServerMeta {
return &ServerMeta{backend, domain, client, kv, ttl, utils.NewLocker()}
}
func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, error) {
rel := &PageMetaContent{
IsPage: false,
Proxy: make(map[string]string),
Alias: make([]string, 0),
Renders: make(map[string][]string),
}
rel := NewPageMetaContent()
if repos, err := s.Repos(owner); err != nil {
return nil, err
} else {
@@ -152,6 +89,7 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
}
}
// 确定存在 index.html , 否则跳过
if find, _ := s.FileExists(owner, repo, rel.CommitID, "index.html"); !find {
rel.IsPage = false
_ = s.cache.Put(key, rel.String(), s.ttl)
@@ -166,13 +104,14 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
return nil, err
}
// 解析配置
if data, err := s.ReadString(owner, repo, rel.CommitID, ".pages.yaml"); err == nil {
cfg := new(PageConfig)
if err = yaml.Unmarshal([]byte(data), cfg); err != nil {
return errFunc(err)
}
rel.Alias = cfg.Alias
rel.VRoute = cfg.VirtualRoute
// 处理 CNAME
for _, cname := range cfg.Alias {
cname = strings.TrimSpace(cname)
if regexpHostname.MatchString(cname) && !strings.HasSuffix(strings.ToLower(cname), strings.ToLower(s.Domain)) {
@@ -181,25 +120,34 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
return errFunc(errors.New("invalid alias name " + cname))
}
}
for sType, patterns := range cfg.Renders {
if r := GetRender(sType); r != nil {
for _, pattern := range strings.Split(patterns, ",") {
rel.Renders[sType] = append(rel.Renders[sType], pattern)
if g, err := glob.Compile(strings.TrimSpace(pattern)); err == nil {
rel.rendersL = append(rel.rendersL, &renderCompiler{
regex: g,
Render: r,
})
} else {
return errFunc(err)
}
}
// 处理渲染器
for sType, pattern := range cfg.Renders() {
var r Render
if r = GetRender(sType); r == nil {
return errFunc(errors.Errorf("render not found %s", sType))
}
if g, err := glob.Compile(strings.TrimSpace(pattern)); err == nil {
rel.rendersL = append(rel.rendersL, &renderCompiler{
regex: g,
Render: r,
})
} else {
return errFunc(err)
}
rel.Renders[sType] = append(rel.Renders[sType], pattern)
}
// 处理跳过内容
for _, pattern := range cfg.Ignores() {
if g, err := glob.Compile(pattern); err == nil {
rel.ignoreL = append(rel.ignoreL, g)
} else {
return errFunc(err)
}
rel.Ignore = append(rel.Ignore, pattern)
}
// 处理反向代理 (清理内容,符合 /<item>)
for path, backend := range cfg.ReverseProxy {
path = strings.TrimSpace(path)
path = strings.ReplaceAll(path, "//", "/")
path = strings.ReplaceAll(path, "//", "/")
path = filepath.ToSlash(filepath.Clean(path))
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
@@ -220,6 +168,17 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
zap.L().Debug("failed to read meta data", zap.String("error", err.Error()))
}
// 兼容 github 的 CNAME 模式
if cname, err := s.ReadString(owner, repo, rel.CommitID, "CNAME"); err == nil {
cname = strings.TrimSpace(cname)
if regexpHostname.MatchString(cname) && !strings.HasSuffix(strings.ToLower(cname), strings.ToLower(s.Domain)) {
rel.Alias = append(rel.Alias, cname)
} else {
zap.L().Debug("指定的 CNAME 不合法", zap.String("cname", cname))
}
}
rel.Alias = utils.ClearDuplicates(rel.Alias)
rel.Ignore = utils.ClearDuplicates(rel.Ignore)
_ = s.cache.Put(key, rel.String(), s.ttl)
return rel, nil
}

85
pkg/core/meta_content.go Normal file
View File

@@ -0,0 +1,85 @@
package core
import (
"encoding/json"
"time"
"github.com/gobwas/glob"
)
type renderCompiler struct {
regex glob.Glob
Render
}
// PageConfig 配置
type PageMetaContent struct {
CommitID string `json:"commit-id"` // 提交 COMMIT ID
LastModified time.Time `json:"last-modified"` // 上次更新时间
IsPage bool `json:"is-page"` // 是否为 Page
ErrorMsg string `json:"error"` // 错误消息
VRoute bool `yaml:"v-route"` // 虚拟路由
Proxy map[string]string `yaml:"proxy"` // 反向代理
Renders map[string][]string `json:"renders"` // 配置的渲染器
Alias []string `yaml:"aliases"` // 重定向
Ignore []string `yaml:"ignore"` // 跳过的内容
rendersL []*renderCompiler
ignoreL []glob.Glob
}
func NewPageMetaContent() *PageMetaContent {
return &PageMetaContent{
IsPage: false,
Proxy: make(map[string]string),
Alias: make([]string, 0),
Renders: make(map[string][]string),
Ignore: []string{".*", "**/.*"},
}
}
func (m *PageMetaContent) From(data string) error {
err := json.Unmarshal([]byte(data), m)
clear(m.rendersL)
for key, gs := range m.Renders {
for _, g := range gs {
m.rendersL = append(m.rendersL, &renderCompiler{
regex: glob.MustCompile(g),
Render: GetRender(key),
})
}
}
clear(m.ignoreL)
for _, g := range m.Ignore {
m.ignoreL = append(m.ignoreL, glob.MustCompile(g))
}
return err
}
func (m *PageMetaContent) IsIgnore(path string) bool {
for _, g := range m.ignoreL {
if g.Match(path) {
return true
}
}
return false
}
func (m *PageMetaContent) TryRender(path ...string) Render {
for _, s := range path {
for _, compiler := range m.rendersL {
if compiler.regex.Match(s) {
return compiler.Render
}
}
}
return nil
}
func (m *PageMetaContent) String() string {
marshal, _ := json.Marshal(m)
return string(marshal)
}

View File

@@ -118,18 +118,22 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
http.Redirect(writer, request, fmt.Sprintf("https://%s/%s", meta.Alias[0], meta.Path), http.StatusFound)
return nil
}
for prefix, backend := range meta.Proxy {
if strings.HasPrefix(meta.Path, prefix) {
targetPath := strings.TrimPrefix(meta.Path, prefix)
proxyPath := "/" + meta.Path
if strings.HasPrefix(proxyPath, prefix) {
targetPath := strings.TrimPrefix(proxyPath, prefix)
if !strings.HasPrefix(targetPath, "/") {
targetPath = "/" + targetPath
}
zap.L().Debug("命中反向代理", zap.Any("prefix", prefix), zap.Any("backend", backend),
zap.Any("path", meta.Path), zap.Any("target", targetPath))
u, _ := url.Parse(backend)
request.URL.Path = targetPath
request.RequestURI = request.URL.RequestURI()
u, _ := url.Parse(backend)
httputil.NewSingleHostReverseProxy(u).ServeHTTP(writer, request)
proxy := httputil.NewSingleHostReverseProxy(u)
proxy.Transport = s.options.HttpClient.Transport
zap.L().Debug("命中反向代理", zap.Any("prefix", prefix), zap.Any("backend", backend),
zap.Any("path", proxyPath), zap.Any("target", fmt.Sprintf("%s%s", u, targetPath)))
proxy.ServeHTTP(writer, request)
return nil
}
}
@@ -137,6 +141,10 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
if request.Method != "GET" {
return os.ErrNotExist
}
if meta.IsIgnore(meta.Path) {
zap.L().Debug("ignore path", zap.Any("request", request.RequestURI), zap.Any("meta.path", meta.Path))
return os.ErrNotExist
}
result, err := s.reader.Open(meta.Owner, meta.Repo, meta.CommitID, meta.Path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
@@ -222,6 +230,5 @@ func (s *Server) Close() error {
if err := s.options.Cache.Close(); err != nil {
return err
}
return nil
return s.backend.Close()
}

15
pkg/utils/array.go Normal file
View File

@@ -0,0 +1,15 @@
package utils
func ClearDuplicates[T comparable](slice []T) []T {
seen := make(map[T]bool)
for _, val := range slice {
seen[val] = true
}
var result []T
for key := range seen {
result = append(result, key)
}
return result
}