From b378f30c053c57bb6970eabae5465d77f4b8ab92 Mon Sep 17 00:00:00 2001 From: Tueem Date: Mon, 18 May 2026 14:41:25 +0200 Subject: [PATCH] feat(svg): add GET endpoint for all svg templates --- internal/routes/get.go | 39 +++++++++++++++++++++++++++++++++++++++ internal/server/http.go | 10 +++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 internal/routes/get.go diff --git a/internal/routes/get.go b/internal/routes/get.go new file mode 100644 index 0000000..f9badf1 --- /dev/null +++ b/internal/routes/get.go @@ -0,0 +1,39 @@ +package routes + +import ( + "encoding/json" + "net/http" + + "tomatentum.net/svg-templater/internal/database" +) + +func Get(w http.ResponseWriter, r *http.Request) { + + svgs, err := database.GetSVG() + if err != nil { + http.Error(w, "Cannot fetch svgs from database", http.StatusInternalServerError) + return + } + /* + data, err := json.Marshal(svgs) + + if err != nil { + http.Error(w, "An Error occurred while encoding json.", http.StatusInternalServerError) + return + } + + _, err = w.Write(data) + + if err != nil { + http.Error(w, "Could not write data.", http.StatusInternalServerError) + return + } + */ + + w.Header().Add("Content-Type", "application/json") + enc := json.NewEncoder(w) + + for _, svg := range svgs { + enc.Encode(svg) + } +} diff --git a/internal/server/http.go b/internal/server/http.go index 633cccd..cf11d37 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -15,11 +15,15 @@ func PrepareHTTP() { fmt.Fprintln(w, "You are authorized!") }) registerAuthorizedFunc("/svg/", func(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + switch r.Method { + case "POST": + routes.CreateSVG(w, r) + return + case "GET": + routes.Get(w, r) return } - routes.CreateSVG(w, r) + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) }) registerAuthorizedFunc("/svg/{id}", func(w http.ResponseWriter, r *http.Request) { switch r.Method {