新增静态资源路径支持
This commit is contained in:
12
config.go
12
config.go
@@ -30,6 +30,8 @@ type Config struct {
|
|||||||
Render ConfigRender `yaml:"render"` // 渲染配置
|
Render ConfigRender `yaml:"render"` // 渲染配置
|
||||||
Proxy ConfigProxy `yaml:"proxy"` // 反向代理配置
|
Proxy ConfigProxy `yaml:"proxy"` // 反向代理配置
|
||||||
|
|
||||||
|
StaticDir string `yaml:"static"` // 静态资源提供路径
|
||||||
|
|
||||||
pageErrNotFound, pageErrUnknown *template.Template
|
pageErrNotFound, pageErrUnknown *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +46,15 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) {
|
|||||||
return nil, errors.Wrap(err, "parse cache size")
|
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)
|
cacheMaxSize, err = units.ParseBase2Bytes(c.Cache.MaxSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "parse cache max size")
|
return nil, errors.Wrap(err, "parse cache max size")
|
||||||
@@ -83,6 +94,7 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) {
|
|||||||
EnableRender: c.Render.Enable,
|
EnableRender: c.Render.Enable,
|
||||||
EnableProxy: c.Proxy.Enable,
|
EnableProxy: c.Proxy.Enable,
|
||||||
DefaultErrorHandler: c.ErrorHandler,
|
DefaultErrorHandler: c.ErrorHandler,
|
||||||
|
StaticDir: c.StaticDir,
|
||||||
Cache: utils.NewCacheMemory(int(cacheMaxSize), int(cacheMaxSize)),
|
Cache: utils.NewCacheMemory(int(cacheMaxSize), int(cacheMaxSize)),
|
||||||
}
|
}
|
||||||
cfg, err := utils.NewAutoConfig(c.Cache.Storage)
|
cfg, err := utils.NewAutoConfig(c.Cache.Storage)
|
||||||
|
|||||||
@@ -30,3 +30,5 @@ render:
|
|||||||
# 反向代理配置
|
# 反向代理配置
|
||||||
proxy:
|
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()
|
defer giteaServer.Close()
|
||||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
|
||||||
defer stop()
|
defer stop()
|
||||||
|
|
||||||
svc := http.Server{Addr: config.Bind, Handler: giteaServer}
|
svc := http.Server{Addr: config.Bind, Handler: giteaServer}
|
||||||
go func() {
|
go func() {
|
||||||
select {
|
select {
|
||||||
|
|||||||
@@ -46,6 +46,8 @@ type ServerOptions struct {
|
|||||||
EnableRender bool
|
EnableRender bool
|
||||||
EnableProxy bool
|
EnableProxy bool
|
||||||
|
|
||||||
|
StaticDir string
|
||||||
|
|
||||||
DefaultErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
|
DefaultErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,6 +63,7 @@ func DefaultOptions(domain string) ServerOptions {
|
|||||||
MetaTTL: time.Minute,
|
MetaTTL: time.Minute,
|
||||||
EnableRender: true,
|
EnableRender: true,
|
||||||
EnableProxy: true,
|
EnableProxy: true,
|
||||||
|
StaticDir: "",
|
||||||
DefaultErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
DefaultErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
http.Error(w, "page not found.", http.StatusNotFound)
|
http.Error(w, "page not found.", http.StatusNotFound)
|
||||||
@@ -76,24 +79,36 @@ type Server struct {
|
|||||||
meta *core.PageDomain
|
meta *core.PageDomain
|
||||||
reader *core.CacheBackendBlobReader
|
reader *core.CacheBackendBlobReader
|
||||||
backend core.Backend
|
backend core.Backend
|
||||||
|
fs http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var staticPrefix = "/.well-known/page-server/"
|
||||||
|
|
||||||
func NewPageServer(backend core.Backend, options ServerOptions) *Server {
|
func NewPageServer(backend core.Backend, options ServerOptions) *Server {
|
||||||
backend = core.NewCacheBackend(backend, options.KVConfig, options.MetaTTL)
|
backend = core.NewCacheBackend(backend, options.KVConfig, options.MetaTTL)
|
||||||
svcMeta := core.NewServerMeta(options.HttpClient, backend, options.KVConfig, options.Domain, options.MetaTTL)
|
svcMeta := core.NewServerMeta(options.HttpClient, backend, options.KVConfig, options.Domain, options.MetaTTL)
|
||||||
pageMeta := core.NewPageDomain(svcMeta, options.KVConfig, options.Domain, options.DefaultBranch)
|
pageMeta := core.NewPageDomain(svcMeta, options.KVConfig, options.Domain, options.DefaultBranch)
|
||||||
reader := core.NewCacheBackendBlobReader(options.HttpClient, backend, options.Cache, options.MaxCacheSize)
|
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{
|
return &Server{
|
||||||
backend: backend,
|
backend: backend,
|
||||||
options: &options,
|
options: &options,
|
||||||
meta: pageMeta,
|
meta: pageMeta,
|
||||||
reader: reader,
|
reader: reader,
|
||||||
|
fs: fs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
sessionId, _ := uuid.NewRandom()
|
sessionId, _ := uuid.NewRandom()
|
||||||
request.Header.Set("Session-ID", sessionId.String())
|
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() {
|
defer func() {
|
||||||
if e := recover(); e != nil {
|
if e := recover(); e != nil {
|
||||||
zap.L().Error("panic!", zap.Any("error", e), zap.Any("id", sessionId))
|
zap.L().Error("panic!", zap.Any("error", e), zap.Any("id", sessionId))
|
||||||
|
|||||||
Reference in New Issue
Block a user