mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 10:25:52 +02:00
refactor: update static file serving middleware to improve functionality
This commit is contained in:
parent
ae16ca4139
commit
98a4708a8c
2 changed files with 24 additions and 7 deletions
|
@ -9,21 +9,39 @@ import (
|
|||
|
||||
"github.com/0xJacky/Nginx-UI/app"
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/uozi-tech/cosy/logger"
|
||||
)
|
||||
|
||||
func MustFs(dir string) (serverFileSystem static.ServeFileSystem) {
|
||||
|
||||
func mustFs(dir string) (serverFileSystem static.ServeFileSystem) {
|
||||
sub, err := fs.Sub(app.DistFS, path.Join("dist", dir))
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
serverFileSystem = ServerFileSystemType{
|
||||
http.FS(sub),
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ServeStatic() []gin.HandlerFunc {
|
||||
const urlPrefix = "/"
|
||||
fs := mustFs(urlPrefix)
|
||||
fileserver := http.FileServer(fs)
|
||||
if urlPrefix != "" {
|
||||
fileserver = http.StripPrefix(urlPrefix, fileserver)
|
||||
}
|
||||
return []gin.HandlerFunc{
|
||||
func(c *gin.Context) {
|
||||
if fs.Exists(urlPrefix, c.Request.URL.Path) {
|
||||
c.Next()
|
||||
}
|
||||
},
|
||||
IPWhiteList(),
|
||||
func(c *gin.Context) {
|
||||
fileserver.ServeHTTP(c.Writer, c.Request)
|
||||
c.Abort()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue