mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-03 07:12:27 +00:00
cmd/ursrv: Provide cached locations.json
This commit is contained in:
parent
83f6da8dca
commit
b28899ac07
@ -754,6 +754,7 @@ func main() {
|
|||||||
http.HandleFunc("/movement.json", withDB(db, movementHandler))
|
http.HandleFunc("/movement.json", withDB(db, movementHandler))
|
||||||
http.HandleFunc("/performance.json", withDB(db, performanceHandler))
|
http.HandleFunc("/performance.json", withDB(db, performanceHandler))
|
||||||
http.HandleFunc("/blockstats.json", withDB(db, blockStatsHandler))
|
http.HandleFunc("/blockstats.json", withDB(db, blockStatsHandler))
|
||||||
|
http.HandleFunc("/locations.json", withDB(db, locationsHandler))
|
||||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||||
|
|
||||||
go cacheRefresher(db)
|
go cacheRefresher(db)
|
||||||
@ -765,9 +766,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cacheData []byte
|
cachedIndex []byte
|
||||||
cacheTime time.Time
|
cachedLocations []byte
|
||||||
cacheMut sync.Mutex
|
cacheTime time.Time
|
||||||
|
cacheMut sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxCacheTime = 15 * time.Minute
|
const maxCacheTime = 15 * time.Minute
|
||||||
@ -791,8 +793,15 @@ func refreshCacheLocked(db *sql.DB) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cacheData = buf.Bytes()
|
cachedIndex = buf.Bytes()
|
||||||
cacheTime = time.Now()
|
cacheTime = time.Now()
|
||||||
|
|
||||||
|
locs := rep["locations"].(map[location]int)
|
||||||
|
wlocs := make([]weightedLocation, 0, len(locs))
|
||||||
|
for loc, w := range locs {
|
||||||
|
wlocs = append(wlocs, weightedLocation{loc, w})
|
||||||
|
}
|
||||||
|
cachedLocations, _ = json.Marshal(wlocs)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -810,13 +819,29 @@ func rootHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
w.Write(cacheData)
|
w.Write(cachedIndex)
|
||||||
} else {
|
} else {
|
||||||
http.Error(w, "Not found", 404)
|
http.Error(w, "Not found", 404)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func locationsHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
|
||||||
|
cacheMut.Lock()
|
||||||
|
defer cacheMut.Unlock()
|
||||||
|
|
||||||
|
if time.Since(cacheTime) > maxCacheTime {
|
||||||
|
if err := refreshCacheLocked(db); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, "Template Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
w.Write(cachedLocations)
|
||||||
|
}
|
||||||
|
|
||||||
func newDataHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
|
func newDataHandler(db *sql.DB, w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
@ -1016,8 +1041,13 @@ func inc(storage map[string]int, key string, i interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type location struct {
|
type location struct {
|
||||||
Latitude float64
|
Latitude float64 `json:"lat"`
|
||||||
Longitude float64
|
Longitude float64 `json:"lon"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type weightedLocation struct {
|
||||||
|
location
|
||||||
|
Weight int `json:"weight"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getReport(db *sql.DB) map[string]interface{} {
|
func getReport(db *sql.DB) map[string]interface{} {
|
||||||
|
Loading…
Reference in New Issue
Block a user