From ae00b84dbfd2bd7d0eb2c4eef7754b8bacc1be8c Mon Sep 17 00:00:00 2001 From: jadc Date: Mon, 10 Mar 2025 17:52:24 -0600 Subject: [PATCH 1/3] Added Docker as a hosting method --- Dockerfile | 26 ++++++++++++++++++++++++++ README.md | 20 +++++++++++++++++++- docker-compose.yml | 9 +++++++++ src/database/main.go | 4 ++-- 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ae48f7b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM golang:latest AS builder + +# Set build directory +WORKDIR /build + +# Copy Go dependency files +COPY src/go.* . + +# Download Go modules +RUN go mod download + +# Copy the rest of the source code +COPY src/* . + +# Build app +RUN go build -v -o redpin + + + +FROM alpine:latest + +# Copy compiled binary from previous stage +COPY --from=builder /build/redpin . + +# Run redpin +CMD ["./redpin"] diff --git a/README.md b/README.md index 26f29b7..0a8fb6d 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,24 @@ TODO This repository contains the modern rewrite and expansion of **redpin** in the Go. If you are looking for the original Python version, see [redpin-py](https://github.com/jadc/redpin-py); I rewrote this bot to practice interfacing with databases and APIs, routing packets, responding to asynchronous events, and generally extend the features of this bot significantly past its current form and existing (free and paid) offerings. +## Installation +The recommended way of hosting is using [Docker](https://www.docker.com/). +1. Clone this repository. + ```sh + git clone https://github.com/jadc/redpin.git + ``` +2. Create an environment file named `.env` with the following contents. + ``` + DISCORD_TOKEN= + DB_FILE=[optional, path to database file] + ``` +3. Run the container using Docker Compose. + ```sh + docker compose up + ``` + +Of course, you can host it without Docker. If you want to do it that way, you probably know how. :) + ## TODO - [x] Implement core functionality @@ -29,6 +47,6 @@ This repository contains the modern rewrite and expansion of **redpin** in the G - [x] Leaderboard for users with most pins (and emojis used) - [ ] Write tests - [x] Write CI/CD pipeline for automatic building and running tests -- [ ] Write Dockerfile for production deployment +- [x] Write Dockerfile for production deployment - [x] Write Nix Shell for development environment - [ ] Showcase bot in [Tour](#Tour) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..32b5653 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +services: + redpin: + container_name: redpin + restart: unless-stopped + env_file: .env + build: + context: . + volumes: + - ./data.db:/data.db diff --git a/src/database/main.go b/src/database/main.go index d2bd5b5..48168ec 100644 --- a/src/database/main.go +++ b/src/database/main.go @@ -25,8 +25,8 @@ func Connect() (*database, error) { once.Do(func(){ // Retrieve file name from the environment database_url := "data.db" - if x := os.Getenv("SQLITE_FILE_NAME"); x != "" { - log.Println("Using environmental variable 'SQLITE_FILE_NAME' for database name.") + if x := os.Getenv("DB_FILE"); x != "" { + log.Println("Using environmental variable 'DB_FILE' for database name.") database_url = x } From 08d85b8b9ceb886afe079bb182ba91cdd751c429 Mon Sep 17 00:00:00 2001 From: jadc Date: Mon, 10 Mar 2025 17:55:23 -0600 Subject: [PATCH 2/3] formatting --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 0a8fb6d..fe179c3 100644 --- a/README.md +++ b/README.md @@ -15,15 +15,18 @@ This repository contains the modern rewrite and expansion of **redpin** in the G ## Installation The recommended way of hosting is using [Docker](https://www.docker.com/). 1. Clone this repository. + ```sh git clone https://github.com/jadc/redpin.git ``` 2. Create an environment file named `.env` with the following contents. + ``` DISCORD_TOKEN= DB_FILE=[optional, path to database file] ``` 3. Run the container using Docker Compose. + ```sh docker compose up ``` From b5566106ae554d7443e6f82be6588462d1389908 Mon Sep 17 00:00:00 2001 From: jadc Date: Tue, 11 Mar 2025 00:32:30 +0000 Subject: [PATCH 3/3] Switched Dockerfile to use scratch image --- Dockerfile | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index ae48f7b..44b0a35 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,20 +6,23 @@ WORKDIR /build # Copy Go dependency files COPY src/go.* . -# Download Go modules +# Download and verify Go modules RUN go mod download +RUN go mod verify # Copy the rest of the source code -COPY src/* . +COPY src/ . # Build app -RUN go build -v -o redpin +RUN CGO_ENABLED=0 GOOS=linux go build -v -o redpin -FROM alpine:latest +FROM scratch -# Copy compiled binary from previous stage +# Copy necessary files from builder +COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=builder /build/redpin . # Run redpin