Skip to content

Commit

Permalink
Set "no cache" on all served pages.
Browse files Browse the repository at this point in the history
Fixes #164.
  • Loading branch information
cyoung committed Dec 27, 2015
1 parent 7f392a7 commit 4f56a4c
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions main/managementinterface.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package main

import (
"encoding/json"
"encoding/hex"
"strings"
"encoding/json"
"fmt"
"golang.org/x/net/websocket"
"io"
"log"
"net/http"
"time"
"strings"
"syscall"
"time"
)

type SettingMessage struct {
Expand Down Expand Up @@ -120,6 +120,7 @@ func handleSituationWS(conn *websocket.Conn) {
// AJAX call - /getStatus. Responds with current global status
// a webservice call for the same data available on the websocket but when only a single update is needed
func handleStatusRequest(w http.ResponseWriter, r *http.Request) {
setNoCache(w)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
statusJSON, _ := json.Marshal(&globalStatus)
Expand All @@ -128,6 +129,7 @@ func handleStatusRequest(w http.ResponseWriter, r *http.Request) {

// AJAX call - /getSituation. Responds with current situation (lat/lon/gdspeed/track/pitch/roll/heading/etc.)
func handleSituationRequest(w http.ResponseWriter, r *http.Request) {
setNoCache(w)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
situationJSON, _ := json.Marshal(&mySituation)
Expand All @@ -136,6 +138,7 @@ func handleSituationRequest(w http.ResponseWriter, r *http.Request) {

// AJAX call - /getTowers. Responds with all ADS-B ground towers that have sent messages that we were able to parse, along with its stats.
func handleTowersRequest(w http.ResponseWriter, r *http.Request) {
setNoCache(w)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
towersJSON, _ := json.Marshal(&ADSBTowers)
Expand All @@ -146,6 +149,7 @@ func handleTowersRequest(w http.ResponseWriter, r *http.Request) {

// AJAX call - /getSettings. Responds with all stratux.conf data.
func handleSettingsGetRequest(w http.ResponseWriter, r *http.Request) {
setNoCache(w)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
settingsJSON, _ := json.Marshal(&globalSettings)
Expand All @@ -155,6 +159,7 @@ func handleSettingsGetRequest(w http.ResponseWriter, r *http.Request) {
// AJAX call - /setSettings. receives via POST command, any/all stratux.conf data.
func handleSettingsSetRequest(w http.ResponseWriter, r *http.Request) {
// define header in support of cross-domain AJAX
setNoCache(w)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Method", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
Expand Down Expand Up @@ -219,7 +224,7 @@ func handleSettingsSetRequest(w http.ResponseWriter, r *http.Request) {
log.Printf("handleSettingsSetRequest:json: unrecognized key:%s\n", key)
}
}
saveSettings()
saveSettings()
}
}

Expand All @@ -231,21 +236,31 @@ func handleSettingsSetRequest(w http.ResponseWriter, r *http.Request) {

func handleShutdownRequest(w http.ResponseWriter, r *http.Request) {
syscall.Sync()
syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF)
syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF)
}


func handleRebootRequest(w http.ResponseWriter, r *http.Request) {
syscall.Sync()
syscall.Sync()
syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
}

func setNoCache(w http.ResponseWriter) {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
}

func defaultServer(w http.ResponseWriter, r *http.Request) {
setNoCache(w)

http.FileServer(http.Dir("/var/www")).ServeHTTP(w, r)
}

func managementInterface() {
weatherUpdate = NewUIBroadcaster()
trafficUpdate = NewUIBroadcaster()

http.Handle("/", http.FileServer(http.Dir("/var/www")))
http.HandleFunc("/", defaultServer)
http.Handle("/logs/", http.StripPrefix("/logs/", http.FileServer(http.Dir("/var/log"))))
http.HandleFunc("/status",
func(w http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit 4f56a4c

Please sign in to comment.