优化部分代码
This commit is contained in:
50
pkg/core/alias.go
Normal file
50
pkg/core/alias.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||
)
|
||||
|
||||
type Alias struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Branch string `json:"branch"`
|
||||
}
|
||||
|
||||
type DomainAlias struct {
|
||||
config utils.Config
|
||||
}
|
||||
|
||||
func NewDomainAlias(config utils.Config) *DomainAlias {
|
||||
return &DomainAlias{config: config}
|
||||
}
|
||||
|
||||
func (a *DomainAlias) Query(domain string) (*Alias, error) {
|
||||
get, err := a.config.Get("alias/" + domain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rel := &Alias{}
|
||||
if err = json.Unmarshal([]byte(get), rel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rel, nil
|
||||
}
|
||||
|
||||
func (a *DomainAlias) Bind(domain, owner, repo, branch string) error {
|
||||
save := &Alias{
|
||||
Owner: owner,
|
||||
Repo: repo,
|
||||
Branch: branch,
|
||||
}
|
||||
saveB, err := json.Marshal(save)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return a.config.Put("domain/"+domain, string(saveB), utils.TtlKeep)
|
||||
}
|
||||
|
||||
func (a *DomainAlias) Unbind(domain string) error {
|
||||
return a.config.Delete("domain/" + domain)
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAny(t *testing.T) {
|
||||
data := make(map[string]string)
|
||||
err := json.Unmarshal([]byte(`{}`), &data)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
98
pkg/core/domain.go
Normal file
98
pkg/core/domain.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var portExp = regexp.MustCompile(`:\d+$`)
|
||||
|
||||
type PageDomain struct {
|
||||
*ServerMeta
|
||||
|
||||
alias *DomainAlias
|
||||
baseDomain string
|
||||
defaultBranch string
|
||||
}
|
||||
|
||||
func NewPageDomain(meta *ServerMeta, config utils.Config, baseDomain, defaultBranch string) *PageDomain {
|
||||
return &PageDomain{
|
||||
baseDomain: baseDomain,
|
||||
defaultBranch: defaultBranch,
|
||||
ServerMeta: meta,
|
||||
alias: NewDomainAlias(config),
|
||||
}
|
||||
}
|
||||
|
||||
type PageDomainContent struct {
|
||||
*PageMetaContent
|
||||
|
||||
Owner string
|
||||
Repo string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (m *PageDomainContent) CacheKey() string {
|
||||
return fmt.Sprintf("%s/%s/%s/%s", m.Owner, m.Repo, m.CommitID, m.Path)
|
||||
}
|
||||
|
||||
func (p *PageDomain) ParseDomainMeta(domain, path, branch string) (*PageDomainContent, error) {
|
||||
if branch == "" {
|
||||
branch = p.defaultBranch
|
||||
}
|
||||
domain = portExp.ReplaceAllString(strings.ToLower(domain), "")
|
||||
pathS := strings.Split(strings.TrimPrefix(path, "/"), "/")
|
||||
|
||||
if !strings.HasSuffix(domain, "."+p.baseDomain) {
|
||||
alias, err := p.alias.Query(domain)
|
||||
if err != nil {
|
||||
zap.L().Warn("未知域名", zap.String("base", p.baseDomain), zap.String("domain", domain))
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
zap.L().Debug("命中别名", zap.String("domain", domain), zap.Any("alias", alias))
|
||||
return p.ReturnMeta(alias.Owner, alias.Repo, alias.Branch, pathS)
|
||||
}
|
||||
owner := strings.TrimSuffix(domain, "."+p.baseDomain)
|
||||
repo := pathS[0]
|
||||
if repo == "" {
|
||||
// 回退到默认仓库
|
||||
repo = p.baseDomain
|
||||
zap.L().Debug("fail back to default repo", zap.String("repo", repo))
|
||||
}
|
||||
returnMeta, err := p.ReturnMeta(owner, repo, branch, pathS[1:])
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
} else if err == nil {
|
||||
return returnMeta, nil
|
||||
}
|
||||
// 回退到默认页面
|
||||
return p.ReturnMeta(owner, repo, domain, pathS)
|
||||
}
|
||||
|
||||
func (p *PageDomain) ReturnMeta(owner string, repo string, branch string, path []string) (*PageDomainContent, error) {
|
||||
rel := &PageDomainContent{}
|
||||
if meta, err := p.GetMeta(owner, repo, branch); err == nil {
|
||||
rel.PageMetaContent = meta
|
||||
rel.Owner = owner
|
||||
rel.Repo = repo
|
||||
rel.Path = strings.Join(path, "/")
|
||||
if strings.HasSuffix(rel.Path, "/") || rel.Path == "" {
|
||||
rel.Path = rel.Path + "index.html"
|
||||
}
|
||||
if meta.Domain != "" {
|
||||
err = p.alias.Bind(meta.Domain, rel.Owner, rel.Repo, branch)
|
||||
if err != nil {
|
||||
zap.L().Warn("别名绑定失败", zap.Error(err))
|
||||
}
|
||||
}
|
||||
return rel, nil
|
||||
}
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
@@ -6,14 +6,19 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||
)
|
||||
|
||||
var regexpHostname = regexp.MustCompile(`^(?:([a-z0-9-]+|\*)\.)?([a-z0-9-]{1,61})\.([a-z0-9]{2,7})$`)
|
||||
|
||||
type ServerMeta struct {
|
||||
Backend
|
||||
|
||||
@@ -111,7 +116,12 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
||||
if cname, err := s.ReadString(owner, repo, rel.CommitID, "CNAME"); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
} else {
|
||||
rel.Domain = strings.TrimSpace(cname)
|
||||
cname = strings.TrimSpace(cname)
|
||||
if regexpHostname.MatchString(cname) {
|
||||
rel.Domain = cname
|
||||
} else {
|
||||
zap.L().Debug("指定的 CNAME 不合法", zap.String("cname", cname))
|
||||
}
|
||||
}
|
||||
if find, _ := s.FileExists(owner, repo, rel.CommitID, ".history"); find {
|
||||
rel.HistoryRouteMode = true
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type PageDomain struct {
|
||||
*ServerMeta
|
||||
|
||||
baseDomain string
|
||||
defaultBranch string
|
||||
}
|
||||
|
||||
func NewPageDomain(meta *ServerMeta, baseDomain, defaultBranch string) *PageDomain {
|
||||
return &PageDomain{
|
||||
baseDomain: baseDomain,
|
||||
defaultBranch: defaultBranch,
|
||||
ServerMeta: meta,
|
||||
}
|
||||
}
|
||||
|
||||
type PageDomainContent struct {
|
||||
*PageMetaContent
|
||||
|
||||
Owner string
|
||||
Repo string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (m *PageDomainContent) CacheKey() string {
|
||||
return fmt.Sprintf("%s/%s/%s/%s", m.Owner, m.Repo, m.CommitID, m.Path)
|
||||
}
|
||||
|
||||
func (p *PageDomain) ParseDomainMeta(domain, path, branch string) (*PageDomainContent, error) {
|
||||
if branch == "" {
|
||||
branch = p.defaultBranch
|
||||
}
|
||||
domain = regexp.MustCompile(`:\d+$`).ReplaceAllString(domain, "")
|
||||
|
||||
rel := &PageDomainContent{}
|
||||
if !strings.HasSuffix(domain, "."+p.baseDomain) {
|
||||
zap.L().Warn("Page Domain does not end with ."+p.baseDomain, zap.String("domain", domain))
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
rel.Owner = strings.TrimSuffix(domain, "."+p.baseDomain)
|
||||
pathS := strings.Split(strings.TrimPrefix(path, "/"), "/")
|
||||
rel.Repo = pathS[0]
|
||||
defaultRepo := rel.Owner + "." + p.baseDomain
|
||||
if rel.Repo == "" {
|
||||
// 回退到默认仓库
|
||||
rel.Repo = defaultRepo
|
||||
zap.L().Debug("fail back to default repo", zap.String("repo", defaultRepo))
|
||||
}
|
||||
|
||||
meta, err := p.GetMeta(rel.Owner, rel.Repo, branch)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
if err == nil {
|
||||
rel.Path = strings.Join(pathS[1:], "/")
|
||||
if strings.HasSuffix(rel.Path, "/") || rel.Path == "" {
|
||||
rel.Path = rel.Path + "index.html"
|
||||
}
|
||||
rel.PageMetaContent = meta
|
||||
return rel, nil
|
||||
}
|
||||
if defaultRepo == rel.Repo {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
if meta, err := p.GetMeta(rel.Owner, defaultRepo, branch); err == nil {
|
||||
rel.PageMetaContent = meta
|
||||
rel.Repo = defaultRepo
|
||||
rel.Path = strings.Join(pathS[1:], "/")
|
||||
if strings.HasSuffix(rel.Path, "/") || rel.Path == "" {
|
||||
rel.Path = rel.Path + "index.html"
|
||||
}
|
||||
return rel, nil
|
||||
}
|
||||
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
Reference in New Issue
Block a user