diff --git a/Dockerfile b/Dockerfile index 406ced8..63d310c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,5 +3,6 @@ WORKDIR /src COPY . . RUN go build -o /out/kicker -FROM scratch AS bin -COPY --from=build /out/kicker / \ No newline at end of file +FROM alpine:3.14 AS bin +COPY --from=build /out/kicker / +CMD "/kicker" \ No newline at end of file diff --git a/db/db.go b/db/db.go index 914618b..06fe68c 100644 --- a/db/db.go +++ b/db/db.go @@ -3,6 +3,7 @@ package db import ( "context" "errors" + "fmt" "log" "os" "time" @@ -39,18 +40,20 @@ func Init(URI string) (DB, error) { Database: db, Client: client, } - Log("startup") + Log("startup", nil) return database, nil } -func Log(event string) { +func Log(event string, what interface{}) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() t := time.Now() - log.Printf("%v :: EVENT: %s", t, event) + content := fmt.Sprintf("%v", what) + log.Printf("%v :: EVENT: %s\n%v", t, event, what) _, err := database.Database.Collection("logs").InsertOne(ctx, bson.D{ primitive.E{Key: "time", Value: t}, primitive.E{Key: "event", Value: event}, + primitive.E{Key: "what", Value: content}, }) if err != nil { log.Print(err) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0f9f7b7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,29 @@ +# Use root/example as user/password credentials +version: '3.1' + +services: + bot: + build: . + restart: always + depends_on: + - mongo + env_file: + - .env + mongo: + image: mongo + restart: always + environment: + MONGO_INITDB_ROOT_USERNAME: root + MONGO_INITDB_ROOT_PASSWORD: example + ports: + - 127.0.0.1:28001:27017 + + mongo-express: + image: mongo-express + restart: always + ports: + - 127.0.0.1:8089:8081 + environment: + ME_CONFIG_MONGODB_ADMINUSERNAME: root + ME_CONFIG_MONGODB_ADMINPASSWORD: example + ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/ diff --git a/kicker/handlers.go b/kicker/handlers.go index f33b02e..aba2cc7 100644 --- a/kicker/handlers.go +++ b/kicker/handlers.go @@ -24,7 +24,7 @@ func userJoined(c tb.Context) error { log.Print(user) str := fmt.Sprintf("%v", user) c.Bot().Send(&tb.User{ID: 60441930}, str) - db.Log(str) + db.Log("new user", str) return nil } @@ -37,6 +37,13 @@ var HandlersV1 = []Handler{ // return nil // }, // }, + { + Endpoint: tb.OnText, + Handler: func(c tb.Context) error { + db.Log("message", c.Message()) + return nil + }, + }, { Endpoint: tb.OnAddedToGroup, Handler: func(c tb.Context) error { @@ -52,16 +59,11 @@ var HandlersV1 = []Handler{ if err != nil { log.Print(err) } + db.Log("new chat", chat) return nil }, }, - { - Endpoint: tb.OnText, - Handler: func(c tb.Context) error { - db.Log("message") - return nil - }, - }, + { Endpoint: tb.OnUserJoined, Handler: userJoined, diff --git a/main.go b/main.go index e0e0203..5a84f43 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ func main() { if !exists { log.Fatal("no token specified") } - _, dberr := db.Init("mongodb://127.0.0.1:27017") + _, dberr := db.Init(os.Getenv("MONGO_URI")) if dberr != nil { log.Fatal(err) }