优化部分代码,补充未实现功能
This commit is contained in:
@@ -147,22 +147,24 @@ func (c *CacheBackendBlobReader) Open(owner, repo, commit, path string) (io.Read
|
||||
return open.Body, nil
|
||||
}
|
||||
// 没法计算大小,跳过
|
||||
lengthStr := open.Header.Get("Content-Length")
|
||||
if lengthStr == "" {
|
||||
return open.Body, nil
|
||||
}
|
||||
length, err := strconv.Atoi(lengthStr)
|
||||
if err != nil || length > c.maxSize {
|
||||
// 超过最大大小,跳过
|
||||
length, err := strconv.Atoi(open.Header.Get("Content-Length"))
|
||||
if err != nil {
|
||||
return open.Body, err
|
||||
}
|
||||
if length > c.maxSize {
|
||||
// 超过最大大小,跳过
|
||||
return &utils.SizeReadCloser{
|
||||
ReadCloser: open.Body,
|
||||
Size: length,
|
||||
}, err
|
||||
}
|
||||
defer open.Body.Close()
|
||||
allBytes, err := io.ReadAll(open.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.cache.Put(key, bytes.NewBuffer(allBytes)); err != nil {
|
||||
zap.S().Warn("缓存归档失败", zap.Error(err))
|
||||
zap.L().Warn("缓存归档失败", zap.Error(err), zap.Int("Size", len(allBytes)), zap.Int("MaxSize", c.maxSize))
|
||||
}
|
||||
return &utils.CacheContent{
|
||||
ReadSeekCloser: utils.NopCloser{
|
||||
|
||||
@@ -6,12 +6,15 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"code.d7z.net/d7z-project/gitea-pages/pkg/core"
|
||||
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||
"github.com/pbnjay/memory"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type ServerOptions struct {
|
||||
@@ -24,6 +27,8 @@ type ServerOptions struct {
|
||||
MaxCacheSize int
|
||||
|
||||
HttpClient *http.Client
|
||||
|
||||
DefaultErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
|
||||
}
|
||||
|
||||
func DefaultOptions(domain string) ServerOptions {
|
||||
@@ -35,12 +40,19 @@ func DefaultOptions(domain string) ServerOptions {
|
||||
Cache: utils.NewCacheMemory(1024*1024*10, int(memory.FreeMemory()/3*2)),
|
||||
MaxCacheSize: 1024 * 1024 * 10,
|
||||
HttpClient: http.DefaultClient,
|
||||
DefaultErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
http.Error(w, "page not found.", http.StatusNotFound)
|
||||
} else if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
meta *core.PageDomain
|
||||
options *ServerOptions
|
||||
meta *core.PageDomain
|
||||
reader *core.CacheBackendBlobReader
|
||||
}
|
||||
|
||||
@@ -50,18 +62,24 @@ func NewPageServer(backend core.Backend, options ServerOptions) *Server {
|
||||
pageMeta := core.NewPageDomain(svcMeta, options.Config, options.Domain, options.DefaultBranch)
|
||||
reader := core.NewCacheBackendBlobReader(options.HttpClient, backend, options.Cache, options.MaxCacheSize)
|
||||
return &Server{
|
||||
meta: pageMeta,
|
||||
options: &options,
|
||||
meta: pageMeta,
|
||||
reader: reader,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
zap.L().Error("panic!", zap.Any("error", e))
|
||||
if err, ok := e.(error); ok {
|
||||
s.options.DefaultErrorHandler(writer, request, err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
err := s.Serve(writer, request)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
http.Error(writer, "page not found.", http.StatusNotFound)
|
||||
} else {
|
||||
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
||||
if err != nil {
|
||||
s.options.DefaultErrorHandler(writer, request, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,17 +88,41 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
zap.L().Debug("获取请求", zap.Any("meta", meta))
|
||||
// todo(feat) : 支持 http range
|
||||
result, err := s.reader.Open(meta.Owner, meta.Repo, meta.CommitID, meta.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
if meta.HistoryRouteMode && errors.Is(err, os.ErrNotExist) {
|
||||
result, err = s.reader.Open(meta.Owner, meta.Repo, meta.CommitID, "index.html")
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err != nil && meta.CustomNotFound && errors.Is(err, os.ErrNotExist) {
|
||||
// 存在 404 页面的情况
|
||||
result, err = s.reader.Open(meta.Owner, meta.Repo, meta.CommitID, "404.html")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writer.Header().Set("Content-Type", mime.TypeByExtension(".html"))
|
||||
writer.WriteHeader(http.StatusNotFound)
|
||||
_, _ = io.Copy(writer, result)
|
||||
_ = result.Close()
|
||||
return nil
|
||||
}
|
||||
fileName := filepath.Base(meta.Path)
|
||||
if reader, ok := result.(*utils.CacheContent); ok {
|
||||
writer.Header().Add("X-Cache", "HIT")
|
||||
writer.Header().Add("Cache-Control", "public, max-age=86400")
|
||||
http.ServeContent(writer, request, fileName, reader.LastModified, reader)
|
||||
_ = reader.Close()
|
||||
} else {
|
||||
if reader, ok := result.(*utils.SizeReadCloser); ok {
|
||||
writer.Header().Add("Content-Length", strconv.Itoa(reader.Size))
|
||||
}
|
||||
// todo(bug) : 直连模式下告知数据长度
|
||||
writer.Header().Add("X-Cache", "MISS")
|
||||
writer.Header().Add("Cache-Control", "public, max-age=86400")
|
||||
writer.Header().Set("Content-Type", mime.TypeByExtension(meta.Path))
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
_, _ = io.Copy(writer, result)
|
||||
|
||||
8
pkg/utils/reader.go
Normal file
8
pkg/utils/reader.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package utils
|
||||
|
||||
import "io"
|
||||
|
||||
type SizeReadCloser struct {
|
||||
io.ReadCloser
|
||||
Size int
|
||||
}
|
||||
Reference in New Issue
Block a user