jlelse
/
kis3
Archived
1
Fork 0

Fix cache control

This commit is contained in:
Jan-Lukas Else 2019-04-08 22:05:21 +02:00
parent 25189b2cbc
commit edf6cc32f2
1 changed files with 18 additions and 6 deletions

24
main.go
View File

@ -66,6 +66,7 @@ func startListening() {
} }
func trackView(w http.ResponseWriter, r *http.Request) { func trackView(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=0")
url := r.Header.Get("Referer") // URL of requesting source url := r.Header.Get("Referer") // URL of requesting source
ref := r.URL.Query().Get("ref") ref := r.URL.Query().Get("ref")
ua := r.Header.Get("User-Agent") ua := r.Header.Get("User-Agent")
@ -79,10 +80,20 @@ func sendHelloResponse(w http.ResponseWriter, _ *http.Request) {
_, _ = fmt.Fprint(w, "Hello from KISSS") _, _ = fmt.Fprint(w, "Hello from KISSS")
} }
func serveTrackingScript(w http.ResponseWriter, _ *http.Request) { func serveTrackingScript(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/javascript") w.Header().Set("Content-Type", "application/javascript")
trackingScriptBytes, _ := app.staticBox.Find("kis3.js") w.Header().Set("Cache-Control", "max-age=432000") // 5 days
_, _ = w.Write(trackingScriptBytes) filename := "kis3.js"
file, err := app.staticBox.Open(filename)
if err != nil {
return
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return
}
http.ServeContent(w, r, filename, stat.ModTime(), file)
} }
func requestStats(w http.ResponseWriter, r *http.Request) { func requestStats(w http.ResponseWriter, r *http.Request) {
@ -123,6 +134,7 @@ func requestStats(w http.ResponseWriter, r *http.Request) {
fmt.Println("Database request failed:", e) fmt.Println("Database request failed:", e)
w.WriteHeader(500) w.WriteHeader(500)
} else if result != nil { } else if result != nil {
w.Header().Set("Cache-Control", "max-age=0")
switch queries.Get("format") { switch queries.Get("format") {
case "json": case "json":
sendJsonResponse(result, w) sendJsonResponse(result, w)
@ -138,14 +150,14 @@ func requestStats(w http.ResponseWriter, r *http.Request) {
} }
func sendPlainResponse(result []*RequestResultRow, w http.ResponseWriter) { func sendPlainResponse(result []*RequestResultRow, w http.ResponseWriter) {
w.Header().Add("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
for _, row := range result { for _, row := range result {
_, _ = fmt.Fprintln(w, (*row).First+": "+strconv.Itoa((*row).Second)) _, _ = fmt.Fprintln(w, (*row).First+": "+strconv.Itoa((*row).Second))
} }
} }
func sendJsonResponse(result []*RequestResultRow, w http.ResponseWriter) { func sendJsonResponse(result []*RequestResultRow, w http.ResponseWriter) {
w.Header().Add("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
jsonBytes, _ := json.Marshal(result) jsonBytes, _ := json.Marshal(result)
_, _ = fmt.Fprintln(w, string(jsonBytes)) _, _ = fmt.Fprintln(w, string(jsonBytes))
} }