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
# 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" ]

View file

@ -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

View file

@ -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

View file

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