Fixed logs and added docker-compose

This commit is contained in:
nefrace 2021-12-07 01:18:44 +03:00
parent 50d3f93465
commit 079b255d09
5 changed files with 49 additions and 14 deletions

View File

@ -3,5 +3,6 @@ WORKDIR /src
COPY . .
RUN go build -o /out/kicker
FROM scratch AS bin
FROM alpine:3.14 AS bin
COPY --from=build /out/kicker /
CMD "/kicker"

View File

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

29
docker-compose.yml Normal file
View File

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

View File

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

View File

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