Delete types/v14.go, replace with v15.go.
Rename internal types to not include message version.
I don't currently see the need to support multiple message
versions that have incompatible layouts.
v15 is backwards compatible with v14, so the config example now
specifies both.
Rename the "response" field in the config file to "static" as
dynamic/static makes more sense from the user's perspective.
(Internally it's still SpaceapidConfig.{Dynamic,Response}
to align with type names and usage.)
33 lines
817 B
Go
33 lines
817 B
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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)
|
|
}
|
|
}
|