From 4f4e44afb1f922ca16063ff2cbd2f0573847b54d Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Sat, 7 Sep 2024 01:49:06 -0400 Subject: Containerization --- Dockerfile | 13 +++++++++++++ Makefile | 12 ++++++++++++ cmd/songlinkr/main.go | 16 ++++++++++------ docker-compose.yml | 8 ++++++++ songlinkr | Bin 0 -> 12294699 bytes 5 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 docker-compose.yml create mode 100755 songlinkr 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 Binary files /dev/null and b/songlinkr differ -- cgit v1.2.3