Skip to content

Commit 0e8b5b5

Browse files
committed
Disable file caching and closes #16
Uses HTTP headers described in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Preventing_caching
1 parent ad18749 commit 0e8b5b5

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

cmd/server/server.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,22 @@ import (
3131
"net/http"
3232
"os"
3333
"path/filepath"
34+
"log"
3435
)
3536

37+
func withCacheDisabled(next http.Handler) http.HandlerFunc {
38+
return func(w http.ResponseWriter, r *http.Request) {
39+
w.Header().Set("Cache-control", "no-cache, no-store, must-revalidate")
40+
next.ServeHTTP(w, r)
41+
}
42+
}
43+
3644
// serverCmd represents the server command
3745
var serverCmd = &cobra.Command{
38-
Use: "server",
39-
Short: "Starts a simple web server to serve static content",
40-
Long: "Starts a simple web server to serve static content",
46+
Aliases: []string{"serve"},
47+
Use: "server",
48+
Short: "Starts a simple web server to serve static content",
49+
Long: "Starts a simple web server to serve static content",
4150
Args: func(cmd *cobra.Command, args []string) error {
4251
if len(args) != 1 {
4352
return errors.New("You have to specify a folder with web content")
@@ -47,8 +56,7 @@ var serverCmd = &cobra.Command{
4756
},
4857
Run: func(cmd *cobra.Command, args []string) {
4958
fs := http.FileServer(http.Dir(args[0]))
50-
http.Handle("/", fs)
51-
59+
http.Handle("/", withCacheDisabled(fs));
5260
srv := config.Cfg.Server
5361
addr := fmt.Sprintf("%s:%s", srv.Address, srv.Port)
5462

@@ -63,7 +71,7 @@ var serverCmd = &cobra.Command{
6371
}
6472

6573
fmt.Printf("Serving %s on %s\n", path, addr)
66-
http.ListenAndServe(addr, nil)
74+
log.Fatal(http.ListenAndServe(addr, nil))
6775
},
6876
Example: ``,
6977
}

0 commit comments

Comments
 (0)