spaceapid/handlers/root.go
bitwhisker 2cc08483a2
fix(handlers): values where omited when false and set go min version to 1.25
switching from v1 to v2 of encoding/json in the root handler to not omit
false values in the spaceapi response.
this increases the min go version to 1.25.
2026-10-05 14:10:36 +02:00

33 lines
820 B
Go

package handlers
import (
"encoding/json/v2"
"log"
"net/http"
"git.hamburg.ccc.de/ccchh/spaceapid/types"
)
func Root(resp *types.SpaceAPIResponse) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Check if GET method
if r.Method != http.MethodGet {
log.Println("Wrong METHOD from", r.RemoteAddr)
w.Header().Set("Allow", http.MethodGet)
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// Serialize response
response, err := json.Marshal(resp)
if err != nil {
log.Println("Failed to serialize JSON response:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, _ = w.Write(response)
}
}