Initial working server/generator

This commit is contained in:
Aaron Fischer 2023-09-28 10:13:41 +02:00
parent 58cfc5d312
commit b75730146e
4 changed files with 69 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/spaceapi-schema.json
/pkg/spaceapi/

View file

@ -1,2 +1,8 @@
# spaceapi
# Build/Use
```
go generate
go run cmd/server.go
```

46
cmd/server.go Normal file
View file

@ -0,0 +1,46 @@
package main
import (
"net/http"
"oko-spaceapi/pkg/spaceapi"
)
//go:generate ../generate-spaceapi-types.sh
func main() {
definition := spaceapi.Root{
// TODO: Fill up with stuff
ApiCompatibility: []string{"14"},
Links: []*spaceapi.LinksItems{
{
Description: "Out main website",
Name: "website",
Url: "https://okoyono.de",
},
},
Space: "økoyono",
Url: "https://okoyono.de/",
Contact: &spaceapi.Contact{
Email: "contact@okoyono.de",
},
Location: &spaceapi.Location{
Address: "",
},
}
json, err := definition.MarshalJSON()
if err != nil {
panic(err)
}
err = http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, err := w.Write(json)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}))
if err != nil {
panic(err)
}
}

15
generate-spaceapi-types.sh Executable file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env bash
script="$(realpath "${BASH_SOURCE[-1]}")"
path="$(dirname "$script")"
cd $path
wget -O spaceapi-schema.json https://raw.githubusercontent.com/SpaceApi/schema/master/14.json
go install github.com/a-h/generate/cmd/schema-generate@latest
mkdir -p pkg/spaceapi
~/go/bin/schema-generate -o pkg/spaceapi/spaceapi.go -p spaceapi spaceapi-schema.json
rm spaceapi-schema.json
# TODO: Do it inline
# https://github.com/a-h/generate/blob/master/cmd/schema-generate/main.go