From 0fd317fd56169538995db75f4aa6384c7f62248b Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 21 Oct 2021 15:45:49 -0400 Subject: [PATCH] add nocache flag --- go.mod | 8 ++++++++ go.sum | 4 ++++ main.go | 13 ++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c439130 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module git.teamworkapps.com/shortcut/httphere + +go 1.17 + +require ( + git.teamworkapps.com/shortcut/enhancedfileserver v0.0.0-20181107163431-49cae39a0eb7 + github.com/gorilla/mux v1.8.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a9c17a7 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +git.teamworkapps.com/shortcut/enhancedfileserver v0.0.0-20181107163431-49cae39a0eb7 h1:wl0faTtewN9Y0nTZpRY2mcaIhVkaR1l8k920U0rDIk0= +git.teamworkapps.com/shortcut/enhancedfileserver v0.0.0-20181107163431-49cae39a0eb7/go.mod h1:4t5y+BFcMRTe6GkAX72YXcjrKbGkOVM7CkJMRHNZYiQ= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= diff --git a/main.go b/main.go index cc37b4d..8b1dddd 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ type config struct { username string password string noindex bool + nocache bool log bool } @@ -28,6 +29,7 @@ func init() { flag.StringVar(&c.password, "password", "", "Require this password to access interface") flag.BoolVar(&c.noindex, "noindex", false, "Disable directory indexing") flag.BoolVar(&c.log, "log", false, "Enable simple request log") + flag.BoolVar(&c.nocache, "nocache", false, "Set http headers to prevent caching of files") flag.Parse() } @@ -42,7 +44,9 @@ func main() { if c.log { r.Use(queryLog) } - + if c.nocache { + r.Use(noCache) + } if c.noindex { r.PathPrefix("/").Handler(http.StripPrefix("/", enhancedfileserver.FileServerNoIndex(http.Dir("./")))) } else { @@ -54,6 +58,13 @@ func main() { log.Fatal(http.ListenAndServe(a, r)) } +func noCache(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Cache-control", "no-cache, no-store") + next.ServeHTTP(w,r) + }) +} + func basicAuth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if c.username != "" && c.password != "" {