Created
April 30, 2017 00:41
-
-
Save PaluMacil/9d9a443f80f57bd0bdef503dadfc7e2f to your computer and use it in GitHub Desktop.
http/net Go no cache headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
) | |
type healthModel struct { | |
Message string `json:"message"` | |
} | |
func main() { | |
http.Handle("/", http.FileServer(http.Dir("./www"))) | |
http.HandleFunc("/api/health", healthHandler) | |
http.ListenAndServe(":3000", nil) | |
} | |
func healthHandler(w http.ResponseWriter, r *http.Request) { | |
message := healthModel{"Hello, world!"} | |
output, err := json.Marshal(message) | |
if err != nil { | |
fmt.Println("Json marshalling failed.") | |
} | |
w.Header().Set("Content-Type", "application/json") | |
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1, and no-cache for IE6 | |
w.Header().Set("Pragma", "no-cache") // HTTP 1.0 released 1997, for IE6, some robots, wget, maybe some old mobile | |
w.Header().Set("Expires", "0") // HTTP 1.0 proxies. | |
fmt.Fprint(w, string(output)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment