Change initial SpaceAPI response source

- The environment variable is now called SPACE_API_JSON_TEMPLATE_PATH
- It is now the path to a file with the initial values, not the JSON itself
- Updated README.md accordingly
This commit is contained in:
Bendodroid 2023-11-04 23:30:26 +01:00
commit 1a9922d5f1
Signed by: bendodroid
GPG key ID: 3EEE19A0F73D5FFC
2 changed files with 26 additions and 14 deletions

28
main.go
View file

@ -6,6 +6,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"slices"
"strconv"
"time"
@ -14,6 +15,8 @@ import (
)
func main() {
var err error
validUsername, success := os.LookupEnv("DOORIS_USERNAME")
if !success || validUsername == "" {
log.Fatalln("Could not retrieve DOORIS_API_KEY env variable or variable is empty")
@ -24,19 +27,30 @@ func main() {
log.Fatalln("Could not retrieve DOORIS_API_KEY env variable or variable is empty")
}
initialJson, success := os.LookupEnv("SPACE_API_JSON")
if !success || initialJson == "" {
log.Fatalln("Could not retrieve SPACE_API_JSON env variable or variable is empty")
templatePath, success := os.LookupEnv("SPACE_API_JSON_TEMPLATE_PATH")
if !success || templatePath == "" {
log.Fatalln("Could not retrieve SPACE_API_JSON_TEMPLATE_PATH env variable or variable is empty")
}
templatePathAbs, err := filepath.Abs(templatePath)
if err != nil {
log.Fatalln("Failed converting", templatePath, "to absolute path:", err)
}
log.Println("Reading initial SpaceAPI response from", templatePathAbs)
initialJson, err := os.ReadFile(templatePathAbs)
if err != nil {
log.Fatalln("Failed reading file:", err)
}
spaceApiResponse := new(types.SpaceAPIResponseV14)
err := json.Unmarshal([]byte(initialJson), spaceApiResponse)
err = json.Unmarshal(initialJson, spaceApiResponse)
if err != nil {
log.Fatalln("Could not parse provided JSON")
log.Fatalln("Could not parse SpaceAPI response template:", err)
}
if !slices.Contains(spaceApiResponse.APICompatibility, "14") {
log.Fatalln("Provided JSON doesn't specify compatibility with API version 14")
log.Fatalln("Provided SpaceAPI response doesn't specify compatibility with API version 14")
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@ -49,7 +63,7 @@ func main() {
response, err := json.Marshal(spaceApiResponse)
if err != nil {
log.Println("Failed to serialize JSON response")
log.Println("Failed to serialize JSON response:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}