浏览代码

generate last-modified string at settings init

0xJacky 3 年之前
父节点
当前提交
c4f7bb7604
共有 4 个文件被更改,包括 14 次插入9 次删除
  1. 1 1
      .air.toml
  2. 1 1
      .github/workflows/build.yml
  3. 2 6
      server/router/middleware.go
  4. 10 1
      server/settings/settings.go

+ 1 - 1
.air.toml

@@ -7,7 +7,7 @@ tmp_dir = "tmp"
 
 [build]
 # Just plain old shell command. You could use `make` as well.
-cmd = "go build -ldflags=\"-X 'github.com/0xJacky/Nginx-UI/server/settings.BuildTime=$(date +%Y.%m.%d.%H%M%S)'\" -o ./tmp/main ."
+cmd = "go build -ldflags=\"-X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'\" -o ./tmp/main ."
 # Binary file yields from `cmd`.
 bin = "tmp/main"
 # Customize binary.

+ 1 - 1
.github/workflows/build.yml

@@ -151,7 +151,7 @@ jobs:
             - name: Build
               run: |
                   mkdir -p dist
-                  go build -ldflags "$LD_FLAGS -X 'github.com/0xJacky/Nginx-UI/server/settings.BuildTime=$(date +%Y.%m.%d.%H%M%S)'" -o dist/nginx-ui -v main.go
+                  go build -ldflags "$LD_FLAGS -X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'" -o dist/nginx-ui -v main.go
 
             - name: Archive backend artifacts
               uses: actions/upload-artifact@v2

+ 2 - 6
server/router/middleware.go

@@ -12,7 +12,6 @@ import (
 	"net/http"
 	"path"
 	"strings"
-	"time"
 )
 
 func recovery() gin.HandlerFunc {
@@ -84,13 +83,10 @@ func cacheJs() gin.HandlerFunc {
 	return func(c *gin.Context) {
 		if strings.Contains(c.Request.URL.String(), "js") {
 			c.Header("Cache-Control", "max-age: 1296000")
-			t, _ := time.Parse("2006.01.02.150405", settings.BuildTime)
-			t = t.Add(-8 * time.Hour)
-			lastModified := strings.ReplaceAll(t.Format(time.RFC1123), "UTC", "GMT")
-			if c.Request.Header.Get("If-Modified-Since") == lastModified {
+			if c.Request.Header.Get("If-Modified-Since") == settings.LastModified {
 				c.AbortWithStatus(http.StatusNotModified)
 			}
-			c.Header("Last-Modified", lastModified)
+			c.Header("Last-Modified", settings.LastModified)
 		}
 	}
 }

+ 10 - 1
server/settings/settings.go

@@ -1,14 +1,18 @@
 package settings
 
 import (
+	"github.com/spf13/cast"
 	"gopkg.in/ini.v1"
 	"log"
+	"strings"
+	"time"
 )
 
 var Conf *ini.File
 
 var (
-	BuildTime string
+	buildTime    string
+	LastModified string
 )
 
 type Server struct {
@@ -36,6 +40,11 @@ var sections = map[string]interface{}{
 	"server": ServerSettings,
 }
 
+func init() {
+	t := time.Unix(cast.ToInt64(buildTime), 0)
+	LastModified = strings.ReplaceAll(t.Format(time.RFC1123), "UTC", "GMT")
+}
+
 func Init(confPath string) {
 	ConfPath = confPath
 	Setup()