From 7e6a3f00cb16ff80473a11f8e5fbe67fcdbbd96d Mon Sep 17 00:00:00 2001
From: github-tijlxyz <contact@tijl.xyz>
Date: Fri, 23 Feb 2024 15:20:17 +0100
Subject: [PATCH] docker release and test workflow

---
 .dockerignore                 |  7 +++++++
 .github/workflows/go.yml      | 22 ++++++++++++++++++++++
 .github/workflows/release.yml | 31 +++++++++++++++++++++++++++++++
 Dockerfile                    | 11 +++++++++++
 README.md                     |  8 +++++---
 main.go                       |  3 ++-
 6 files changed, 78 insertions(+), 4 deletions(-)
 create mode 100644 .dockerignore
 create mode 100644 .github/workflows/go.yml
 create mode 100644 Dockerfile

diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..343dee5
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,7 @@
+README.md
+LICENSE
+.git/
+db/
+users.json
+khatru-invite
+
diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml
new file mode 100644
index 0000000..294be5d
--- /dev/null
+++ b/.github/workflows/go.yml
@@ -0,0 +1,22 @@
+name: Test Build
+
+on:
+  push:
+    branches: [ "main" ]
+  pull_request:
+    branches: [ "main" ]
+
+jobs:
+
+  build:
+    runs-on: ubuntu-latest
+    steps:
+    - uses: actions/checkout@v3
+
+    - name: Set up Go
+      uses: actions/setup-go@v4
+      with:
+        go-version: '1.21'
+
+    - name: Build
+      run: go build -v ./...
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index a037293..41d07be 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -8,7 +8,9 @@ permissions:
   contents: write
 
 jobs:
+
   releases-matrix:
+    name: Build and release binary
     runs-on: ubuntu-latest
     strategy:
       matrix:
@@ -28,3 +30,32 @@ jobs:
           md5sum: false
           sha256sum: false
           compress_assets: false
+
+  release-docker:
+    name: Push Docker image to Docker Hub
+    runs-on: ubuntu-latest
+    steps:
+      - name: Check out the repo
+        uses: actions/checkout@v4
+
+      - name: Log in to Docker Hub
+        uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
+        with:
+          username: ${{ secrets.DOCKER_USERNAME }}
+          password: ${{ secrets.DOCKER_PASSWORD }}
+
+      - name: Extract metadata (tags, labels) for Docker
+        id: meta
+        uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
+        with:
+          images: tijlxyz/khatru-pyramid
+
+      - name: Build and push Docker image
+        uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
+        with:
+          context: .
+          file: ./Dockerfile
+          push: true
+          tags: ${{ steps.meta.outputs.tags }}
+          labels: ${{ steps.meta.outputs.labels }}
+
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..22c82c9
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,11 @@
+FROM golang:1.21 AS builder
+WORKDIR /app
+COPY . .
+RUN go build -o khatru-invite .
+
+FROM ubuntu:latest
+COPY --from=builder /app/khatru-invite /app/
+ENV DATABASE_PATH="/app/db"
+ENV USERDATA_PATH="/app/users.json"
+CMD ["/app/khatru-invite"]
+
diff --git a/README.md b/README.md
index 3e9a746..4166274 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,8 @@
 
 A relay based on [Khatru](https://github.com/fiatjaf/khatru) with a invite hierarchy feature.
 
-some notes before running: 
-1. configure the relay settings in `.env`
-2. manually add someone to the `users.json` file, like this: `{"07adfda9c5adc80881bb2a5220f6e3181e0c043b90fa115c4f183464022968e6":""}`
+### Deploy with docker
+
+1. create and manually add a pubkey to users.json: `touch users.json && echo '{"your nostr hex pubkey":""}' > users.json`
+2. deploy with docker: `docker run -p 3334:3334 -v ./users.json:/app/users.json -v ./db:/app/db -e DOMAIN=yourdomain.example.com -e RELAY_NAME="your relay name" -e RELAY_PUBKEY="your nostr hex pubkey" tijlxyz/khatru-pyramid:latest`
+
diff --git a/main.go b/main.go
index 7739540..ed68477 100644
--- a/main.go
+++ b/main.go
@@ -28,7 +28,7 @@ type Settings struct {
 	RelayContact     string `envconfig:"RELAY_CONTACT"`
 	RelayIcon        string `envconfig:"RELAY_ICON"`
 	DatabasePath     string `envconfig:"DATABASE_PATH" default:"./db"`
-    UserdataPath     string `envconfig:"USERDATA_PATH" default:"./users.json"`
+	UserdataPath     string `envconfig:"USERDATA_PATH" default:"./users.json"`
 
 	MaxInvitesPerPerson int `envconfig:"MAX_INVITES_PER_PERSON" default:"3"`
 }
@@ -42,6 +42,7 @@ var (
 )
 
 func main() {
+
 	err := envconfig.Process("", &s)
 	if err != nil {
 		log.Fatal().Err(err).Msg("couldn't process envconfig")