2024-01-13 23:26:04 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Root(resp *types.SpaceAPIResponseV14) 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", "*")
|
2024-01-16 01:24:19 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2024-01-13 23:26:04 +01:00
|
|
|
_, _ = w.Write(response)
|
|
|
|
}
|
|
|
|
}
|