From cde3f90d2d41d2fd459492f31ef358a3eba145c2 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 17 Jan 2024 16:05:11 -0500 Subject: [PATCH] feat: implement GET /s5/registry --- api/s5.go | 3 +++ api/s5/http.go | 30 ++++++++++++++++++++++++++++++ api/s5/messages.go | 6 ++++++ 3 files changed, 39 insertions(+) diff --git a/api/s5.go b/api/s5.go index 7c00f33..6b84878 100644 --- a/api/s5.go +++ b/api/s5.go @@ -47,5 +47,8 @@ func getRoutes(h *s5.HttpHandler, portal interfaces.Portal) map[string]jape.Hand // Debug API "/s5/debug/download_urls/:cid": s5.AuthMiddleware(h.DebugDownloadUrls, portal), + + //Registry API + "GET /s5/registry": s5.AuthMiddleware(h.RegistryQuery, portal), } } diff --git a/api/s5/http.go b/api/s5/http.go index ad06546..dbfd8fe 100644 --- a/api/s5/http.go +++ b/api/s5/http.go @@ -942,6 +942,36 @@ func (h *HttpHandler) DebugDownloadUrls(jc jape.Context) { _, _ = jc.ResponseWriter.Write([]byte(strings.Join(output, "\n"))) } +func (h *HttpHandler) RegistryQuery(jc jape.Context) { + var pk string + + if jc.DecodeForm("pk", &pk) != nil { + return + } + + pkBytes, err := base64.RawURLEncoding.DecodeString(pk) + if jc.Check("error decoding pk", err) != nil { + return + } + + entry, err := h.getNode().Services().Registry().Get(pkBytes) + if jc.Check("error getting registry entry", err) != nil { + return + } + + if entry == nil { + jc.ResponseWriter.WriteHeader(http.StatusNotFound) + return + } + + jc.Encode(&RegistryQueryResponse{ + Pk: base64.RawURLEncoding.EncodeToString(entry.PK()), + Revision: entry.Revision(), + Data: base64.RawURLEncoding.EncodeToString(entry.Data()), + Signature: base64.RawURLEncoding.EncodeToString(entry.Signature()), + }) +} + func (h *HttpHandler) getNode() s5interfaces.Node { proto, _ := h.portal.ProtocolRegistry().Get("s5") protoInstance := proto.(*protocols.S5Protocol) diff --git a/api/s5/messages.go b/api/s5/messages.go index cd73130..0a0c393 100644 --- a/api/s5/messages.go +++ b/api/s5/messages.go @@ -53,3 +53,9 @@ type AccountStatsTotal struct { type AppUploadResponse struct { CID string `json:"cid"` } +type RegistryQueryResponse struct { + Pk string `json:"pk"` + Revision uint64 `json:"revision"` + Data string `json:"data"` + Signature string `json:"signature"` +}