fix(embed_fs): enable embed fs for prod

This commit is contained in:
hexxa 2022-04-17 12:33:11 +08:00 committed by Hexxa
parent 56505dacdd
commit d7263cc3c8
3 changed files with 19 additions and 8 deletions

View file

@ -82,7 +82,10 @@ func (h *MultiUsersSvc) APIAccessControl() gin.HandlerFunc {
return return
} else if accessPath == "/" || // TODO: temporarily allow accessing static resources } else if accessPath == "/" || // TODO: temporarily allow accessing static resources
accessPath == "/favicon.ico" || accessPath == "/favicon.ico" ||
strings.HasPrefix(accessPath, "/static") { strings.HasPrefix(accessPath, "/css") ||
strings.HasPrefix(accessPath, "/font") ||
strings.HasPrefix(accessPath, "/img") ||
strings.HasPrefix(accessPath, "/js") {
c.Next() c.Next()
return return
} }

View file

@ -111,7 +111,7 @@ func DefaultConfigStruct() *Config {
ReadTimeout: 2000, ReadTimeout: 2000,
WriteTimeout: 1000 * 3600 * 24, // 1 day WriteTimeout: 1000 * 3600 * 24, // 1 day
MaxHeaderBytes: 512, MaxHeaderBytes: 512,
PublicPath: "public", PublicPath: "static/public",
}, },
Workers: &WorkerPoolCfg{ Workers: &WorkerPoolCfg{
QueueSize: 1024, QueueSize: 1024,

View file

@ -39,6 +39,7 @@ import (
"github.com/ihexxa/quickshare/src/kvstore" "github.com/ihexxa/quickshare/src/kvstore"
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd" "github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
"github.com/ihexxa/quickshare/src/worker/localworker" "github.com/ihexxa/quickshare/src/worker/localworker"
qsstatic "github.com/ihexxa/quickshare/static"
) )
type Server struct { type Server struct {
@ -251,16 +252,23 @@ func initHandlers(router *gin.Engine, cfg gocfg.ICfg, deps *depidx.Deps) (*gin.E
return nil, fmt.Errorf("new setting service error: %w", err) return nil, fmt.Errorf("new setting service error: %w", err)
} }
// middleware
router.Use(userHdrs.AuthN())
router.Use(userHdrs.APIAccessControl())
publicPath, ok := cfg.String("Server.PublicPath") publicPath, ok := cfg.String("Server.PublicPath")
if !ok || publicPath == "" { if !ok || publicPath == "" {
return nil, errors.New("publicPath not found or empty") return nil, errors.New("publicPath not found or empty")
} }
if cfg.BoolOr("Server.Debug", false) {
// middleware
router.Use(userHdrs.AuthN())
router.Use(userHdrs.APIAccessControl())
// tmp static server
router.Use(static.Serve("/", static.LocalFile(publicPath, false))) router.Use(static.Serve("/", static.LocalFile(publicPath, false)))
} else {
esFS, err := qsstatic.NewEmbedStaticFS()
if err != nil {
return nil, err
}
router.Use(static.Serve("/", esFS))
}
// handler // handler
v1 := router.Group("/v1") v1 := router.Group("/v1")