Skip to content

Commit abba011

Browse files
新增静态资源路径支持
1 parent 7961084 commit abba011

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type Config struct {
3030
Render ConfigRender `yaml:"render"` // 渲染配置
3131
Proxy ConfigProxy `yaml:"proxy"` // 反向代理配置
3232

33+
StaticDir string `yaml:"static"` // 静态资源提供路径
34+
3335
pageErrNotFound, pageErrUnknown *template.Template
3436
}
3537

@@ -44,6 +46,15 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) {
4446
return nil, errors.Wrap(err, "parse cache size")
4547
}
4648

49+
if c.StaticDir != "" {
50+
stat, err := os.Stat(c.StaticDir)
51+
if err != nil {
52+
return nil, errors.Wrap(err, "static dir not exists")
53+
}
54+
if !stat.IsDir() {
55+
return nil, errors.New("static dir is not a directory")
56+
}
57+
}
4758
cacheMaxSize, err = units.ParseBase2Bytes(c.Cache.MaxSize)
4859
if err != nil {
4960
return nil, errors.Wrap(err, "parse cache max size")
@@ -83,6 +94,7 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) {
8394
EnableRender: c.Render.Enable,
8495
EnableProxy: c.Proxy.Enable,
8596
DefaultErrorHandler: c.ErrorHandler,
97+
StaticDir: c.StaticDir,
8698
Cache: utils.NewCacheMemory(int(cacheMaxSize), int(cacheMaxSize)),
8799
}
88100
cfg, err := utils.NewAutoConfig(c.Cache.Storage)

config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ render:
2929
enable: false
3030
# 反向代理配置
3131
proxy:
32-
enable: false
32+
enable: false
33+
# 静态资源路径,可供任意网站使用 /.well-known/page-server/ 路径拉取文件
34+
static: /path/to/dir/

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func main() {
4545
defer giteaServer.Close()
4646
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
4747
defer stop()
48+
4849
svc := http.Server{Addr: config.Bind, Handler: giteaServer}
4950
go func() {
5051
select {

pkg/server.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type ServerOptions struct {
4646
EnableRender bool
4747
EnableProxy bool
4848

49+
StaticDir string
50+
4951
DefaultErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
5052
}
5153

@@ -61,6 +63,7 @@ func DefaultOptions(domain string) ServerOptions {
6163
MetaTTL: time.Minute,
6264
EnableRender: true,
6365
EnableProxy: true,
66+
StaticDir: "",
6467
DefaultErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
6568
if errors.Is(err, os.ErrNotExist) {
6669
http.Error(w, "page not found.", http.StatusNotFound)
@@ -76,24 +79,36 @@ type Server struct {
7679
meta *core.PageDomain
7780
reader *core.CacheBackendBlobReader
7881
backend core.Backend
82+
fs http.Handler
7983
}
8084

85+
var staticPrefix = "/.well-known/page-server/"
86+
8187
func NewPageServer(backend core.Backend, options ServerOptions) *Server {
8288
backend = core.NewCacheBackend(backend, options.KVConfig, options.MetaTTL)
8389
svcMeta := core.NewServerMeta(options.HttpClient, backend, options.KVConfig, options.Domain, options.MetaTTL)
8490
pageMeta := core.NewPageDomain(svcMeta, options.KVConfig, options.Domain, options.DefaultBranch)
8591
reader := core.NewCacheBackendBlobReader(options.HttpClient, backend, options.Cache, options.MaxCacheSize)
92+
var fs http.Handler
93+
if options.StaticDir != "" {
94+
fs = http.StripPrefix(staticPrefix, http.FileServer(http.Dir(options.StaticDir)))
95+
}
8696
return &Server{
8797
backend: backend,
8898
options: &options,
8999
meta: pageMeta,
90100
reader: reader,
101+
fs: fs,
91102
}
92103
}
93104

94105
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
95106
sessionId, _ := uuid.NewRandom()
96107
request.Header.Set("Session-ID", sessionId.String())
108+
if s.fs != nil && strings.HasPrefix(request.URL.Path, staticPrefix) {
109+
s.fs.ServeHTTP(writer, request)
110+
return
111+
}
97112
defer func() {
98113
if e := recover(); e != nil {
99114
zap.L().Error("panic!", zap.Any("error", e), zap.Any("id", sessionId))

0 commit comments

Comments
 (0)