From 1a9922d5f148cc3b315afee7fc43cd3c41e69798 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Sat, 4 Nov 2023 23:30:26 +0100 Subject: [PATCH] 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 --- README.md | 12 +++++------- main.go | 28 +++++++++++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index eb58b77..d20c944 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,12 @@ # SpaceAPI Daemon -`spaceapid` serves a [SpaceAPI](https://spaceapi.io)-compatible JSON on port -8080: +`spaceapid` serves a [SpaceAPI](https://spaceapi.io)-compatible JSON on port 8080: ```shell curl -X GET http://localhost:8080 ``` -The state of the boolean `state->open` property can be modified via -`/state/open`: +The state of the boolean `state->open` property can be modified via `/state/open`: ```shell curl -X PUT -u user:password -d true http://localhost:8080/state/open @@ -16,8 +14,8 @@ curl -X PUT -u user:password -d true http://localhost:8080/state/open ## Building -See the `go.mod` file for minimum required Go version. There are currently no -dependencies other than the Go standard library. +See the `go.mod` file for minimum required Go version. There are currently no dependencies apart from the Go +standard library. ```shell go build . @@ -28,5 +26,5 @@ go build . Substitute the environment variables with appropriate custom values. ```shell -env DOORIS_USERNAME=user DOORIS_PASSWORD=password SPACE_API_JSON="$(cat ccchh-template.json)" go run . +env DOORIS_USERNAME=user DOORIS_PASSWORD=password SPACE_API_JSON_TEMPLATE_PATH=ccchh-template.json go run . ``` diff --git a/main.go b/main.go index 97cc493..d8f399d 100644 --- a/main.go +++ b/main.go @@ -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 }