-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_server.go
37 lines (32 loc) · 990 Bytes
/
simple_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"io"
"net/http"
"github.com/gorilla/mux"
)
func HelloHandler(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
if vars["name"] != "" {
io.WriteString(writer, "hello "+vars["name"]+"!")
} else {
io.WriteString(writer, "hello!")
}
}
func NoCacheDecorator(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
h.ServeHTTP(w, r)
})
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/hello", HelloHandler)
router.HandleFunc("/hello/{name}", HelloHandler)
staticHandler := http.FileServer(http.Dir("."))
staticHandler = http.StripPrefix("/static/", staticHandler)
staticHandler = NoCacheDecorator(staticHandler)
router.PathPrefix("/static/").Handler(staticHandler)
http.ListenAndServe("localhost:1234", router)
}