From b0a4772dee5ac21f0b78f984761c0464371eb33b Mon Sep 17 00:00:00 2001 From: Corey Date: Sun, 7 Jul 2019 12:38:36 +0100 Subject: [PATCH] Dockerized osrs.cx bot; includes Dockerfile and docker-compose.yml --- Dockerfile | 23 +++++++++++++++++++++++ docker-compose.yml | 16 ++++++++++++++++ main.go | 4 ++++ util/store.go | 21 +++++++++++++++++++-- 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4ed6b39 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +# builder +FROM golang:1.12 as builder + +RUN mkdir /osrscx +WORKDIR /osrscx + +COPY go.mod . +COPY go.sum . +RUN go mod download + +COPY . . + +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o /go/bin/osrscx + +# runner +FROM alpine:latest + +LABEL maintainer="Corey (notmeta) " + +RUN apk --no-cache add ca-certificates bash +COPY --from=builder /go/bin/osrscx /go/bin/osrscx + +ENTRYPOINT ["/go/bin/osrscx"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b9d71ee --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +version: "3.7" +services: + redis_db: + image: redis + command: redis-server --appendonly yes + ports: + - "127.0.0.1:6379:6379" + volumes: + - /docker/redis-data:/data + bot: + build: . + links: + - redis_db + environment: + # - DG_TOKEN= + - REDIS_IP=redis_db \ No newline at end of file diff --git a/main.go b/main.go index 07c0ab6..bd0d5bd 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,10 @@ func init() { if Session.Token == "" { flag.StringVar(&Session.Token, "t", "", "Discord Authentication Token") } + util.RedisIp = os.Getenv("REDIS_IP") + if util.RedisIp == "" { + flag.StringVar(&util.RedisIp, "redis", "localhost", "Redis Store IP Address") + } } func main() { diff --git a/util/store.go b/util/store.go index 0d0b719..513a231 100644 --- a/util/store.go +++ b/util/store.go @@ -5,19 +5,36 @@ import ( "github.com/bwmarrin/discordgo" "github.com/go-redis/redis" "log" + "time" ) var Store *redis.Client +var RedisIp string const RsnKeyFormat = "rsn:%s" func StoreInit() { Store = redis.NewClient(&redis.Options{ - Addr: "localhost:6379", + Addr: RedisIp + ":6379", Password: "", DB: 0, }) - _, err := Store.Ping().Result() + + var err error + pingAttempts := 0 + + for pingAttempts < 5 { + _, err = Store.Ping().Result() + + if err != nil { + log.Printf("Ping failed, waiting and trying again") + pingAttempts += 1 + time.Sleep(5 * time.Second) + } else { + break + } + } + if err != nil { log.Fatalf("Failed to connect to local redis container!\n%s", err) } else {