diff --git a/Dockerfile b/Dockerfile index b76f3b3..2c0c7b7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ WORKDIR /app RUN apk add --no-cache bash wget RUN ./generate-spaceapi-types.sh -RUN GOOS=linux go build -o oko-spaceapi cmd/server.go +RUN GOOS=linux go build -o oko-spaceapi cmd/cmd.go FROM alpine WORKDIR /app diff --git a/cmd/cmd.go b/cmd/cmd.go new file mode 100644 index 0000000..f33918a --- /dev/null +++ b/cmd/cmd.go @@ -0,0 +1,9 @@ +package main + +import "git.okoyono.de/oko-intern/okospaceapi/pkg/api" + +//go:generate ../generate-spaceapi-types.sh + +func main() { + api.StartServer() +} diff --git a/go.mod b/go.mod index 7755745..2352d17 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module git.okoyono.de/oko-intern/okospaceapi -go 1.21.1 +go 1.21.6 diff --git a/cmd/server.go b/pkg/api/definition.go similarity index 75% rename from cmd/server.go rename to pkg/api/definition.go index d4cd0d1..aed92b7 100644 --- a/cmd/server.go +++ b/pkg/api/definition.go @@ -1,15 +1,13 @@ -package main +package api import ( + "encoding/json" + "git.okoyono.de/oko-intern/okospaceapi/pkg/location" "git.okoyono.de/oko-intern/okospaceapi/pkg/spaceapi" - "net/http" ) -//go:generate ../generate-spaceapi-types.sh - -func main() { +func BuildDefinition() []byte { definition := spaceapi.Root{ - // TODO: Fill up with stuff ApiCompatibility: []string{"14"}, Space: "økoyono", Url: "https://okoyono.de/", @@ -19,8 +17,6 @@ func main() { Matrix: "#public:matrix.okoyono.de", }, Location: &spaceapi.Location{ - Lat: 27.987850, // Latitude and Longitude of Mount Everest :) - Lon: 86.925026, Timezone: "Europe/Berlin", }, Logo: "https://okoyono.de/images/ant.svg", @@ -45,7 +41,6 @@ func main() { Name: "searxng", Url: "https://search.okoyono.de", }, - }, Projects: []string{ "https://buchdesmonats.okoyono.de/", @@ -71,19 +66,16 @@ func main() { }, } - json, err := definition.MarshalJSON() + // Random location + randomLocation := location.SpinThatWheel() + definition.Location.Lat = randomLocation.Latitude + definition.Location.Lon = randomLocation.Longitude + definition.Location.Address = randomLocation.Name + ", Germany" + + jsonString, err := json.Marshal(definition) 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) - } + return jsonString } diff --git a/pkg/api/server.go b/pkg/api/server.go new file mode 100644 index 0000000..222ddea --- /dev/null +++ b/pkg/api/server.go @@ -0,0 +1,17 @@ +package api + +import "net/http" + +func StartServer() { + err := http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, err := w.Write(BuildDefinition()) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + })) + + if err != nil { + panic(err) + } +} diff --git a/pkg/location/wheel_of_fortune.go b/pkg/location/wheel_of_fortune.go new file mode 100644 index 0000000..9cd15d8 --- /dev/null +++ b/pkg/location/wheel_of_fortune.go @@ -0,0 +1,20 @@ +package location + +import "math/rand" + +type Place struct { + Latitude float64 + Longitude float64 + Name string +} + +func SpinThatWheel() Place { + // Cool places to be in Germany :) + places := []Place{ + {Latitude: 48.858370, Longitude: 2.294481, Name: "Eifeltower"}, + {Latitude: 27.987850, Longitude: 86.925026, Name: "Mount Everest"}, + } + + pick := rand.Int() % len(places) + return places[pick] +}