From abba011a61a46c2586f9e5bc8de00a6577454196 Mon Sep 17 00:00:00 2001 From: dragon Date: Thu, 18 Sep 2025 16:41:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=9D=99=E6=80=81=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E8=B7=AF=E5=BE=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.go | 12 ++++++++++++ config.yaml | 4 +++- main.go | 1 + pkg/server.go | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 70d91c5..471bdb5 100644 --- a/config.go +++ b/config.go @@ -30,6 +30,8 @@ type Config struct { Render ConfigRender `yaml:"render"` // 渲染配置 Proxy ConfigProxy `yaml:"proxy"` // 反向代理配置 + StaticDir string `yaml:"static"` // 静态资源提供路径 + pageErrNotFound, pageErrUnknown *template.Template } @@ -44,6 +46,15 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) { return nil, errors.Wrap(err, "parse cache size") } + if c.StaticDir != "" { + stat, err := os.Stat(c.StaticDir) + if err != nil { + return nil, errors.Wrap(err, "static dir not exists") + } + if !stat.IsDir() { + return nil, errors.New("static dir is not a directory") + } + } cacheMaxSize, err = units.ParseBase2Bytes(c.Cache.MaxSize) if err != nil { return nil, errors.Wrap(err, "parse cache max size") @@ -83,6 +94,7 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) { EnableRender: c.Render.Enable, EnableProxy: c.Proxy.Enable, DefaultErrorHandler: c.ErrorHandler, + StaticDir: c.StaticDir, Cache: utils.NewCacheMemory(int(cacheMaxSize), int(cacheMaxSize)), } cfg, err := utils.NewAutoConfig(c.Cache.Storage) diff --git a/config.yaml b/config.yaml index 8badd7f..1615b6f 100644 --- a/config.yaml +++ b/config.yaml @@ -29,4 +29,6 @@ render: enable: false # 反向代理配置 proxy: - enable: false \ No newline at end of file + enable: false +# 静态资源路径,可供任意网站使用 /.well-known/page-server/ 路径拉取文件 +static: /path/to/dir/ \ No newline at end of file diff --git a/main.go b/main.go index 432c3d6..93b2280 100644 --- a/main.go +++ b/main.go @@ -45,6 +45,7 @@ func main() { defer giteaServer.Close() ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT) defer stop() + svc := http.Server{Addr: config.Bind, Handler: giteaServer} go func() { select { diff --git a/pkg/server.go b/pkg/server.go index 1473330..5e8aad5 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -46,6 +46,8 @@ type ServerOptions struct { EnableRender bool EnableProxy bool + StaticDir string + DefaultErrorHandler func(w http.ResponseWriter, r *http.Request, err error) } @@ -61,6 +63,7 @@ func DefaultOptions(domain string) ServerOptions { MetaTTL: time.Minute, EnableRender: true, EnableProxy: true, + StaticDir: "", DefaultErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) { if errors.Is(err, os.ErrNotExist) { http.Error(w, "page not found.", http.StatusNotFound) @@ -76,24 +79,36 @@ type Server struct { meta *core.PageDomain reader *core.CacheBackendBlobReader backend core.Backend + fs http.Handler } +var staticPrefix = "/.well-known/page-server/" + func NewPageServer(backend core.Backend, options ServerOptions) *Server { backend = core.NewCacheBackend(backend, options.KVConfig, options.MetaTTL) svcMeta := core.NewServerMeta(options.HttpClient, backend, options.KVConfig, options.Domain, options.MetaTTL) pageMeta := core.NewPageDomain(svcMeta, options.KVConfig, options.Domain, options.DefaultBranch) reader := core.NewCacheBackendBlobReader(options.HttpClient, backend, options.Cache, options.MaxCacheSize) + var fs http.Handler + if options.StaticDir != "" { + fs = http.StripPrefix(staticPrefix, http.FileServer(http.Dir(options.StaticDir))) + } return &Server{ backend: backend, options: &options, meta: pageMeta, reader: reader, + fs: fs, } } func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { sessionId, _ := uuid.NewRandom() request.Header.Set("Session-ID", sessionId.String()) + if s.fs != nil && strings.HasPrefix(request.URL.Path, staticPrefix) { + s.fs.ServeHTTP(writer, request) + return + } defer func() { if e := recover(); e != nil { zap.L().Error("panic!", zap.Any("error", e), zap.Any("id", sessionId))