Skip to content

Commit

Permalink
Add rest endpoint with status preview
Browse files Browse the repository at this point in the history
  • Loading branch information
undg committed Feb 11, 2025
1 parent a599e99 commit d5fd489
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
14 changes: 14 additions & 0 deletions json/jsonSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}))
}
Expand All @@ -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())
}
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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:
Expand Down

0 comments on commit d5fd489

Please sign in to comment.