From 25e1065f0564207f3a4b8eb5ff8f1cc6e89fdd8b Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Sun, 23 Jun 2024 12:48:27 +0100 Subject: [PATCH] Output slice from owned endpoint --- servers/api.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/servers/api.go b/servers/api.go index 8234506..325fad5 100644 --- a/servers/api.go +++ b/servers/api.go @@ -14,6 +14,7 @@ import ( vUtils "github.com/1f349/violet/utils" "github.com/julienschmidt/httprouter" "net/http" + "slices" "strconv" "time" ) @@ -46,6 +47,7 @@ func NewApiServer(listen string, db *database.Queries, signer mjwt.Verifier, dom }) // Endpoint for grabbing owned certificates + // TODO(melon): rewrite this endpoint to prevent using a map then converting into a slice later r.GET("/owned", checkAuthWithPerm(signer, "orchid:cert", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims) { domains := getDomainOwnershipClaims(b.Claims.Perms) domainMap := make(map[string]bool) @@ -107,8 +109,18 @@ func NewApiServer(listen string, db *database.Queries, signer mjwt.Verifier, dom m[c.Id] = &c } } + + // remap into a slice + arr := make([]*Certificate, 0, len(m)) + slices.SortFunc(arr, func(a, b *Certificate) int { + return int(a.Id - b.Id) + }) + for _, v := range m { + arr = append(arr, v) + } + rw.WriteHeader(http.StatusOK) - _ = json.NewEncoder(rw).Encode(m) + _ = json.NewEncoder(rw).Encode(arr) })) // Endpoint for looking up a certificate