mirror of
https://github.com/netfun2000/rttys_zhaojh329.git
synced 2026-02-27 09:53:24 +08:00
api: Optimizing Static Resource Handling
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -208,33 +209,41 @@ func apiStart(br *broker) {
|
||||
}
|
||||
})
|
||||
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
fs, _ := fs.Sub(staticFs, "ui/dist")
|
||||
root := http.FS(fs)
|
||||
fh := http.FileServer(root)
|
||||
|
||||
path := c.Request.URL.Path
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
upath := path.Clean(c.Request.URL.Path)
|
||||
|
||||
if path != "/" {
|
||||
f, err := fs.Open(path[1:])
|
||||
if strings.HasSuffix(upath, ".js") || strings.HasSuffix(upath, ".css") {
|
||||
if strings.Contains(c.Request.Header.Get("Accept-Encoding"), "gzip") {
|
||||
f, err := root.Open(upath + ".gz")
|
||||
if err == nil {
|
||||
f.Close()
|
||||
|
||||
c.Request.URL.Path += ".gz"
|
||||
|
||||
if strings.HasSuffix(upath, ".js") {
|
||||
c.Writer.Header().Set("Content-Type", "application/javascript")
|
||||
} else if strings.HasSuffix(upath, ".css") {
|
||||
c.Writer.Header().Set("Content-Type", "text/css")
|
||||
}
|
||||
|
||||
c.Writer.Header().Set("Content-Encoding", "gzip")
|
||||
}
|
||||
}
|
||||
} else if upath != "/" {
|
||||
f, err := root.Open(upath)
|
||||
if err != nil {
|
||||
c.Request.URL.Path = "/"
|
||||
r.HandleContext(c)
|
||||
return
|
||||
}
|
||||
|
||||
if strings.Contains(c.Request.Header.Get("Accept-Encoding"), "gzip") {
|
||||
if strings.HasSuffix(path, "css") || strings.HasSuffix(path, "js") {
|
||||
magic := make([]byte, 2)
|
||||
f.Read(magic)
|
||||
if magic[0] == 0x1f && magic[1] == 0x8b {
|
||||
c.Writer.Header().Set("Content-Encoding", "gzip")
|
||||
}
|
||||
}
|
||||
defer f.Close()
|
||||
}
|
||||
|
||||
f.Close()
|
||||
}
|
||||
|
||||
http.FileServer(http.FS(fs)).ServeHTTP(c.Writer, c.Request)
|
||||
fh.ServeHTTP(c.Writer, c.Request)
|
||||
})
|
||||
|
||||
go func() {
|
||||
|
||||
+1
-2
@@ -12,8 +12,7 @@ export default defineConfig({
|
||||
compression({
|
||||
algorithms: ['gzip'],
|
||||
deleteOriginalAssets: true,
|
||||
threshold: 10240,
|
||||
filename: '[path][base]'
|
||||
threshold: 10240
|
||||
}),
|
||||
vueI18n()
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user