api/cache
This commit is contained in:
parent
8e2b0ada32
commit
46a72e1aef
|
@ -12,40 +12,58 @@ import (
|
||||||
func (h APIHandler) GetCache(w http.ResponseWriter, r *http.Request) {
|
func (h APIHandler) GetCache(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
cacheid := vars["cacheid"]
|
cacheid := vars["cacheid"]
|
||||||
|
// Use a channel to synchronize the goroutines
|
||||||
d, err := h.cache.Get(cacheid)
|
ch := make(chan []byte)
|
||||||
if err != nil {
|
// Fetch data from cache asynchronously
|
||||||
fmt.Println(err)
|
go func() {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
d, err := h.cache.Get(cacheid)
|
||||||
return
|
if err != nil {
|
||||||
}
|
fmt.Println(err)
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
result := d
|
ch <- nil
|
||||||
|
return
|
||||||
if data, ok := d.([]any); ok {
|
|
||||||
if limitsmin, ok := r.URL.Query()["limits.min"]; ok {
|
|
||||||
min, _ := strconv.Atoi(limitsmin[0])
|
|
||||||
if limitsmax, ok := r.URL.Query()["limits.max"]; ok {
|
|
||||||
max, _ := strconv.Atoi(limitsmax[0])
|
|
||||||
if max > len(data) {
|
|
||||||
result = data[min:]
|
|
||||||
} else {
|
|
||||||
result = data[min:max]
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result = data[min:]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
var data []any
|
||||||
|
if val, ok := d.([]any); ok {
|
||||||
|
data = val
|
||||||
|
} else {
|
||||||
|
data = []any{d}
|
||||||
|
}
|
||||||
|
j := toJSON(data, w, r)
|
||||||
|
ch <- j // Signal that the data has been fetched successfully
|
||||||
|
close(ch)
|
||||||
|
}()
|
||||||
|
// wait for the JSON marshaling goroutine to finish
|
||||||
|
j := <-ch
|
||||||
|
if j == nil {
|
||||||
|
return // Stop processing if an error occurred
|
||||||
}
|
}
|
||||||
|
// Send the JSON response to the client
|
||||||
j, err := json.Marshal(result)
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(j)
|
w.Write(j)
|
||||||
|
|
||||||
|
<-ch
|
||||||
|
}
|
||||||
|
func toJSON(data []any, w http.ResponseWriter, r *http.Request) []byte {
|
||||||
|
result := data
|
||||||
|
if limitsmin, ok := r.URL.Query()["limits.min"]; ok {
|
||||||
|
min, _ := strconv.Atoi(limitsmin[0])
|
||||||
|
if limitsmax, ok := r.URL.Query()["limits.max"]; ok {
|
||||||
|
max, _ := strconv.Atoi(limitsmax[0])
|
||||||
|
if max > len(data) {
|
||||||
|
result = data[min:]
|
||||||
|
} else {
|
||||||
|
result = data[min:max]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
result = data[min:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
j, err := json.Marshal(result)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return j
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit d6de19d8e238fec2f03957abdfa7b49a463d4a3c
|
|
@ -74,6 +74,7 @@ func NewEtcdHandler(cfg *viper.Viper) (*EtcdHandler, error) {
|
||||||
Password: password,
|
Password: password,
|
||||||
DialTimeout: 5 * time.Second,
|
DialTimeout: 5 * time.Second,
|
||||||
})
|
})
|
||||||
|
fmt.Println(endpoints,prefix,username,password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue