diff --git a/.forgejo/workflows/docker-image.yaml b/.forgejo/workflows/docker-image.yaml new file mode 100644 index 0000000..6cbdcc1 --- /dev/null +++ b/.forgejo/workflows/docker-image.yaml @@ -0,0 +1,49 @@ +on: + push: + branches: + - main + +jobs: + docker-image: + runs-on: docker + steps: + - name: Install dependencies + run: | + echo deb http://deb.debian.org/debian bullseye-backports main | tee /etc/apt/sources.list.d/backports.list + apt-get update -qq + apt-get install -y git + DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -qq -y -t bullseye-backports docker.io + + - name: Checkout the sourcecode + uses: actions/checkout@v3 + + - name: Set up Docker build environment (rootless) + uses: docker/setup-buildx-action@v3 + with: + driver-opts: | + image=moby/buildkit:v0.12.1-rootless + network=host + + - name: Login to forgejo docker registry + uses: docker/login-action@v3 + with: + registry: git.okoyono.de + username: ${{ secrets.DOCKER_REGISTRY_USERNAME }} + password: ${{ secrets.DOCKER_REGISTRY_PASSWORD }} + + - name: Build and push the docker image + uses: docker/build-push-action@v5 + with: + push: true + tags: git.okoyono.de/okoyono-intern/okospaceapi:latest + + deployment: + runs-on: self-hosted + steps: + # As an alternative, we could connect to the server via ssh and execute the + # script directly, but I like the webhook thing much more, because it is + # so much more versatile. + - name: Deploy via webhook + run: | + apk add curl + curl -v -X GET "http://host.docker.internal:8100?token=${{ secrets.DEPLOY_TOKEN }}&script=deploy-spaceapi.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/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b76f3b3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM golang:alpine AS build +COPY . /app +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 + +FROM alpine +WORKDIR /app +COPY --from=build /app/oko-spaceapi /app/oko-spaceapi +EXPOSE 8080 +CMD ["/app/oko-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..93b4f98 --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,103 @@ +package main + +import ( + "git.okoyono.de/oko-intern/okospaceapi/pkg/spaceapi" + "net/http" +) + +//go:generate ../generate-spaceapi-types.sh + +func main() { + definition := spaceapi.Root{ + // TODO: Fill up with stuff + ApiCompatibility: []string{"14"}, + Space: "økoyono", + Url: "https://okoyono.de/", + Contact: &spaceapi.Contact{ + Email: "oko@datenhalter.de", // TODO: Better email + Mastodon: "@f@social.okoyono.de", // TODO: Replace with real handle + Matrix: "#public:matrix.okoyono.de", + }, + Location: &spaceapi.Location{ + Lat: 0.0, // TODO: Fill with cool coordinates + Lon: 0.0, // TODO: Fill with cool coordinates + Timezone: "Europe/Berlin", + }, + Logo: "https://okoyono.de/images/ant.svg", + Links: []*spaceapi.LinksItems{ + { + Description: "Website", + Name: "website", + Url: "https://okoyono.de", + }, + { + Description: "Forgejo (Sourcecode)", + Name: "forgejo", + Url: "https://git.okoyono.de", + }, + { + Description: "Mastodon (Social Media)", + Name: "mastodon", + Url: "https://social.okoyono.de", + }, + { + Description: "OwnCast (Tube)", + Name: "owncast", + Url: "https://tube.okoyono.de", + }, + { + Description: "SearXNG (Search)", + Name: "searxng", + Url: "https://search.okoyono.de", + }, + { + Description: "Etherpad (pad)", + Name: "etherpad", + Url: "https://pad.okoyono.de", + }, + { + Description: "Jitsi (Videochat)", + Name: "jitsi", + Url: "https://meet.okoyono.de", + }, + }, + Projects: []string{ + "https://buchdesmonats.okoyono.de/", + "https://comicdesmonats.okoyono.de/", + "https://git.okoyono.de/okoyono/weatherstation", + }, + Sensors: &spaceapi.Sensors{ + TotalMemberCount: []*spaceapi.TotalMemberCountItems{ + { + Description: "We are a small group of people from south Germany, meeting remote on a regular basis.", + Location: "Remote", + Value: 6, + }, + }, + //Temperature: []*spaceapi.TemperatureItems{ + // { + // Location: "@f", + // Name: "weatherstatiøn", + // Description: "Weatherstatiøn located at @f.", + // Value: sensors.TemperatureF(), + // }, + //}, + }, + } + + 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/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..ababb63 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,8 @@ +version: "3" + +services: + spaceapi: + build: . + restart: always + ports: + - "8080:8080" \ No newline at end of file diff --git a/generate-spaceapi-types.sh b/generate-spaceapi-types.sh new file mode 100755 index 0000000..5972705 --- /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 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7755745 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.okoyono.de/oko-intern/okospaceapi + +go 1.21.1 diff --git a/pkg/sensors/wind.go b/pkg/sensors/wind.go new file mode 100644 index 0000000..3274077 --- /dev/null +++ b/pkg/sensors/wind.go @@ -0,0 +1,6 @@ +package sensors + +func TemperatureF() float64 { + // TODO: Fetch from influxdb (or remove influxdb finally!) + return 23.0 +}