From f4b752cc04f11f712de43243f02dacf5070dd9d3 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Thu, 11 Jan 2024 00:19:55 +0100 Subject: [PATCH] trap os signal to interrupt with ctrl+c --- Dockerfile | 10 ++++------ Makefile | 5 ++++- cmd/bot/main.go | 13 +++++++++++-- docker-compose.yml | 3 ++- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 009d8b5..bcc33af 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,21 +3,19 @@ FROM golang:alpine3.19 RUN apk add --no-cache git ca-certificates build-base su-exec olm-dev sqlite - # Setup folders RUN mkdir /app WORKDIR /app # Copy the source from the current directory to the working Directory inside the container COPY . . +#COPY .env . +COPY ./db/nourybot.db . # Download all the dependencies RUN go get -d -v ./... -RUN go get -u maunium.net/go/mautrix - -# Build the Go app -RUN go build . +Run go build -o "Nourybot.out" ./cmd/bot # Run the executable -CMD [ "./nourybot-matrix" ] +CMD [ "./Nourybot.out", "--database", "nourybot.db" ] diff --git a/Makefile b/Makefile index a5c18e1..2eea27e 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,10 @@ BINARY_NAME=Nourybot-Matrix.out dev: go build -o ${BINARY_NAME} cmd/bot/* - ./${BINARY_NAME} + ./${BINARY_NAME} --database "./db/nourybot.db" + +build: + go build -o ${BINARY_NAME} cmd/bot/* rebuild: docker compose down diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 52dc2a1..8ab4e29 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -14,7 +14,9 @@ import ( "flag" "fmt" "os" + "os/signal" "sync" + "syscall" "time" "github.com/chzyer/readline" @@ -29,6 +31,7 @@ import ( ) var debug = flag.Bool("debug", false, "Enable debug logs") +var database = flag.String("database", "./db/nourybot.db", "SQLite database path") //var database = flag.String("database", "test.db", "SQLite database path") @@ -47,7 +50,6 @@ func main() { homeserver := os.Getenv("MATRIX_HOMESERVER") username := os.Getenv("MATRIX_USERNAME") password := os.Getenv("MATRIX_PASSWORD") - database := os.Getenv("SQLITE_DATABASE") client, err := mautrix.NewClient(homeserver, "", "") if err != nil { @@ -113,7 +115,7 @@ func main() { }) - cryptoHelper, err := cryptohelper.NewCryptoHelper(app.MatrixClient, []byte("meow"), database) + cryptoHelper, err := cryptohelper.NewCryptoHelper(app.MatrixClient, []byte("meow"), *database) if err != nil { panic(err) } @@ -137,6 +139,13 @@ func main() { // Set the client crypto helper in order to automatically encrypt outgoing messages app.MatrixClient.Crypto = cryptoHelper + c := make(chan os.Signal) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + <-c + os.Exit(1) + }() + log.Info().Msg("Now running") syncCtx, cancelSync := context.WithCancel(context.Background()) var syncStopWait sync.WaitGroup diff --git a/docker-compose.yml b/docker-compose.yml index 0fcb6af..7d7d6fd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,4 +5,5 @@ services: build: context: . dockerfile: Dockerfile - env_file: .env + volumes: + - ./.env:/.env