From fcef2c479ff2b66a56e378d962ac9745015a0117 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Tue, 9 Jan 2024 05:09:01 +0100 Subject: [PATCH] bot connects to server --- .gitignore | 1 + Dockerfile | 23 +++++++++++++++++++++++ Makefile | 3 +++ docker-compose.yml | 8 ++++++++ go.mod | 3 ++- go.sum | 6 ++++-- main.go | 27 +++++++++++++++------------ 7 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.gitignore b/.gitignore index 0d4b06e..9e805d9 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ bin/ go.work .env +nourybot-matrix \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..009d8b5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +# Start from golang base image +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 . . + +# 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 the executable +CMD [ "./nourybot-matrix" ] + diff --git a/Makefile b/Makefile index e69de29..839de2d 100644 --- a/Makefile +++ b/Makefile @@ -0,0 +1,3 @@ +rebuild: + docker compose down + docker-compose up --force-recreate --no-deps --build nourybot-matrix \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0fcb6af --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +version: "3.7" + +services: + nourybot-matrix: + build: + context: . + dockerfile: Dockerfile + env_file: .env diff --git a/go.mod b/go.mod index c3fb194..1509cdc 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,8 @@ go 1.20 require ( github.com/chzyer/readline v1.5.1 - github.com/mattn/go-sqlite3 v1.14.18 + github.com/joho/godotenv v1.5.1 + github.com/mattn/go-sqlite3 v1.14.19 github.com/rs/zerolog v1.31.0 maunium.net/go/mautrix v0.16.3-0.20231215142331-753cdb2e1cb0 ) diff --git a/go.sum b/go.sum index c292f26..99be258 100644 --- a/go.sum +++ b/go.sum @@ -8,13 +8,15 @@ github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38 github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI= -github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI= +github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= diff --git a/main.go b/main.go index aaa94a3..085a995 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "time" "github.com/chzyer/readline" + "github.com/joho/godotenv" _ "github.com/mattn/go-sqlite3" "github.com/rs/zerolog" @@ -27,21 +28,23 @@ import ( "maunium.net/go/mautrix/id" ) -var homeserver = flag.String("homeserver", "", "Matrix homeserver") -var username = flag.String("username", "", "Matrix username localpart") -var password = flag.String("password", "", "Matrix password") -var database = flag.String("database", "mautrix-example.db", "SQLite database path") var debug = flag.Bool("debug", false, "Enable debug logs") +//var database = flag.String("database", "test.db", "SQLite database path") + func main() { flag.Parse() - if *username == "" || *password == "" || *homeserver == "" { - _, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) - flag.PrintDefaults() - os.Exit(1) + err := godotenv.Load() + if err != nil { + panic(err) } - client, err := mautrix.NewClient(*homeserver, "", "") + 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 { panic(err) } @@ -91,7 +94,7 @@ func main() { } }) - cryptoHelper, err := cryptohelper.NewCryptoHelper(client, []byte("meow"), *database) + cryptoHelper, err := cryptohelper.NewCryptoHelper(client, []byte("meow"), database) if err != nil { panic(err) } @@ -103,8 +106,8 @@ func main() { // You don't need to set a device ID in LoginAs because the crypto helper will set it for you if necessary. cryptoHelper.LoginAs = &mautrix.ReqLogin{ Type: mautrix.AuthTypePassword, - Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: *username}, - Password: *password, + Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: username}, + Password: password, } // If you want to use multiple clients with the same DB, you should set a distinct database account ID for each one. //cryptoHelper.DBAccountID = ""