重构项目
This commit is contained in:
6
main.go
6
main.go
@@ -10,6 +10,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"gopkg.d7z.net/gitea-pages/pkg/core"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"gopkg.d7z.net/gitea-pages/pkg"
|
"gopkg.d7z.net/gitea-pages/pkg"
|
||||||
@@ -64,7 +65,10 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
giteaServer := pkg.NewPageServer(gitea, *options)
|
backend := core.NewCacheBackend(gitea, options.CacheMeta, options.CacheMetaTTL,
|
||||||
|
options.CacheBlob, options.CacheBlobLimit,
|
||||||
|
)
|
||||||
|
giteaServer := pkg.NewPageServer(backend, *options)
|
||||||
defer giteaServer.Close()
|
defer giteaServer.Close()
|
||||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
|
||||||
defer stop()
|
defer stop()
|
||||||
|
|||||||
@@ -34,13 +34,13 @@ func (c *CacheBackend) Close() error {
|
|||||||
func NewCacheBackend(
|
func NewCacheBackend(
|
||||||
backend Backend,
|
backend Backend,
|
||||||
cacheMeta kv.KV,
|
cacheMeta kv.KV,
|
||||||
cacheMetaTtl time.Duration,
|
cacheMetaTTL time.Duration,
|
||||||
|
|
||||||
cacheBlob cache.Cache,
|
cacheBlob cache.Cache,
|
||||||
cacheBlobLimit uint64,
|
cacheBlobLimit uint64,
|
||||||
) *CacheBackend {
|
) *CacheBackend {
|
||||||
repoCache := tools.NewCache[map[string]string](cacheMeta, "repos", cacheMetaTtl)
|
repoCache := tools.NewCache[map[string]string](cacheMeta, "repos", cacheMetaTTL)
|
||||||
branchCache := tools.NewCache[map[string]*BranchInfo](cacheMeta, "branches", cacheMetaTtl)
|
branchCache := tools.NewCache[map[string]*BranchInfo](cacheMeta, "branches", cacheMetaTTL)
|
||||||
return &CacheBackend{
|
return &CacheBackend{
|
||||||
backend: backend,
|
backend: backend,
|
||||||
cacheRepo: repoCache,
|
cacheRepo: repoCache,
|
||||||
@@ -115,7 +115,7 @@ func (c *CacheBackend) Open(ctx context.Context, client *http.Client, owner, rep
|
|||||||
}
|
}
|
||||||
return &http.Response{
|
return &http.Response{
|
||||||
Status: "200 OK",
|
Status: "200 OK",
|
||||||
StatusCode: 200,
|
StatusCode: http.StatusOK,
|
||||||
Proto: "HTTP/1.1",
|
Proto: "HTTP/1.1",
|
||||||
ProtoMajor: 1,
|
ProtoMajor: 1,
|
||||||
ProtoMinor: 1,
|
ProtoMinor: 1,
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ func (p *PageDomain) ParseDomainMeta(ctx context.Context, domain, path, branch s
|
|||||||
|
|
||||||
func (p *PageDomain) returnMeta(ctx context.Context, owner, repo, branch string, path []string) (*PageDomainContent, error) {
|
func (p *PageDomain) returnMeta(ctx context.Context, owner, repo, branch string, path []string) (*PageDomainContent, error) {
|
||||||
result := &PageDomainContent{}
|
result := &PageDomainContent{}
|
||||||
meta, err := p.GetMeta(ctx, owner, repo, branch)
|
meta, vfs, err := p.GetMeta(ctx, owner, repo, branch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
zap.L().Debug("查询错误", zap.Error(err))
|
zap.L().Debug("查询错误", zap.Error(err))
|
||||||
if meta != nil {
|
if meta != nil {
|
||||||
@@ -82,7 +82,7 @@ func (p *PageDomain) returnMeta(ctx context.Context, owner, repo, branch string,
|
|||||||
result.PageMetaContent = meta
|
result.PageMetaContent = meta
|
||||||
result.Owner = owner
|
result.Owner = owner
|
||||||
result.Repo = repo
|
result.Repo = repo
|
||||||
result.PageVFS = NewPageVFS(p.client, p, result.Owner, result.Repo, result.CommitID)
|
result.PageVFS = vfs
|
||||||
result.Path = strings.Join(path, "/")
|
result.Path = strings.Join(path, "/")
|
||||||
|
|
||||||
if err = p.alias.Bind(ctx, meta.Alias, result.Owner, result.Repo, branch); err != nil {
|
if err = p.alias.Bind(ctx, meta.Alias, result.Owner, result.Repo, branch); err != nil {
|
||||||
|
|||||||
208
pkg/core/meta.go
208
pkg/core/meta.go
@@ -3,7 +3,6 @@ package core
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -32,183 +31,200 @@ type ServerMeta struct {
|
|||||||
|
|
||||||
client *http.Client
|
client *http.Client
|
||||||
cache *tools.Cache[PageMetaContent]
|
cache *tools.Cache[PageMetaContent]
|
||||||
|
|
||||||
locker *utils.Locker
|
locker *utils.Locker
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerMeta(client *http.Client, backend Backend, kv kv.KV, domain string, ttl time.Duration) *ServerMeta {
|
func NewServerMeta(client *http.Client, backend Backend, kv kv.KV, domain string, ttl time.Duration) *ServerMeta {
|
||||||
return &ServerMeta{
|
return &ServerMeta{
|
||||||
backend, domain, client,
|
Backend: backend,
|
||||||
tools.NewCache[PageMetaContent](kv, "pages/meta", ttl),
|
Domain: domain,
|
||||||
utils.NewLocker(),
|
client: client,
|
||||||
|
cache: tools.NewCache[PageMetaContent](kv, "pages/meta", ttl),
|
||||||
|
locker: utils.NewLocker(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*PageMetaContent, error) {
|
func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*PageMetaContent, *PageVFS, error) {
|
||||||
rel := NewPageMetaContent()
|
|
||||||
repos, err := s.Repos(ctx, owner)
|
repos, err := s.Repos(ctx, owner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defBranch := repos[repo]
|
defBranch := repos[repo]
|
||||||
if defBranch == "" {
|
if defBranch == "" {
|
||||||
return nil, os.ErrNotExist
|
return nil, nil, os.ErrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
if branch == "" {
|
if branch == "" {
|
||||||
branch = defBranch
|
branch = defBranch
|
||||||
}
|
}
|
||||||
|
|
||||||
branches, err := s.Branches(ctx, owner, repo)
|
branches, err := s.Branches(ctx, owner, repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
info := branches[branch]
|
info := branches[branch]
|
||||||
if info == nil {
|
if info == nil {
|
||||||
return nil, os.ErrNotExist
|
return nil, nil, os.ErrNotExist
|
||||||
}
|
}
|
||||||
rel.CommitID = info.ID
|
|
||||||
rel.LastModified = info.LastModified
|
|
||||||
|
|
||||||
key := fmt.Sprintf("%s/%s/%s", owner, repo, branch)
|
key := fmt.Sprintf("%s/%s/%s", owner, repo, branch)
|
||||||
if cache, find := s.cache.Load(ctx, key); find {
|
|
||||||
|
if cache, found := s.cache.Load(ctx, key); found {
|
||||||
if cache.IsPage {
|
if cache.IsPage {
|
||||||
return rel, nil
|
return &cache, NewPageVFS(s.client, s.Backend, owner, repo, cache.CommitID), nil
|
||||||
} else {
|
|
||||||
return nil, os.ErrNotExist
|
|
||||||
}
|
}
|
||||||
|
return nil, nil, os.ErrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
mux := s.locker.Open(key)
|
mux := s.locker.Open(key)
|
||||||
mux.Lock()
|
mux.Lock()
|
||||||
defer mux.Unlock()
|
defer mux.Unlock()
|
||||||
if cache, find := s.cache.Load(ctx, key); find {
|
|
||||||
|
if cache, found := s.cache.Load(ctx, key); found {
|
||||||
if cache.IsPage {
|
if cache.IsPage {
|
||||||
return rel, nil
|
return &cache, NewPageVFS(s.client, s.Backend, owner, repo, cache.CommitID), nil
|
||||||
} else {
|
|
||||||
return nil, os.ErrNotExist
|
|
||||||
}
|
}
|
||||||
|
return nil, nil, os.ErrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
// 确定存在 index.html , 否则跳过
|
rel := NewEmptyPageMetaContent()
|
||||||
if find, _ := s.FileExists(ctx, owner, repo, rel.CommitID, "index.html"); !find {
|
vfs := NewPageVFS(s.client, s.Backend, owner, repo, info.ID)
|
||||||
|
rel.CommitID = info.ID
|
||||||
|
rel.LastModified = info.LastModified
|
||||||
|
|
||||||
|
// 检查是否存在 index.html
|
||||||
|
if exists, _ := vfs.Exists(ctx, "index.html"); !exists {
|
||||||
rel.IsPage = false
|
rel.IsPage = false
|
||||||
_ = s.cache.Store(ctx, key, *rel)
|
_ = s.cache.Store(ctx, key, *rel)
|
||||||
return nil, os.ErrNotExist
|
return nil, nil, os.ErrNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
rel.IsPage = true
|
rel.IsPage = true
|
||||||
errCall := func(err error) error {
|
|
||||||
rel.IsPage = false
|
|
||||||
rel.ErrorMsg = err.Error()
|
|
||||||
_ = s.cache.Store(ctx, key, *rel)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// 添加默认跳过的内容
|
// 添加默认跳过的内容
|
||||||
for _, defIgnore := range rel.Ignore {
|
for _, defIgnore := range rel.Ignore {
|
||||||
rel.ignoreL = append(rel.ignoreL, glob.MustCompile(defIgnore))
|
rel.ignoreL = append(rel.ignoreL, glob.MustCompile(defIgnore))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析配置
|
// 解析配置
|
||||||
if data, err := s.ReadString(ctx, owner, repo, rel.CommitID, ".pages.yaml"); err == nil {
|
if err := s.parsePageConfig(ctx, rel, vfs); err != nil {
|
||||||
|
rel.IsPage = false
|
||||||
|
rel.ErrorMsg = err.Error()
|
||||||
|
_ = s.cache.Store(ctx, key, *rel)
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理 CNAME 文件
|
||||||
|
if err := s.parseCNAME(ctx, rel, vfs); err != nil {
|
||||||
|
rel.IsPage = false
|
||||||
|
rel.ErrorMsg = err.Error()
|
||||||
|
_ = s.cache.Store(ctx, key, *rel)
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rel.Alias = utils.ClearDuplicates(rel.Alias)
|
||||||
|
rel.Ignore = utils.ClearDuplicates(rel.Ignore)
|
||||||
|
_ = s.cache.Store(ctx, key, *rel)
|
||||||
|
return rel, vfs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *ServerMeta) parsePageConfig(ctx context.Context, rel *PageMetaContent, vfs *PageVFS) error {
|
||||||
|
data, err := vfs.ReadString(ctx, ".pages.yaml")
|
||||||
|
if err != nil {
|
||||||
|
zap.L().Debug("failed to read meta data", zap.String("error", err.Error()))
|
||||||
|
return nil // 配置文件不存在不是错误
|
||||||
|
}
|
||||||
|
|
||||||
cfg := new(PageConfig)
|
cfg := new(PageConfig)
|
||||||
if err = yaml.Unmarshal([]byte(data), cfg); err != nil {
|
if err = yaml.Unmarshal([]byte(data), cfg); err != nil {
|
||||||
return nil, errCall(err)
|
return errors.Wrap(err, "parse .pages.yaml failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
rel.VRoute = cfg.VirtualRoute
|
rel.VRoute = cfg.VirtualRoute
|
||||||
// 处理 CNAME
|
|
||||||
|
// 处理别名
|
||||||
for _, cname := range cfg.Alias {
|
for _, cname := range cfg.Alias {
|
||||||
cname = strings.TrimSpace(cname)
|
if err := s.addAlias(rel, cname); err != nil {
|
||||||
if regexpHostname.MatchString(cname) && !strings.HasSuffix(strings.ToLower(cname), strings.ToLower(s.Domain)) {
|
return err
|
||||||
rel.Alias = append(rel.Alias, cname)
|
|
||||||
} else {
|
|
||||||
return nil, errCall(errors.New("invalid alias name " + cname))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理渲染器
|
// 处理渲染器
|
||||||
for sType, pattern := range cfg.Renders() {
|
for sType, pattern := range cfg.Renders() {
|
||||||
var r Render
|
r := GetRender(sType)
|
||||||
if r = GetRender(sType); r == nil {
|
if r == nil {
|
||||||
return nil, errCall(errors.Errorf("render not found %s", sType))
|
return errors.Errorf("render not found %s", sType)
|
||||||
}
|
}
|
||||||
if g, err := glob.Compile(strings.TrimSpace(pattern)); err == nil {
|
|
||||||
|
g, err := glob.Compile(strings.TrimSpace(pattern))
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "compile render pattern failed: %s", pattern)
|
||||||
|
}
|
||||||
|
|
||||||
rel.rendersL = append(rel.rendersL, &renderCompiler{
|
rel.rendersL = append(rel.rendersL, &renderCompiler{
|
||||||
regex: g,
|
regex: g,
|
||||||
Render: r,
|
Render: r,
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
return nil, errCall(err)
|
|
||||||
}
|
|
||||||
rel.Renders[sType] = append(rel.Renders[sType], pattern)
|
rel.Renders[sType] = append(rel.Renders[sType], pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理跳过内容
|
// 处理跳过内容
|
||||||
for _, pattern := range cfg.Ignores() {
|
for _, pattern := range cfg.Ignores() {
|
||||||
if g, err := glob.Compile(pattern); err == nil {
|
g, err := glob.Compile(pattern)
|
||||||
rel.ignoreL = append(rel.ignoreL, g)
|
if err != nil {
|
||||||
} else {
|
return errors.Wrapf(err, "compile ignore pattern failed: %s", pattern)
|
||||||
return nil, errCall(err)
|
|
||||||
}
|
}
|
||||||
|
rel.ignoreL = append(rel.ignoreL, g)
|
||||||
rel.Ignore = append(rel.Ignore, pattern)
|
rel.Ignore = append(rel.Ignore, pattern)
|
||||||
}
|
}
|
||||||
// 处理反向代理 (清理内容,符合 /<item>)
|
|
||||||
|
// 处理反向代理
|
||||||
for path, backend := range cfg.ReverseProxy {
|
for path, backend := range cfg.ReverseProxy {
|
||||||
path = filepath.ToSlash(filepath.Clean(path))
|
path = filepath.ToSlash(filepath.Clean(path))
|
||||||
if !strings.HasPrefix(path, "/") {
|
if !strings.HasPrefix(path, "/") {
|
||||||
path = "/" + path
|
path = "/" + path
|
||||||
}
|
}
|
||||||
path = strings.TrimSuffix(path, "/")
|
path = strings.TrimSuffix(path, "/")
|
||||||
var rURL *url.URL
|
|
||||||
if rURL, err = url.Parse(backend); err != nil {
|
rURL, err := url.Parse(backend)
|
||||||
return nil, errCall(err)
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "parse backend url failed: %s", backend)
|
||||||
}
|
}
|
||||||
|
|
||||||
if rURL.Scheme != "http" && rURL.Scheme != "https" {
|
if rURL.Scheme != "http" && rURL.Scheme != "https" {
|
||||||
return nil, errCall(errors.New("invalid backend url " + backend))
|
return errors.Errorf("invalid backend url scheme: %s", backend)
|
||||||
}
|
}
|
||||||
|
|
||||||
rel.Proxy[path] = rURL.String()
|
rel.Proxy[path] = rURL.String()
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// 不存在配置,但也可以重定向
|
return nil
|
||||||
zap.L().Debug("failed to read meta data", zap.String("error", err.Error()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 兼容 github 的 CNAME 模式
|
func (s *ServerMeta) parseCNAME(ctx context.Context, rel *PageMetaContent, vfs *PageVFS) error {
|
||||||
if cname, err := s.ReadString(ctx, owner, repo, rel.CommitID, "CNAME"); err == nil {
|
cname, err := vfs.ReadString(ctx, "CNAME")
|
||||||
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.Store(ctx, key, *rel)
|
|
||||||
return rel, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *ServerMeta) ReadString(ctx context.Context, owner, repo, branch, path string) (string, error) {
|
|
||||||
resp, err := s.Open(ctx, s.client, owner, repo, branch, path, nil)
|
|
||||||
if resp != nil {
|
|
||||||
defer resp.Body.Close()
|
|
||||||
}
|
|
||||||
if err != nil || resp == nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return "", os.ErrNotExist
|
|
||||||
}
|
|
||||||
all, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil // CNAME 文件不存在是正常情况
|
||||||
}
|
}
|
||||||
return string(all), nil
|
if err := s.addAlias(rel, cname); err != nil {
|
||||||
|
zap.L().Debug("指定的 CNAME 不合法", zap.String("cname", cname), zap.Error(err))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServerMeta) FileExists(ctx context.Context, owner, repo, branch, path string) (bool, error) {
|
func (s *ServerMeta) addAlias(rel *PageMetaContent, cname string) error {
|
||||||
resp, err := s.Open(ctx, s.client, owner, repo, branch, path, nil)
|
cname = strings.TrimSpace(cname)
|
||||||
if resp != nil {
|
if !regexpHostname.MatchString(cname) {
|
||||||
defer resp.Body.Close()
|
return errors.New("invalid domain name format")
|
||||||
}
|
}
|
||||||
if err != nil || resp == nil {
|
|
||||||
return false, err
|
if strings.HasSuffix(strings.ToLower(cname), strings.ToLower(s.Domain)) {
|
||||||
|
return errors.New("alias cannot be subdomain of main domain")
|
||||||
}
|
}
|
||||||
if resp.StatusCode == http.StatusOK {
|
|
||||||
return true, nil
|
rel.Alias = append(rel.Alias, cname)
|
||||||
}
|
return nil
|
||||||
return false, nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ type PageMetaContent struct {
|
|||||||
ignoreL []glob.Glob
|
ignoreL []glob.Glob
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPageMetaContent() *PageMetaContent {
|
func NewEmptyPageMetaContent() *PageMetaContent {
|
||||||
return &PageMetaContent{
|
return &PageMetaContent{
|
||||||
IsPage: false,
|
IsPage: false,
|
||||||
Proxy: make(map[string]string),
|
Proxy: make(map[string]string),
|
||||||
|
|||||||
@@ -73,3 +73,11 @@ func (p *PageVFS) Read(ctx context.Context, path string) ([]byte, error) {
|
|||||||
defer open.Close()
|
defer open.Close()
|
||||||
return io.ReadAll(open)
|
return io.ReadAll(open)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *PageVFS) ReadString(ctx context.Context, path string) (string, error) {
|
||||||
|
read, err := p.Read(ctx, path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(read), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Domain struct {
|
|
||||||
Org string `json:"org"`
|
|
||||||
Repo string `json:"repo"`
|
|
||||||
Branch string `json:"branch"` // commit id or branch
|
|
||||||
Path string `json:"path"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var portExp = regexp.MustCompile(`:\d+$`)
|
|
||||||
|
|
||||||
type DomainParser struct {
|
|
||||||
baseDomain string
|
|
||||||
defaultBranch string
|
|
||||||
alias *DomainAlias
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *DomainParser) ParseDomains(request *http.Request) ([]Domain, error) {
|
|
||||||
host := portExp.ReplaceAllString(strings.ToLower(request.Host), "")
|
|
||||||
path := strings.Split(strings.Trim(request.URL.Path, "/"), "/")
|
|
||||||
branch := request.URL.Query().Get("branch")
|
|
||||||
if branch == "" {
|
|
||||||
branch = d.defaultBranch
|
|
||||||
}
|
|
||||||
result := make([]Domain, 0)
|
|
||||||
if strings.HasSuffix(host, d.baseDomain) {
|
|
||||||
org := strings.TrimSuffix(host, d.baseDomain)
|
|
||||||
if len(path) > 1 {
|
|
||||||
// repo.base.com/path
|
|
||||||
result = append(result, Domain{
|
|
||||||
Org: org,
|
|
||||||
Repo: path[0],
|
|
||||||
Branch: branch,
|
|
||||||
Path: strings.Join(path[1:], "/"),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
// repo.base.com/
|
|
||||||
result = append(result, Domain{
|
|
||||||
Org: org,
|
|
||||||
Repo: host,
|
|
||||||
Branch: branch,
|
|
||||||
Path: strings.Join(path, "/"),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
if find, _ := d.alias.Query(request.Context(), host); find != nil {
|
|
||||||
result = append(result, Domain{
|
|
||||||
Org: find.Owner,
|
|
||||||
Repo: find.Repo,
|
|
||||||
Branch: find.Branch,
|
|
||||||
Path: request.URL.Path,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -12,7 +13,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Render interface {
|
type Render interface {
|
||||||
Render(w http.ResponseWriter, r *http.Request, input io.Reader) error
|
Render(ctx context.Context, w http.ResponseWriter, r *http.Request, input io.Reader, meta *PageDomainContent) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterRender(fType string, r Render) {
|
func RegisterRender(fType string, r Render) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package renders
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -16,14 +17,17 @@ func init() {
|
|||||||
core.RegisterRender("gotemplate", &GoTemplate{})
|
core.RegisterRender("gotemplate", &GoTemplate{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g GoTemplate) Render(w http.ResponseWriter, r *http.Request, input io.Reader) error {
|
func (g GoTemplate) Render(ctx context.Context, w http.ResponseWriter, r *http.Request, input io.Reader, meta *core.PageDomainContent) error {
|
||||||
dataB, err := io.ReadAll(input)
|
data, err := io.ReadAll(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
|
parse, err := utils.NewTemplate().Funcs(map[string]any{
|
||||||
parse, err := utils.NewTemplate(string(dataB))
|
"template": func(path string) (any, error) {
|
||||||
|
return meta.ReadString(ctx, path)
|
||||||
|
},
|
||||||
|
}).Parse(string(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package pkg
|
package pkg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
@@ -15,16 +17,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"gopkg.d7z.net/middleware/cache"
|
|
||||||
"gopkg.d7z.net/middleware/kv"
|
|
||||||
|
|
||||||
stdErr "errors"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"gopkg.d7z.net/gitea-pages/pkg/core"
|
"gopkg.d7z.net/gitea-pages/pkg/core"
|
||||||
"gopkg.d7z.net/gitea-pages/pkg/utils"
|
"gopkg.d7z.net/gitea-pages/pkg/utils"
|
||||||
|
"gopkg.d7z.net/middleware/cache"
|
||||||
|
"gopkg.d7z.net/middleware/kv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var portExp = regexp.MustCompile(`:\d+$`)
|
var portExp = regexp.MustCompile(`:\d+$`)
|
||||||
@@ -94,9 +91,6 @@ type Server struct {
|
|||||||
var staticPrefix = "/.well-known/page-server/"
|
var staticPrefix = "/.well-known/page-server/"
|
||||||
|
|
||||||
func NewPageServer(backend core.Backend, options ServerOptions) *Server {
|
func NewPageServer(backend core.Backend, options ServerOptions) *Server {
|
||||||
backend = core.NewCacheBackend(backend, options.CacheMeta, options.CacheMetaTTL,
|
|
||||||
options.CacheBlob, options.CacheBlobLimit,
|
|
||||||
)
|
|
||||||
svcMeta := core.NewServerMeta(options.HTTPClient, backend, options.CacheMeta, options.Domain, options.CacheMetaTTL)
|
svcMeta := core.NewServerMeta(options.HTTPClient, backend, options.CacheMeta, options.Domain, options.CacheMetaTTL)
|
||||||
pageMeta := core.NewPageDomain(svcMeta, core.NewDomainAlias(options.Alias), options.Domain, options.DefaultBranch)
|
pageMeta := core.NewPageDomain(svcMeta, core.NewDomainAlias(options.Alias), options.Domain, options.DefaultBranch)
|
||||||
var fs http.Handler
|
var fs http.Handler
|
||||||
@@ -135,24 +129,22 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
|||||||
|
|
||||||
func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error {
|
func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error {
|
||||||
ctx := request.Context()
|
ctx := request.Context()
|
||||||
domainHost := portExp.ReplaceAllString(strings.ToLower(request.Host), "")
|
domain := portExp.ReplaceAllString(strings.ToLower(request.Host), "")
|
||||||
|
meta, err := s.meta.ParseDomainMeta(ctx, domain, request.URL.Path, request.URL.Query().Get("branch"))
|
||||||
meta, err := s.meta.ParseDomainMeta(ctx, domainHost, request.URL.Path, request.URL.Query().Get("branch"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
zap.L().Debug("new request", zap.Any("request path", meta.Path))
|
zap.L().Debug("new request", zap.Any("request path", meta.Path))
|
||||||
if len(meta.Alias) > 0 && !slices.Contains(meta.Alias, domainHost) {
|
if len(meta.Alias) > 0 && !slices.Contains(meta.Alias, domain) {
|
||||||
// 重定向到配置的地址
|
// 重定向到配置的地址
|
||||||
zap.L().Debug("redirect", zap.Any("src", request.Host), zap.Any("dst", meta.Alias[0]))
|
zap.L().Debug("redirect", zap.Any("src", request.Host), zap.Any("dst", meta.Alias[0]))
|
||||||
http.Redirect(writer, request, fmt.Sprintf("https://%s/%s", meta.Alias[0], meta.Path), http.StatusFound)
|
http.Redirect(writer, request, fmt.Sprintf("https://%s/%s", meta.Alias[0], meta.Path), http.StatusFound)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// 处理反向代理
|
|
||||||
if s.options.EnableProxy && s.Proxy(writer, request, meta) {
|
if s.options.EnableProxy && s.Proxy(writer, request, meta) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// 在非反向代理时处理目录访问
|
|
||||||
if strings.HasSuffix(meta.Path, "/") || meta.Path == "" {
|
if strings.HasSuffix(meta.Path, "/") || meta.Path == "" {
|
||||||
meta.Path += "index.html"
|
meta.Path += "index.html"
|
||||||
}
|
}
|
||||||
@@ -162,66 +154,62 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
|
|||||||
}
|
}
|
||||||
if meta.IgnorePath(meta.Path) {
|
if meta.IgnorePath(meta.Path) {
|
||||||
zap.L().Debug("ignore path", zap.Any("request", request.RequestURI), zap.Any("meta.path", meta.Path))
|
zap.L().Debug("ignore path", zap.Any("request", request.RequestURI), zap.Any("meta.path", meta.Path))
|
||||||
err = os.ErrNotExist
|
return os.ErrNotExist
|
||||||
}
|
|
||||||
type resp struct {
|
|
||||||
IsError bool
|
|
||||||
Path string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
callPath := []resp{{false, meta.Path}}
|
var resp *http.Response
|
||||||
|
var path string
|
||||||
|
failback := []string{meta.Path, meta.Path + "/index.html"}
|
||||||
if meta.VRoute {
|
if meta.VRoute {
|
||||||
callPath = append(callPath, resp{false, "index.html"})
|
failback = append(failback, "index.html")
|
||||||
} else {
|
|
||||||
callPath = append(callPath, resp{false, meta.Path + "/index.html"})
|
|
||||||
}
|
}
|
||||||
callPath = append(callPath, resp{true, "404.html"})
|
failback = append(failback, "404.html")
|
||||||
|
for _, p := range failback {
|
||||||
var callResp *http.Response
|
resp, err = meta.NativeOpen(request.Context(), p, nil)
|
||||||
callErr := os.ErrNotExist
|
if err != nil {
|
||||||
var callRespMeta resp
|
if resp != nil {
|
||||||
for _, r := range callPath {
|
resp.Body.Close()
|
||||||
callResp, callErr = meta.NativeOpen(request.Context(), r.Path, nil)
|
|
||||||
if callErr != nil {
|
|
||||||
if callResp != nil {
|
|
||||||
_ = callResp.Body.Close()
|
|
||||||
}
|
}
|
||||||
if !errors.Is(callErr, os.ErrNotExist) {
|
if !errors.Is(err, os.ErrNotExist) {
|
||||||
zap.L().Debug("error", zap.Any("error", callErr))
|
zap.L().Debug("error", zap.Any("error", err))
|
||||||
}
|
}
|
||||||
callRespMeta = r
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
path = p
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if callResp == nil {
|
if resp == nil {
|
||||||
return os.ErrNotExist
|
return os.ErrNotExist
|
||||||
}
|
}
|
||||||
if callErr != nil {
|
defer resp.Body.Close()
|
||||||
// 回退失败
|
|
||||||
return callErr
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
render := meta.TryRender(meta.Path)
|
if path == "404.html" && request.URL.Path != "/404.html" {
|
||||||
writer.Header().Set("Content-Type", callResp.Header.Get("Content-Type"))
|
|
||||||
if callRespMeta.IsError {
|
|
||||||
render = meta.TryRender(meta.Path)
|
|
||||||
writer.WriteHeader(http.StatusNotFound)
|
writer.WriteHeader(http.StatusNotFound)
|
||||||
} else if render == nil {
|
|
||||||
lastMod, err := time.Parse(http.TimeFormat, callResp.Header.Get("Last-Modified"))
|
|
||||||
if seekResp, ok := callResp.Body.(io.ReadSeeker); ok && err == nil {
|
|
||||||
http.ServeContent(writer, request, filepath.Base(callRespMeta.Path), lastMod, seekResp)
|
|
||||||
}
|
}
|
||||||
} else {
|
ctx, cancel := context.WithTimeout(request.Context(), 3*time.Second)
|
||||||
defer callResp.Body.Close()
|
defer cancel()
|
||||||
return render.Render(writer, request, callResp.Body)
|
if render := meta.TryRender(path); render != nil {
|
||||||
|
return render.Render(ctx, writer, request, resp.Body, meta)
|
||||||
}
|
}
|
||||||
|
writer.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
|
||||||
|
lastMod, err := time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified"))
|
||||||
|
if err == nil {
|
||||||
|
if seeker, ok := resp.Body.(io.ReadSeeker); ok && !(path == "404.html" && request.URL.Path != "/404.html") {
|
||||||
|
http.ServeContent(writer, request, filepath.Base(path), lastMod, seeker)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
_, err = io.Copy(writer, resp.Body)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) Proxy(writer http.ResponseWriter, request *http.Request, meta *core.PageDomainContent) bool {
|
func (s *Server) Proxy(writer http.ResponseWriter, request *http.Request, meta *core.PageDomainContent) bool {
|
||||||
for prefix, backend := range meta.Proxy {
|
|
||||||
proxyPath := "/" + meta.Path
|
proxyPath := "/" + meta.Path
|
||||||
|
for prefix, backend := range meta.Proxy {
|
||||||
if strings.HasPrefix(proxyPath, prefix) {
|
if strings.HasPrefix(proxyPath, prefix) {
|
||||||
targetPath := strings.TrimPrefix(proxyPath, prefix)
|
targetPath := strings.TrimPrefix(proxyPath, prefix)
|
||||||
if !strings.HasPrefix(targetPath, "/") {
|
if !strings.HasPrefix(targetPath, "/") {
|
||||||
@@ -250,7 +238,7 @@ func (s *Server) Proxy(writer http.ResponseWriter, request *http.Request, meta *
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Close() error {
|
func (s *Server) Close() error {
|
||||||
return stdErr.Join(
|
return errors.Join(
|
||||||
s.options.CacheBlob.Close(),
|
s.options.CacheBlob.Close(),
|
||||||
s.options.CacheMeta.Close(),
|
s.options.CacheMeta.Close(),
|
||||||
s.options.Alias.Close(),
|
s.options.Alias.Close(),
|
||||||
|
|||||||
@@ -27,17 +27,17 @@ func NewTemplateInject(r *http.Request, def map[string]any) map[string]any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func MustTemplate(data string) *template.Template {
|
func MustTemplate(data string) *template.Template {
|
||||||
newTemplate, err := NewTemplate(data)
|
parse, err := NewTemplate().Parse(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return newTemplate
|
return parse
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTemplate(data string) (*template.Template, error) {
|
func NewTemplate() *template.Template {
|
||||||
funcMap := sprig.FuncMap()
|
funcMap := sprig.FuncMap()
|
||||||
delete(funcMap, "env")
|
delete(funcMap, "env")
|
||||||
delete(funcMap, "expandenv")
|
delete(funcMap, "expandenv")
|
||||||
t := template.New("tmpl").Funcs(funcMap)
|
t := template.New("tmpl").Funcs(funcMap)
|
||||||
return t.Parse(data)
|
return t
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user