trap os signal to interrupt with ctrl+c

This commit is contained in:
lyx0 2024-01-11 00:19:55 +01:00
parent 07c59b5811
commit f4b752cc04
4 changed files with 21 additions and 10 deletions

View file

@ -3,21 +3,19 @@ FROM golang:alpine3.19
RUN apk add --no-cache git ca-certificates build-base su-exec olm-dev sqlite RUN apk add --no-cache git ca-certificates build-base su-exec olm-dev sqlite
# Setup folders # Setup folders
RUN mkdir /app RUN mkdir /app
WORKDIR /app WORKDIR /app
# Copy the source from the current directory to the working Directory inside the container # Copy the source from the current directory to the working Directory inside the container
COPY . . COPY . .
#COPY .env .
COPY ./db/nourybot.db .
# Download all the dependencies # Download all the dependencies
RUN go get -d -v ./... RUN go get -d -v ./...
RUN go get -u maunium.net/go/mautrix Run go build -o "Nourybot.out" ./cmd/bot
# Build the Go app
RUN go build .
# Run the executable # Run the executable
CMD [ "./nourybot-matrix" ] CMD [ "./Nourybot.out", "--database", "nourybot.db" ]

View file

@ -2,7 +2,10 @@ BINARY_NAME=Nourybot-Matrix.out
dev: dev:
go build -o ${BINARY_NAME} cmd/bot/* go build -o ${BINARY_NAME} cmd/bot/*
./${BINARY_NAME} ./${BINARY_NAME} --database "./db/nourybot.db"
build:
go build -o ${BINARY_NAME} cmd/bot/*
rebuild: rebuild:
docker compose down docker compose down

View file

@ -14,7 +14,9 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"os/signal"
"sync" "sync"
"syscall"
"time" "time"
"github.com/chzyer/readline" "github.com/chzyer/readline"
@ -29,6 +31,7 @@ import (
) )
var debug = flag.Bool("debug", false, "Enable debug logs") 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") //var database = flag.String("database", "test.db", "SQLite database path")
@ -47,7 +50,6 @@ func main() {
homeserver := os.Getenv("MATRIX_HOMESERVER") homeserver := os.Getenv("MATRIX_HOMESERVER")
username := os.Getenv("MATRIX_USERNAME") username := os.Getenv("MATRIX_USERNAME")
password := os.Getenv("MATRIX_PASSWORD") password := os.Getenv("MATRIX_PASSWORD")
database := os.Getenv("SQLITE_DATABASE")
client, err := mautrix.NewClient(homeserver, "", "") client, err := mautrix.NewClient(homeserver, "", "")
if err != nil { 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 { if err != nil {
panic(err) panic(err)
} }
@ -137,6 +139,13 @@ func main() {
// Set the client crypto helper in order to automatically encrypt outgoing messages // Set the client crypto helper in order to automatically encrypt outgoing messages
app.MatrixClient.Crypto = cryptoHelper 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") log.Info().Msg("Now running")
syncCtx, cancelSync := context.WithCancel(context.Background()) syncCtx, cancelSync := context.WithCancel(context.Background())
var syncStopWait sync.WaitGroup var syncStopWait sync.WaitGroup

View file

@ -5,4 +5,5 @@ services:
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
env_file: .env volumes:
- ./.env:/.env