Skip to content

Commit c4f7bb7

Browse files
committed
generate last-modified string at settings init
1 parent 77dfd64 commit c4f7bb7

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

.air.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tmp_dir = "tmp"
77

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

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ jobs:
151151
- name: Build
152152
run: |
153153
mkdir -p dist
154-
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
154+
go build -ldflags "$LD_FLAGS -X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'" -o dist/nginx-ui -v main.go
155155
156156
- name: Archive backend artifacts
157157
uses: actions/upload-artifact@v2

server/router/middleware.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"net/http"
1313
"path"
1414
"strings"
15-
"time"
1615
)
1716

1817
func recovery() gin.HandlerFunc {
@@ -84,13 +83,10 @@ func cacheJs() gin.HandlerFunc {
8483
return func(c *gin.Context) {
8584
if strings.Contains(c.Request.URL.String(), "js") {
8685
c.Header("Cache-Control", "max-age: 1296000")
87-
t, _ := time.Parse("2006.01.02.150405", settings.BuildTime)
88-
t = t.Add(-8 * time.Hour)
89-
lastModified := strings.ReplaceAll(t.Format(time.RFC1123), "UTC", "GMT")
90-
if c.Request.Header.Get("If-Modified-Since") == lastModified {
86+
if c.Request.Header.Get("If-Modified-Since") == settings.LastModified {
9187
c.AbortWithStatus(http.StatusNotModified)
9288
}
93-
c.Header("Last-Modified", lastModified)
89+
c.Header("Last-Modified", settings.LastModified)
9490
}
9591
}
9692
}

server/settings/settings.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package settings
22

33
import (
4+
"github.com/spf13/cast"
45
"gopkg.in/ini.v1"
56
"log"
7+
"strings"
8+
"time"
69
)
710

811
var Conf *ini.File
912

1013
var (
11-
BuildTime string
14+
buildTime string
15+
LastModified string
1216
)
1317

1418
type Server struct {
@@ -36,6 +40,11 @@ var sections = map[string]interface{}{
3640
"server": ServerSettings,
3741
}
3842

43+
func init() {
44+
t := time.Unix(cast.ToInt64(buildTime), 0)
45+
LastModified = strings.ReplaceAll(t.Format(time.RFC1123), "UTC", "GMT")
46+
}
47+
3948
func Init(confPath string) {
4049
ConfPath = confPath
4150
Setup()

0 commit comments

Comments
 (0)