* /control/version.json: add "recheck_now" parameter

This commit is contained in:
Simon Zolin 2019-06-25 15:55:09 +03:00
parent f1e6a30931
commit 5d60bb05ab
2 changed files with 29 additions and 11 deletions

View File

@ -257,7 +257,11 @@ Server can only auto-update if the current version is equal or higher than `self
Request: Request:
GET /control/version.json POST /control/version.json
{
"recheck_now": true | false // if false, server will check for a new version data only once in several hours
}
Response: Response:

View File

@ -51,6 +51,10 @@ func getVersionResp(data []byte) []byte {
return d return d
} }
type getVersionJSONRequest struct {
RecheckNow bool `json:"recheck_now"`
}
// Get the latest available version from the Internet // Get the latest available version from the Internet
func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) { func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
log.Tracef("%s %v", r.Method, r.URL) log.Tracef("%s %v", r.Method, r.URL)
@ -60,19 +64,29 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
return return
} }
now := time.Now() req := getVersionJSONRequest{}
controlLock.Lock() err := json.NewDecoder(r.Body).Decode(&req)
cached := now.Sub(versionCheckLastTime) <= versionCheckPeriod && len(versionCheckJSON) != 0 if err != nil {
data := versionCheckJSON httpError(w, http.StatusBadRequest, "JSON parse: %s", err)
controlLock.Unlock()
if cached {
// return cached copy
w.Header().Set("Content-Type", "application/json")
w.Write(getVersionResp(data))
return return
} }
now := time.Now()
if !req.RecheckNow {
controlLock.Lock()
cached := now.Sub(versionCheckLastTime) <= versionCheckPeriod && len(versionCheckJSON) != 0
data := versionCheckJSON
controlLock.Unlock()
if cached {
log.Tracef("Returning cached data")
w.Header().Set("Content-Type", "application/json")
w.Write(getVersionResp(data))
return
}
}
log.Tracef("Downloading data from %s", versionCheckURL)
resp, err := client.Get(versionCheckURL) resp, err := client.Get(versionCheckURL)
if err != nil { if err != nil {
httpError(w, http.StatusBadGateway, "Couldn't get version check json from %s: %T %s\n", versionCheckURL, err, err) httpError(w, http.StatusBadGateway, "Couldn't get version check json from %s: %T %s\n", versionCheckURL, err, err)