summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2024-09-07 01:49:06 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2024-09-07 01:49:06 -0400
commit4f4e44afb1f922ca16063ff2cbd2f0573847b54d (patch)
tree4f4670f79040ae25a4c98d4c89a634209a93f879
parentf903439cbc504b9a7b676eb8a1c4e1d4c95a61fc (diff)
Containerization
-rw-r--r--Dockerfile13
-rw-r--r--Makefile12
-rw-r--r--cmd/songlinkr/main.go16
-rw-r--r--docker-compose.yml8
-rwxr-xr-xsonglinkrbin0 -> 12294699 bytes
5 files changed, 43 insertions, 6 deletions
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..a8b048e
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,13 @@
+# Step 1: Setup
+FROM golang:latest
+WORKDIR /app
+
+COPY go.* ./
+COPY cmd cmd
+COPY internal internal
+
+RUN go mod download && go mod verify
+
+RUN go build -o /application cmd/songlinkr/main.go
+
+CMD ["/application"]
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..73191ef
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,12 @@
+DOCKER_IMAGE=songlinkr
+CONTAINER_NAME=songlinkr-container
+
+compile:
+ docker build -t $(DOCKER_IMAGE) .
+
+run:
+ docker run --rm \
+ --name $(CONTAINER_NAME) \
+ -v $(PWD)/secrets:/secrets \
+ -e SECRETS_PATH="/secrets" \
+ $(DOCKER_IMAGE)
diff --git a/cmd/songlinkr/main.go b/cmd/songlinkr/main.go
index f068203..50c53fa 100644
--- a/cmd/songlinkr/main.go
+++ b/cmd/songlinkr/main.go
@@ -1,8 +1,7 @@
package main
import (
- "fmt"
- "log"
+ "log/slog"
"os"
"os/signal"
"path"
@@ -23,7 +22,8 @@ func AppAction(ctx *cli.Context) error {
sess, err := discordgo.New("Bot " + token)
if err != nil {
- log.Fatal(err)
+ slog.Error("Unable to launch slog", "error", err)
+ os.Exit(1)
}
sess.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
@@ -62,7 +62,7 @@ func AppAction(ctx *cli.Context) error {
}
defer sess.Close()
- fmt.Println("Bot is now running. Press CTRL-C to exit.")
+ slog.Info("Bot is now running")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
@@ -79,6 +79,9 @@ func getSecret(secretsPath, secretName string) (string, error) {
}
func main() {
+ loggr := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{}))
+ slog.SetDefault(loggr)
+
app := &cli.App{
Name: "Songlinkr",
Usage: "A Discord bot that converts song links to Universal Song.link",
@@ -86,11 +89,12 @@ func main() {
Flags: []cli.Flag{
&cli.StringFlag{
Name: "secrets-path",
- EnvVars: []string{"SECRETS_DIR"},
+ EnvVars: []string{"SECRETS_PATH"},
},
},
}
if err := app.Run(os.Args); err != nil {
- log.Fatal(err)
+ slog.Error("An error occured during runtime", "error", err)
+ os.Exit(1)
}
}
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..3221546
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,8 @@
+---
+services:
+ songlinkr:
+ container_name: songlinkr
+ build: .
+ volumes: [./secrets:/secrets]
+ environment: [SECRETS_PATH=/secrets]
+ restart: unless-stopped
diff --git a/songlinkr b/songlinkr
new file mode 100755
index 0000000..2e7359a
--- /dev/null
+++ b/songlinkr
Binary files differ