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

8
go.mod Normal file
View File

@ -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
)

4
go.sum Normal file
View File

@ -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=

13
main.go
View File

@ -16,6 +16,7 @@ type config struct {
username string username string
password string password string
noindex bool noindex bool
nocache bool
log bool log bool
} }
@ -28,6 +29,7 @@ func init() {
flag.StringVar(&c.password, "password", "", "Require this password to access interface") flag.StringVar(&c.password, "password", "", "Require this password to access interface")
flag.BoolVar(&c.noindex, "noindex", false, "Disable directory indexing") flag.BoolVar(&c.noindex, "noindex", false, "Disable directory indexing")
flag.BoolVar(&c.log, "log", false, "Enable simple request log") 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() flag.Parse()
} }
@ -42,7 +44,9 @@ func main() {
if c.log { if c.log {
r.Use(queryLog) r.Use(queryLog)
} }
if c.nocache {
r.Use(noCache)
}
if c.noindex { if c.noindex {
r.PathPrefix("/").Handler(http.StripPrefix("/", enhancedfileserver.FileServerNoIndex(http.Dir("./")))) r.PathPrefix("/").Handler(http.StripPrefix("/", enhancedfileserver.FileServerNoIndex(http.Dir("./"))))
} else { } else {
@ -54,6 +58,13 @@ func main() {
log.Fatal(http.ListenAndServe(a, r)) 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 { func basicAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if c.username != "" && c.password != "" { if c.username != "" && c.password != "" {