Skip to content

Commit

Permalink
Server: fixed 2 routes that returned item related json data. Reduced …
Browse files Browse the repository at this point in the history
…slightly size and ram usage
  • Loading branch information
luskaner committed Jul 22, 2024
1 parent 203846d commit bd3c4a8
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 30,384 deletions.
21 changes: 15 additions & 6 deletions server/files/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package files

import (
"encoding/json"
"fmt"
orderedmap "github.com/wk8/go-ordered-map/v2"
"net/http"
"os"
Expand All @@ -18,7 +19,9 @@ var keyedFilenames = map[string]struct{}{

var Login []i.A
var ArrayFiles = make(map[string]i.A)
var KeyedFiles = make(map[string]*orderedmap.OrderedMap[string, any])

var KeyedFiles = make(map[string][]byte)
var NameToSignature = make(map[string]string)
var CloudFiles models.CloudFilesIndexMap

func Initialize() {
Expand Down Expand Up @@ -66,7 +69,10 @@ func initializeResponses() {
var result = orderedmap.New[string, any]()
err = json.Unmarshal(data, result)
if err == nil {
KeyedFiles[name] = result
rawSignature, _ := result.Get("dataSignature")
serverSignature := rawSignature.(string)
KeyedFiles[name] = data
NameToSignature[name] = serverSignature
}
} else {
var result i.A
Expand All @@ -91,19 +97,22 @@ func ReturnSignedAsset(name string, w *http.ResponseWriter, r *http.Request, key
var response any
if keyedResponse {
response = KeyedFiles[name]
rawSignature, _ := response.(*orderedmap.OrderedMap[string, any]).Get("dataSignature")
serverSignature = rawSignature.(string)
serverSignature = NameToSignature[name]
} else {
response = ArrayFiles[name]
arrayResponse := response.(i.A)
serverSignature = arrayResponse[len(arrayResponse)-1].(string)
}
if r.URL.Query().Get("signature") != serverSignature {
i.JSON(w, response)
if keyedResponse {
i.RawJSON(w, response.([]byte))
} else {
i.JSON(w, response)
}
return
}
if keyedResponse {
i.JSON(w, i.H{"result": 0, "dataSignature": serverSignature})
i.RawJSON(w, []byte(fmt.Sprintf(`{"result":0,"dataSignature":"%s"}`, serverSignature)))
} else {
emptyArrays := make(i.A, len(response.(i.A))-2)
ret := i.A{0}
Expand Down
5 changes: 5 additions & 0 deletions server/internal/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ func JSON(w *http.ResponseWriter, data any) {
_ = json.NewEncoder(*w).Encode(data)
}

func RawJSON(w *http.ResponseWriter, data []byte) {
(*w).Header().Set("Content-Type", "application/json")
_, _ = (*w).Write(data)
}

func decode(dst interface{}, src map[string][]string) error {
err := decoder.Decode(dst, src)
if err == nil {
Expand Down
Loading

0 comments on commit bd3c4a8

Please sign in to comment.