mirror of
https://github.com/tgdrive/teldrive.git
synced 2025-10-06 13:26:08 +08:00
add cache headers
This commit is contained in:
parent
d09f1e346f
commit
bdaf6610ec
3 changed files with 22 additions and 1 deletions
9
main.go
9
main.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
|
@ -52,6 +53,14 @@ func main() {
|
|||
|
||||
router.Use(gin.ErrorLogger())
|
||||
|
||||
router.Use(func(c *gin.Context) {
|
||||
path := c.Request.URL.Path
|
||||
if path == "" || path == "/" {
|
||||
c.Redirect(http.StatusMovedPermanently, "/my-drive")
|
||||
}
|
||||
c.Next()
|
||||
})
|
||||
|
||||
routes.AddRoutes(router)
|
||||
|
||||
ui.AddRoutes(router)
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit f3f2f5b371e407c983e756ad8955f0a8b345ca28
|
||||
Subproject commit ab2936c3f24945221f6b800c6e0743b7501ac512
|
12
ui/ui.go
12
ui/ui.go
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/contrib/static"
|
||||
|
@ -17,6 +18,17 @@ var staticFS embed.FS
|
|||
func AddRoutes(router gin.IRouter) {
|
||||
embeddedBuildFolder := newStaticFileSystem()
|
||||
fallbackFileSystem := newFallbackFileSystem(embeddedBuildFolder)
|
||||
|
||||
router.Use(func(c *gin.Context) {
|
||||
isStatic, _ := path.Match("/assets/*", c.Request.URL.Path)
|
||||
isImg, _ := path.Match("/img/*", c.Request.URL.Path)
|
||||
if isStatic || isImg {
|
||||
c.Writer.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
||||
} else {
|
||||
c.Writer.Header().Set("Cache-Control", "public, max-age=0, s-maxage=0, must-revalidate")
|
||||
}
|
||||
c.Next()
|
||||
})
|
||||
router.Use(static.Serve("/", embeddedBuildFolder))
|
||||
router.Use(static.Serve("/", fallbackFileSystem))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue