-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.go
67 lines (58 loc) · 1.68 KB
/
static.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
const (
maxAgeMinute = 60
ttlVendor = 60 * maxAgeMinute
ttlStatic = 5 * maxAgeMinute
ttlPostDir = 1 * maxAgeMinute
)
var (
adminOnly = os.Getenv("ADMIN_ONLY") == "1"
_postDir = os.Getenv("POST_DIR")
postDirAbs = ""
)
func init() {
if abs, err := filepath.Abs(_postDir); err != nil {
log.Fatalf("env.POST_DIR=%q is invalid\n", _postDir)
} else {
postDirAbs = abs
}
}
func staticRoute(r *gin.Engine) {
// Note: How to cache static files? #1222
// https://github.com/gin-gonic/gin/issues/1222
r.Use(func(c *gin.Context) {
ttl := ttlStatic
path := c.Request.URL.Path
if strings.HasPrefix(path, "/vendor/") && path != "/vendor/blog.js" && path != "/vendor/blog.css" {
ttl = ttlVendor
} else if strings.HasPrefix(path, "/p/") {
ttl = ttlPostDir
}
// Apply the Cache-Control header to the static files
c.Header("Cache-Control", fmt.Sprintf("public, max-age=%d", ttl))
})
r.Use(protectPrivate)
// Note: Inability to use '/' for static files #75
// https://github.com/gin-gonic/gin/issues/75
// r.Static("/", "./static")
r.StaticFile("/edit", "./static/edit.html")
r.StaticFile("/admin", "./static/admin.html")
r.StaticFile("/login", "./static/login.html")
// r.Use(static.Serve("/", static.LocalFile("./static", false)))
r.Use(static.Serve("/", static.LocalFile("./silent_ext", true)))
r.Use(static.Serve("/vendor", static.LocalFile("./silent/blog/vendor", false)))
r.StaticFile("/favicon.ico", "./silent/blog/favicon.ico")
if adminOnly {
r.Use(checkAuth)
}
r.Use(static.Serve("/p", static.LocalFile(postDirAbs, false)))
}