add nocache flag

This commit is contained in:
Your Name
2021-10-21 15:45:49 -04:00
parent 73a3d635dd
commit 0fd317fd56
3 changed files with 24 additions and 1 deletions

13
main.go
View File

@@ -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 != "" {