Add a wheel of fortune and fix json errors
All checks were successful
/ docker-image (push) Successful in 1m30s
/ deployment (push) Successful in 0s

This commit is contained in:
Aaron Fischer 2024-01-24 11:45:19 +01:00
parent 626b3827d2
commit 162d1cc00c
6 changed files with 60 additions and 22 deletions

View file

@ -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

9
cmd/cmd.go Normal file
View file

@ -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()
}

2
go.mod
View file

@ -1,3 +1,3 @@
module git.okoyono.de/oko-intern/okospaceapi
go 1.21.1
go 1.21.6

View file

@ -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
}

17
pkg/api/server.go Normal file
View file

@ -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)
}
}

View file

@ -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]
}