Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

Commit 7905899

Browse files
committed
Disable cache by default, allow disabling CORS handler
1 parent ab15bfe commit 7905899

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

cmd/root.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,21 @@ var rootCmd = &cobra.Command{
6767
log.Printf("Listening for HTTPS connections on %s", addr)
6868
log.Printf("Serving from directory %s", dir)
6969

70+
handler := logger(http.FileServer(http.Dir(dir)))
71+
7072
// permit some CORS stuff
71-
corsHandler := cors.New(cors.Options{
72-
AllowCredentials: true,
73-
AllowOriginFunc: func(origin string) bool {return true},
74-
})
73+
if disableCORS, _ := cmd.Flags().GetBool("no-cors"); !disableCORS {
74+
handler = cors.New(cors.Options{
75+
AllowCredentials: true,
76+
AllowOriginFunc: func(origin string) bool {return true},
77+
}).Handler(handler)
78+
}
79+
80+
// Disable browser cache?
81+
if cache, _ := cmd.Flags().GetBool("cache"); !cache {
82+
handler = nocache(handler)
83+
}
7584

76-
handler := corsHandler.Handler(logger(http.FileServer(http.Dir(dir))))
7785
log.Fatal(http.ListenAndServeTLS(addr, certFile, keyFile, handler))
7886
},
7987
}
@@ -87,12 +95,21 @@ func Execute() {
8795

8896
func init(){
8997
rootCmd.Flags().StringP("listen", "l", ":8443", "Listen address")
90-
//rootCmd.Flags().Bool("version", false, "Print version and exit")
98+
rootCmd.Flags().Bool("no-cors", false, "Disable CORS handling")
99+
rootCmd.Flags().Bool("cache", false, "Enable client side caching")
91100
}
92101

93102
func logger(handler http.Handler) http.Handler {
94103
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
95104
log.Printf( "%s \"%s %s %s\"\n", r.RemoteAddr, r.Method, r.URL, r.Proto)
96105
handler.ServeHTTP(w, r)
97106
})
98-
}
107+
}
108+
109+
func nocache(handler http.Handler) http.Handler {
110+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
111+
w.Header().Add("Cache-Control", "no-store")
112+
handler.ServeHTTP(w, r)
113+
})
114+
}
115+

0 commit comments

Comments
 (0)