From d5fd4891168686909cc24eff711d2608db06c37a Mon Sep 17 00:00:00 2001 From: undg Date: Tue, 11 Feb 2025 10:36:54 +0000 Subject: [PATCH] Add rest endpoint with status preview --- json/jsonSchema.go | 14 ++++++++++++++ main.go | 10 ++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/json/jsonSchema.go b/json/jsonSchema.go index ebaec40..48fd185 100644 --- a/json/jsonSchema.go +++ b/json/jsonSchema.go @@ -29,6 +29,16 @@ func serveSchemaJSON(w http.ResponseWriter, t reflect.Type) { fmt.Fprint(w, string(b)) } +func serveRestJSON(w http.ResponseWriter, restJSON interface{}) { + + w.Header().Set("Content-Type", "application/json") + + if err := json.NewEncoder(w).Encode(restJSON); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + func ServeStatusSchemaJSON(w http.ResponseWriter, r *http.Request) { serveSchemaJSON(w, reflect.TypeOf(pactl.Status{})) } @@ -40,3 +50,7 @@ func ServeMessageSchemaJSON(w http.ResponseWriter, r *http.Request) { func ServeResponseSchemaJSON(w http.ResponseWriter, r *http.Request) { serveSchemaJSON(w, reflect.TypeOf(Response{})) } + +func ServeStatusRestJSON(w http.ResponseWriter, r *http.Request) { + serveRestJSON(w, pactl.GetStatus()) +} diff --git a/main.go b/main.go index 601e898..6b6f4eb 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( "os" "github.com/undg/go-prapi/buildinfo" - "github.com/undg/go-prapi/json" + prapiJSON "github.com/undg/go-prapi/json" "github.com/undg/go-prapi/utils" "github.com/undg/go-prapi/ws" ) @@ -18,11 +18,13 @@ func startServer(mux *http.ServeMux) { mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/api/v1/schema/status": - json.ServeStatusSchemaJSON(w, r) + prapiJSON.ServeStatusSchemaJSON(w, r) case "/api/v1/schema/message": - json.ServeMessageSchemaJSON(w, r) + prapiJSON.ServeMessageSchemaJSON(w, r) case "/api/v1/schema/response": - json.ServeResponseSchemaJSON(w, r) + prapiJSON.ServeResponseSchemaJSON(w, r) + case "/api/v1/status": + prapiJSON.ServeStatusRestJSON(w, r) case "/api/v1/ws": ws.HandleWebSocket(w, r) default: