重构项目

This commit is contained in:
ExplodingDragon
2025-11-15 01:09:25 +08:00
parent d67dbf88ae
commit 3492dead8d
34 changed files with 715 additions and 444 deletions

View File

@@ -19,5 +19,5 @@ type Backend interface {
// Branches return branch + commit id
Branches(ctx context.Context, owner, repo string) (map[string]*BranchInfo, error)
// Open return file or error (error)
Open(ctx context.Context, client *http.Client, owner, repo, commit, path string, headers http.Header) (*http.Response, error)
Open(ctx context.Context, owner, repo, commit, path string, headers http.Header) (*http.Response, error)
}

View File

@@ -7,36 +7,31 @@ import (
"github.com/pkg/errors"
"go.uber.org/zap"
"gopkg.d7z.net/middleware/kv"
)
type PageDomain struct {
*ServerMeta
alias *DomainAlias
pageDB kv.KV
baseDomain string
defaultBranch string
}
func NewPageDomain(meta *ServerMeta, alias *DomainAlias, pageDB kv.KV, baseDomain, defaultBranch string) *PageDomain {
func NewPageDomain(meta *ServerMeta, alias *DomainAlias, baseDomain, defaultBranch string) *PageDomain {
return &PageDomain{
baseDomain: baseDomain,
defaultBranch: defaultBranch,
ServerMeta: meta,
alias: alias,
pageDB: pageDB,
}
}
type PageContent struct {
*PageMetaContent
*PageVFS
OrgDB kv.KV
RepoDB kv.KV
Owner string
Repo string
Path string
Owner string
Repo string
Path string
}
func (p *PageDomain) ParseDomainMeta(ctx context.Context, domain, path, branch string) (*PageContent, error) {
@@ -87,9 +82,6 @@ func (p *PageDomain) returnMeta(ctx context.Context, owner, repo, branch string,
result.PageMetaContent = meta
result.Owner = owner
result.Repo = repo
result.PageVFS = NewPageVFS(p.client, p.Backend, owner, repo, result.CommitID)
result.OrgDB = p.pageDB.Child("org").Child(owner)
result.RepoDB = p.pageDB.Child("repo").Child(owner).Child(repo)
result.Path = strings.Join(path, "/")
if err = p.alias.Bind(ctx, meta.Alias, result.Owner, result.Repo, branch); err != nil {

View File

@@ -9,8 +9,19 @@ import (
"strings"
"go.uber.org/zap"
"gopkg.d7z.net/middleware/kv"
"gopkg.d7z.net/middleware/tools"
)
type FilterContext struct {
context.Context
*PageContent
*PageVFS
Cache *tools.TTLCache
OrgDB kv.CursorPagedKV
RepoDB kv.CursorPagedKV
}
type FilterParams map[string]any
func (f FilterParams) String() string {
@@ -33,30 +44,28 @@ type Filter struct {
}
func NextCallWrapper(call FilterCall, parentCall NextCall, stack Filter) NextCall {
return func(ctx context.Context, writer http.ResponseWriter, request *http.Request, metadata *PageContent) error {
return func(ctx FilterContext, writer http.ResponseWriter, request *http.Request) error {
zap.L().Debug(fmt.Sprintf("call filter(%s) before", stack.Type), zap.Any("filter", stack))
err := call(ctx, writer, request, metadata, parentCall)
err := call(ctx, writer, request, parentCall)
zap.L().Debug(fmt.Sprintf("call filter(%s) after", stack.Type), zap.Any("filter", stack), zap.Error(err))
return err
}
}
type NextCall func(
ctx context.Context,
ctx FilterContext,
writer http.ResponseWriter,
request *http.Request,
metadata *PageContent,
) error
var NotFountNextCall = func(ctx context.Context, writer http.ResponseWriter, request *http.Request, metadata *PageContent) error {
var NotFountNextCall = func(ctx FilterContext, writer http.ResponseWriter, request *http.Request) error {
return os.ErrNotExist
}
type FilterCall func(
ctx context.Context,
ctx FilterContext,
writer http.ResponseWriter,
request *http.Request,
metadata *PageContent,
next NextCall,
) error

View File

@@ -38,8 +38,7 @@ type PageMetaContent struct {
IsPage bool `json:"is_page"` // 是否为 Page
ErrorMsg string `json:"error"` // 错误消息 (作为 500 错误日志暴露至前端)
Alias []string `json:"alias"` // alias
Alias []string `json:"alias"` // alias
Filters []Filter `json:"filters"` // 路由消息
}
@@ -127,7 +126,7 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
}
rel := NewEmptyPageMetaContent()
vfs := NewPageVFS(s.client, s.Backend, owner, repo, info.ID)
vfs := NewPageVFS(s.Backend, owner, repo, info.ID)
rel.CommitID = info.ID
rel.LastModified = info.LastModified

View File

@@ -9,7 +9,6 @@ import (
type PageVFS struct {
backend Backend
client *http.Client
org string
repo string
@@ -18,14 +17,12 @@ type PageVFS struct {
// todo: 限制最大文件加载大小
func NewPageVFS(
client *http.Client,
backend Backend,
org string,
repo string,
commitID string,
) *PageVFS {
return &PageVFS{
client: client,
backend: backend,
org: org,
repo: repo,
@@ -34,7 +31,7 @@ func NewPageVFS(
}
func (p *PageVFS) NativeOpen(ctx context.Context, path string, headers http.Header) (*http.Response, error) {
return p.backend.Open(ctx, p.client, p.org, p.repo, p.commitID, path, headers)
return p.backend.Open(ctx, p.org, p.repo, p.commitID, path, headers)
}
func (p *PageVFS) Exists(ctx context.Context, path string) (bool, error) {