新增静态资源路径支持
This commit is contained in:
12
config.go
12
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)
|
||||
|
||||
@@ -29,4 +29,6 @@ render:
|
||||
enable: false
|
||||
# 反向代理配置
|
||||
proxy:
|
||||
enable: false
|
||||
enable: false
|
||||
# 静态资源路径,可供任意网站使用 /.well-known/page-server/ 路径拉取文件
|
||||
static: /path/to/dir/
|
||||
1
main.go
1
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 {
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user