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
parent 27a054d985
commit 1a9922d5f1
Signed by: bendodroid
GPG key ID: 3EEE19A0F73D5FFC
2 changed files with 26 additions and 14 deletions

View file

@ -1,14 +1,12 @@
# SpaceAPI Daemon # SpaceAPI Daemon
`spaceapid` serves a [SpaceAPI](https://spaceapi.io)-compatible JSON on port `spaceapid` serves a [SpaceAPI](https://spaceapi.io)-compatible JSON on port 8080:
8080:
```shell ```shell
curl -X GET http://localhost:8080 curl -X GET http://localhost:8080
``` ```
The state of the boolean `state->open` property can be modified via The state of the boolean `state->open` property can be modified via `/state/open`:
`/state/open`:
```shell ```shell
curl -X PUT -u user:password -d true http://localhost:8080/state/open 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 ## Building
See the `go.mod` file for minimum required Go version. There are currently no See the `go.mod` file for minimum required Go version. There are currently no dependencies apart from the Go
dependencies other than the Go standard library. standard library.
```shell ```shell
go build . go build .
@ -28,5 +26,5 @@ go build .
Substitute the environment variables with appropriate custom values. Substitute the environment variables with appropriate custom values.
```shell ```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 .
``` ```

28
main.go
View file

@ -6,6 +6,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"path/filepath"
"slices" "slices"
"strconv" "strconv"
"time" "time"
@ -14,6 +15,8 @@ import (
) )
func main() { func main() {
var err error
validUsername, success := os.LookupEnv("DOORIS_USERNAME") validUsername, success := os.LookupEnv("DOORIS_USERNAME")
if !success || validUsername == "" { if !success || validUsername == "" {
log.Fatalln("Could not retrieve DOORIS_API_KEY env variable or variable is empty") 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") log.Fatalln("Could not retrieve DOORIS_API_KEY env variable or variable is empty")
} }
initialJson, success := os.LookupEnv("SPACE_API_JSON") templatePath, success := os.LookupEnv("SPACE_API_JSON_TEMPLATE_PATH")
if !success || initialJson == "" { if !success || templatePath == "" {
log.Fatalln("Could not retrieve SPACE_API_JSON env variable or variable is empty") 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) spaceApiResponse := new(types.SpaceAPIResponseV14)
err := json.Unmarshal([]byte(initialJson), spaceApiResponse) err = json.Unmarshal(initialJson, spaceApiResponse)
if err != nil { 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") { 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) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@ -49,7 +63,7 @@ func main() {
response, err := json.Marshal(spaceApiResponse) response, err := json.Marshal(spaceApiResponse)
if err != nil { if err != nil {
log.Println("Failed to serialize JSON response") log.Println("Failed to serialize JSON response:", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }