this repo has no description
1package routes 2 3import ( 4 "encoding/json" 5 "net/http" 6 7 "github.com/go-chi/chi/v5" 8 "github.com/icyphox/bild/config" 9 "github.com/icyphox/bild/db" 10) 11 12type InternalHandle struct { 13 c *config.Config 14 db *db.DB 15} 16 17func SetupInternal(c *config.Config, db *db.DB) http.Handler { 18 ih := &InternalHandle{ 19 c: c, 20 db: db, 21 } 22 23 r := chi.NewRouter() 24 r.Route("/internal/allkeys", func(r chi.Router) { 25 r.Get("/", ih.AllKeys) 26 }) 27 28 return r 29} 30 31func (h *InternalHandle) returnJSON(w http.ResponseWriter, data interface{}) error { 32 w.Header().Set("Content-Type", "application/json") 33 res, err := json.Marshal(data) 34 if err != nil { 35 return err 36 } 37 _, err = w.Write(res) 38 return err 39} 40 41func (h *InternalHandle) returnErr(w http.ResponseWriter, err error) error { 42 w.WriteHeader(http.StatusInternalServerError) 43 return h.returnJSON(w, map[string]string{ 44 "error": err.Error(), 45 }) 46} 47 48func (h *InternalHandle) AllKeys(w http.ResponseWriter, r *http.Request) { 49 keys, err := h.db.GetAllPublicKeys() 50 if err != nil { 51 h.returnErr(w, err) 52 return 53 } 54 keyMap := map[string]string{} 55 for _, key := range keys { 56 keyMap[key.DID] = key.Key 57 } 58 if err := h.returnJSON(w, keyMap); err != nil { 59 h.returnErr(w, err) 60 return 61 } 62}