From b75730146efaa1b3244d711b4183a8246eca6f25 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Thu, 28 Sep 2023 10:13:41 +0200 Subject: [PATCH] Initial working server/generator --- .gitignore | 2 ++ README.md | 6 +++++ cmd/server.go | 46 ++++++++++++++++++++++++++++++++++++++ generate-spaceapi-types.sh | 15 +++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 .gitignore create mode 100644 cmd/server.go create mode 100755 generate-spaceapi-types.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..001310d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/spaceapi-schema.json +/pkg/spaceapi/ diff --git a/README.md b/README.md index feec449..de10922 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,8 @@ # spaceapi +# Build/Use + +``` +go generate +go run cmd/server.go +``` diff --git a/cmd/server.go b/cmd/server.go new file mode 100644 index 0000000..843b59b --- /dev/null +++ b/cmd/server.go @@ -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) + } +} diff --git a/generate-spaceapi-types.sh b/generate-spaceapi-types.sh new file mode 100755 index 0000000..ff3fe14 --- /dev/null +++ b/generate-spaceapi-types.sh @@ -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 \ No newline at end of file