From 46a72e1aefe7c437d356564afe0334f10d7390aa Mon Sep 17 00:00:00 2001 From: root Date: Thu, 20 Apr 2023 13:14:25 +0300 Subject: [PATCH] api/cache --- Dockerfile | 0 README.md | 0 config.go | 0 go.mod | 0 go.sum | 0 handlers/api/api.go | 0 handlers/api/cache.go | 78 +++++++++++++-------- handlers/api/export.go | 0 handlers/api/geo.go | 0 handlers/api/oidc.go | 0 handlers/application/administration.go | 0 handlers/application/agenda.go | 0 handlers/application/application.go | 0 handlers/application/beneficiaries.go | 0 handlers/application/dashboard.go | 0 handlers/application/directory.go | 0 handlers/application/group.go | 0 handlers/application/group_module.go | 0 handlers/application/journeys.go | 0 handlers/application/members.go | 0 handlers/application/support.go | 0 handlers/application/vehicles-management.go | 0 handlers/application/vehicles.go | 0 handlers/auth/auth.go | 0 handlers/auth/disconnect.go | 0 handlers/auth/groups.go | 0 handlers/auth/lost_password.go | 0 handlers/auth/onboarding.go | 0 handlers/exports/agenda.go | 0 handlers/exports/exports.go | 0 handlers/exports/fleets.go | 0 main.go | 0 renderer/administration.go | 0 renderer/agenda.go | 0 renderer/auth.go | 0 renderer/beneficiaries.go | 0 renderer/dashboard.go | 0 renderer/directory.go | 0 renderer/func-maps.go | 0 renderer/group.go | 0 renderer/group_module.go | 0 renderer/journeys.go | 0 renderer/layout.go | 0 renderer/mailer.go | 0 renderer/members.go | 0 renderer/renderer.go | 0 renderer/support.go | 0 renderer/vehicle-management.go | 0 renderer/vehicles.go | 0 services/agenda.go | 0 services/fleets.go | 0 services/groupsmanagement.go | 0 services/mobilityaccounts.go | 0 services/services.go | 0 themes | 1 + utils/form-validators/form-validators.go | 0 utils/form-validators/phone-numbers.go | 0 utils/icons/svg-icons.go | 0 utils/identification/groups.go | 0 utils/identification/oidc.go | 0 utils/profile-pictures/profile-pictures.go | 0 utils/sorting/beneficiaries.go | 0 utils/sorting/events.go | 0 utils/sorting/fleets.go | 0 utils/sorting/groups.go | 0 utils/sorting/sorting.go | 0 utils/storage/cache.go | 0 utils/storage/etcd.go | 1 + utils/storage/files.go | 0 utils/storage/kv.go | 0 utils/storage/minio.go | 0 utils/storage/sessions.go | 0 72 files changed, 50 insertions(+), 30 deletions(-) mode change 100644 => 100755 Dockerfile mode change 100644 => 100755 README.md mode change 100644 => 100755 config.go mode change 100644 => 100755 go.mod mode change 100644 => 100755 go.sum mode change 100644 => 100755 handlers/api/api.go mode change 100644 => 100755 handlers/api/cache.go mode change 100644 => 100755 handlers/api/export.go mode change 100644 => 100755 handlers/api/geo.go mode change 100644 => 100755 handlers/api/oidc.go mode change 100644 => 100755 handlers/application/administration.go mode change 100644 => 100755 handlers/application/agenda.go mode change 100644 => 100755 handlers/application/application.go mode change 100644 => 100755 handlers/application/beneficiaries.go mode change 100644 => 100755 handlers/application/dashboard.go mode change 100644 => 100755 handlers/application/directory.go mode change 100644 => 100755 handlers/application/group.go mode change 100644 => 100755 handlers/application/group_module.go mode change 100644 => 100755 handlers/application/journeys.go mode change 100644 => 100755 handlers/application/members.go mode change 100644 => 100755 handlers/application/support.go mode change 100644 => 100755 handlers/application/vehicles-management.go mode change 100644 => 100755 handlers/application/vehicles.go mode change 100644 => 100755 handlers/auth/auth.go mode change 100644 => 100755 handlers/auth/disconnect.go mode change 100644 => 100755 handlers/auth/groups.go mode change 100644 => 100755 handlers/auth/lost_password.go mode change 100644 => 100755 handlers/auth/onboarding.go mode change 100644 => 100755 handlers/exports/agenda.go mode change 100644 => 100755 handlers/exports/exports.go mode change 100644 => 100755 handlers/exports/fleets.go mode change 100644 => 100755 main.go mode change 100644 => 100755 renderer/administration.go mode change 100644 => 100755 renderer/agenda.go mode change 100644 => 100755 renderer/auth.go mode change 100644 => 100755 renderer/beneficiaries.go mode change 100644 => 100755 renderer/dashboard.go mode change 100644 => 100755 renderer/directory.go mode change 100644 => 100755 renderer/func-maps.go mode change 100644 => 100755 renderer/group.go mode change 100644 => 100755 renderer/group_module.go mode change 100644 => 100755 renderer/journeys.go mode change 100644 => 100755 renderer/layout.go mode change 100644 => 100755 renderer/mailer.go mode change 100644 => 100755 renderer/members.go mode change 100644 => 100755 renderer/renderer.go mode change 100644 => 100755 renderer/support.go mode change 100644 => 100755 renderer/vehicle-management.go mode change 100644 => 100755 renderer/vehicles.go mode change 100644 => 100755 services/agenda.go mode change 100644 => 100755 services/fleets.go mode change 100644 => 100755 services/groupsmanagement.go mode change 100644 => 100755 services/mobilityaccounts.go mode change 100644 => 100755 services/services.go create mode 160000 themes mode change 100644 => 100755 utils/form-validators/form-validators.go mode change 100644 => 100755 utils/form-validators/phone-numbers.go mode change 100644 => 100755 utils/icons/svg-icons.go mode change 100644 => 100755 utils/identification/groups.go mode change 100644 => 100755 utils/identification/oidc.go mode change 100644 => 100755 utils/profile-pictures/profile-pictures.go mode change 100644 => 100755 utils/sorting/beneficiaries.go mode change 100644 => 100755 utils/sorting/events.go mode change 100644 => 100755 utils/sorting/fleets.go mode change 100644 => 100755 utils/sorting/groups.go mode change 100644 => 100755 utils/sorting/sorting.go mode change 100644 => 100755 utils/storage/cache.go mode change 100644 => 100755 utils/storage/etcd.go mode change 100644 => 100755 utils/storage/files.go mode change 100644 => 100755 utils/storage/kv.go mode change 100644 => 100755 utils/storage/minio.go mode change 100644 => 100755 utils/storage/sessions.go diff --git a/Dockerfile b/Dockerfile old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/config.go b/config.go old mode 100644 new mode 100755 diff --git a/go.mod b/go.mod old mode 100644 new mode 100755 diff --git a/go.sum b/go.sum old mode 100644 new mode 100755 diff --git a/handlers/api/api.go b/handlers/api/api.go old mode 100644 new mode 100755 diff --git a/handlers/api/cache.go b/handlers/api/cache.go old mode 100644 new mode 100755 index 433d7fc..ba72b72 --- a/handlers/api/cache.go +++ b/handlers/api/cache.go @@ -12,40 +12,58 @@ import ( func (h APIHandler) GetCache(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) cacheid := vars["cacheid"] - - d, err := h.cache.Get(cacheid) - if err != nil { - fmt.Println(err) - w.WriteHeader(http.StatusNotFound) - return - } - - result := d - - 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:] - } + // Use a channel to synchronize the goroutines + ch := make(chan []byte) + // Fetch data from cache asynchronously + go func() { + d, err := h.cache.Get(cacheid) + if err != nil { + fmt.Println(err) + w.WriteHeader(http.StatusNotFound) + ch <- nil + return } + 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 } - - j, err := json.Marshal(result) - if err != nil { - w.WriteHeader(http.StatusNotFound) - return - } - + // Send the JSON response to the client w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") 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 } diff --git a/handlers/api/export.go b/handlers/api/export.go old mode 100644 new mode 100755 diff --git a/handlers/api/geo.go b/handlers/api/geo.go old mode 100644 new mode 100755 diff --git a/handlers/api/oidc.go b/handlers/api/oidc.go old mode 100644 new mode 100755 diff --git a/handlers/application/administration.go b/handlers/application/administration.go old mode 100644 new mode 100755 diff --git a/handlers/application/agenda.go b/handlers/application/agenda.go old mode 100644 new mode 100755 diff --git a/handlers/application/application.go b/handlers/application/application.go old mode 100644 new mode 100755 diff --git a/handlers/application/beneficiaries.go b/handlers/application/beneficiaries.go old mode 100644 new mode 100755 diff --git a/handlers/application/dashboard.go b/handlers/application/dashboard.go old mode 100644 new mode 100755 diff --git a/handlers/application/directory.go b/handlers/application/directory.go old mode 100644 new mode 100755 diff --git a/handlers/application/group.go b/handlers/application/group.go old mode 100644 new mode 100755 diff --git a/handlers/application/group_module.go b/handlers/application/group_module.go old mode 100644 new mode 100755 diff --git a/handlers/application/journeys.go b/handlers/application/journeys.go old mode 100644 new mode 100755 diff --git a/handlers/application/members.go b/handlers/application/members.go old mode 100644 new mode 100755 diff --git a/handlers/application/support.go b/handlers/application/support.go old mode 100644 new mode 100755 diff --git a/handlers/application/vehicles-management.go b/handlers/application/vehicles-management.go old mode 100644 new mode 100755 diff --git a/handlers/application/vehicles.go b/handlers/application/vehicles.go old mode 100644 new mode 100755 diff --git a/handlers/auth/auth.go b/handlers/auth/auth.go old mode 100644 new mode 100755 diff --git a/handlers/auth/disconnect.go b/handlers/auth/disconnect.go old mode 100644 new mode 100755 diff --git a/handlers/auth/groups.go b/handlers/auth/groups.go old mode 100644 new mode 100755 diff --git a/handlers/auth/lost_password.go b/handlers/auth/lost_password.go old mode 100644 new mode 100755 diff --git a/handlers/auth/onboarding.go b/handlers/auth/onboarding.go old mode 100644 new mode 100755 diff --git a/handlers/exports/agenda.go b/handlers/exports/agenda.go old mode 100644 new mode 100755 diff --git a/handlers/exports/exports.go b/handlers/exports/exports.go old mode 100644 new mode 100755 diff --git a/handlers/exports/fleets.go b/handlers/exports/fleets.go old mode 100644 new mode 100755 diff --git a/main.go b/main.go old mode 100644 new mode 100755 diff --git a/renderer/administration.go b/renderer/administration.go old mode 100644 new mode 100755 diff --git a/renderer/agenda.go b/renderer/agenda.go old mode 100644 new mode 100755 diff --git a/renderer/auth.go b/renderer/auth.go old mode 100644 new mode 100755 diff --git a/renderer/beneficiaries.go b/renderer/beneficiaries.go old mode 100644 new mode 100755 diff --git a/renderer/dashboard.go b/renderer/dashboard.go old mode 100644 new mode 100755 diff --git a/renderer/directory.go b/renderer/directory.go old mode 100644 new mode 100755 diff --git a/renderer/func-maps.go b/renderer/func-maps.go old mode 100644 new mode 100755 diff --git a/renderer/group.go b/renderer/group.go old mode 100644 new mode 100755 diff --git a/renderer/group_module.go b/renderer/group_module.go old mode 100644 new mode 100755 diff --git a/renderer/journeys.go b/renderer/journeys.go old mode 100644 new mode 100755 diff --git a/renderer/layout.go b/renderer/layout.go old mode 100644 new mode 100755 diff --git a/renderer/mailer.go b/renderer/mailer.go old mode 100644 new mode 100755 diff --git a/renderer/members.go b/renderer/members.go old mode 100644 new mode 100755 diff --git a/renderer/renderer.go b/renderer/renderer.go old mode 100644 new mode 100755 diff --git a/renderer/support.go b/renderer/support.go old mode 100644 new mode 100755 diff --git a/renderer/vehicle-management.go b/renderer/vehicle-management.go old mode 100644 new mode 100755 diff --git a/renderer/vehicles.go b/renderer/vehicles.go old mode 100644 new mode 100755 diff --git a/services/agenda.go b/services/agenda.go old mode 100644 new mode 100755 diff --git a/services/fleets.go b/services/fleets.go old mode 100644 new mode 100755 diff --git a/services/groupsmanagement.go b/services/groupsmanagement.go old mode 100644 new mode 100755 diff --git a/services/mobilityaccounts.go b/services/mobilityaccounts.go old mode 100644 new mode 100755 diff --git a/services/services.go b/services/services.go old mode 100644 new mode 100755 diff --git a/themes b/themes new file mode 160000 index 0000000..d6de19d --- /dev/null +++ b/themes @@ -0,0 +1 @@ +Subproject commit d6de19d8e238fec2f03957abdfa7b49a463d4a3c diff --git a/utils/form-validators/form-validators.go b/utils/form-validators/form-validators.go old mode 100644 new mode 100755 diff --git a/utils/form-validators/phone-numbers.go b/utils/form-validators/phone-numbers.go old mode 100644 new mode 100755 diff --git a/utils/icons/svg-icons.go b/utils/icons/svg-icons.go old mode 100644 new mode 100755 diff --git a/utils/identification/groups.go b/utils/identification/groups.go old mode 100644 new mode 100755 diff --git a/utils/identification/oidc.go b/utils/identification/oidc.go old mode 100644 new mode 100755 diff --git a/utils/profile-pictures/profile-pictures.go b/utils/profile-pictures/profile-pictures.go old mode 100644 new mode 100755 diff --git a/utils/sorting/beneficiaries.go b/utils/sorting/beneficiaries.go old mode 100644 new mode 100755 diff --git a/utils/sorting/events.go b/utils/sorting/events.go old mode 100644 new mode 100755 diff --git a/utils/sorting/fleets.go b/utils/sorting/fleets.go old mode 100644 new mode 100755 diff --git a/utils/sorting/groups.go b/utils/sorting/groups.go old mode 100644 new mode 100755 diff --git a/utils/sorting/sorting.go b/utils/sorting/sorting.go old mode 100644 new mode 100755 diff --git a/utils/storage/cache.go b/utils/storage/cache.go old mode 100644 new mode 100755 diff --git a/utils/storage/etcd.go b/utils/storage/etcd.go old mode 100644 new mode 100755 index 14d044d..fb4512a --- a/utils/storage/etcd.go +++ b/utils/storage/etcd.go @@ -74,6 +74,7 @@ func NewEtcdHandler(cfg *viper.Viper) (*EtcdHandler, error) { Password: password, DialTimeout: 5 * time.Second, }) + fmt.Println(endpoints,prefix,username,password) if err != nil { fmt.Println(err) return nil, err diff --git a/utils/storage/files.go b/utils/storage/files.go old mode 100644 new mode 100755 diff --git a/utils/storage/kv.go b/utils/storage/kv.go old mode 100644 new mode 100755 diff --git a/utils/storage/minio.go b/utils/storage/minio.go old mode 100644 new mode 100755 diff --git a/utils/storage/sessions.go b/utils/storage/sessions.go old mode 100644 new mode 100755