Skip to content

Commit 57fe15a

Browse files
committed
Add cache option
1 parent 4e2b423 commit 57fe15a

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,18 @@ func (s *Items) Join(sep string) (data string) {
8181

8282
type StaticHandler struct {
8383
Root string
84+
Cache bool
8485
fileServer http.Handler
8586
}
8687

8788
func (h *StaticHandler) Handle(c *gin.Context) {
89+
if !h.Cache {
90+
c.Writer.Header().Add("Cache-Control",
91+
"no-cache, no-store, must-revalidate")
92+
c.Writer.Header().Add("Pragma", "no-cache")
93+
c.Writer.Header().Add("Expires", "0")
94+
}
95+
8896
path := filepath.Join(h.Root, filepath.FromSlash(
8997
filepath.Clean("/"+c.Param("filepath"))))
9098

@@ -199,18 +207,21 @@ func main() {
199207
pathPtr := flag.String("path", path, "Path to serve")
200208
hostPtr := flag.String("host", "", "Server host")
201209
portPtr := flag.Int("port", 8000, "Server port number")
210+
cachePtr := flag.Bool("cache", false, "Enable cache")
202211
flag.Parse()
203212
path = *pathPtr
204213
host := *hostPtr
205214
port := *portPtr
215+
cache := *cachePtr
206216

207217
path, err = filepath.Abs(path)
208218
if err != nil {
209219
panic(err)
210220
}
211221

212222
static := &StaticHandler{
213-
Root: path,
223+
Root: path,
224+
Cache: cache,
214225
}
215226

216227
gin.SetMode(gin.ReleaseMode)

0 commit comments

Comments
 (0)