feat: complete cmpp platform phases 0-5
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Status struct {
|
||||
Status string `json:"status"`
|
||||
Service string `json:"service"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
}
|
||||
|
||||
func Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(Status{
|
||||
Status: "ok",
|
||||
Service: "cmpp-gateway",
|
||||
Timestamp: time.Now().UTC().Format(time.RFC3339Nano),
|
||||
})
|
||||
})
|
||||
return mux
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHealthHandler(t *testing.T) {
|
||||
server := httptest.NewServer(Handler())
|
||||
defer server.Close()
|
||||
|
||||
response, err := http.Get(server.URL + "/health")
|
||||
if err != nil {
|
||||
t.Fatalf("get health: %v", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
t.Fatalf("unexpected status: %d", response.StatusCode)
|
||||
}
|
||||
|
||||
var payload Status
|
||||
if err := json.NewDecoder(response.Body).Decode(&payload); err != nil {
|
||||
t.Fatalf("decode health payload: %v", err)
|
||||
}
|
||||
if payload.Status != "ok" || payload.Service != "cmpp-gateway" {
|
||||
t.Fatalf("unexpected payload: %+v", payload)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user