Skip to content

Commit

Permalink
add dblogger
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesread committed Jul 16, 2024
1 parent d9be096 commit d56f2c1
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ builds:
goos: [linux]
goarch: [amd64]

- id: bot-dblogger
main: ./cmd/japella-bot-dblogger/main.go
binary: japella-bot-dblogger
env: [CGO_ENABLED=0]
goos: [linux]
goarch: [amd64]



archives:
- builds:
Expand All @@ -63,6 +71,12 @@ dockers:
image_templates:
- ghcr.io/jamesread/japella-adaptor-telegram

- id: bot-dblogger
dockerfile: Dockerfile.japella-bot-dblogger
image_templates:
- ghcr.io/jamesread/japella-bot-dblogger


changelog:
sort: asc
filters:
Expand Down
7 changes: 7 additions & 0 deletions Dockerfile.japella-bot-dblogger
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM registry.fedoraproject.org/fedora-minimal:38-x86_64

LABEL org.opencontainers.image.source https://github.com/jamesread/japella

ADD japella-bot-dblogger /

ENTRYPOINT [ "/japella-bot-dblogger" ]
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ default:
go build -o japella-adaptor-telegram github.com/jamesread/japella/cmd/japella-adaptor-telegram/
go build -o japella-bot-utils github.com/jamesread/japella/cmd/japella-bot-utils/
go build -o japella-bot-watcher-prometheus github.com/jamesread/japella/cmd/japella-bot-watcher-prometheus/
go build -o japella-bot-dblogger github.com/jamesread/japella/cmd/japella-bot-dblogger/

grpc: go-tools
buf generate
Expand Down
78 changes: 78 additions & 0 deletions cmd/japella-bot-dblogger/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"database/sql"
"fmt"

_ "github.com/go-sql-driver/mysql"

"github.com/jamesread/japella/internal/amqp"
log "github.com/sirupsen/logrus"
"github.com/jamesread/japella/internal/runtimeconfig"
pb "github.com/jamesread/japella/gen/protobuf"
)

type DatabaseConfig struct {
Host string
User string
Password string
Database string
}

type LocalConfig struct {
Common *runtimeconfig.CommonConfig
Database *DatabaseConfig
}

func main() {
log.Infof("japella-bot-dblogger")

cfg := &LocalConfig{}
cfg.Common = runtimeconfig.LoadNewConfigCommon()
cfg.Database = &DatabaseConfig{}

runtimeconfig.LoadConfig("config.database.yaml", cfg.Database)

log.Infof("cfg: %+v", cfg)

db := ConnectDatabase(cfg)
ListenForMessages(db)
}

func ListenForMessages(db *sql.DB) {
_, handler := amqp.ConsumeForever("IncommingMessage", func(d amqp.Delivery) {
msg := &pb.IncommingMessage{}

amqp.Decode(d.Message.Body, &msg)

log.Infof("recv: %+v", msg)

handleMessage(db, msg)
})

handler.Wait()
log.Infof("done")
}

func ConnectDatabase(cfg *LocalConfig) *sql.DB {
url := fmt.Sprintf("%v:%v@tcp(%v)/%v?parseTime=true", cfg.Database.User, cfg.Database.Password, cfg.Database.Host, cfg.Database.Database)

db, err := sql.Open("mysql", url)

if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}

return db
}

func handleMessage(db *sql.DB, msg *pb.IncommingMessage) {
log.Infof("handleMessage: %+v", msg)

_, err := db.Exec("INSERT INTO messages (channel, author, content) VALUES (?, ?, ?)", msg.Channel, msg.Author, msg.Content)

if err != nil {
log.Errorf("err: %v", err)
}
}

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
buf.build/gen/go/bufbuild/registry/protocolbuffers/go v1.31.0-20231111212044-1119bf4b707e.2 // indirect
connectrpc.com/connect v1.12.0 // indirect
connectrpc.com/otelconnect v0.6.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
Expand All @@ -43,6 +44,7 @@ require (
github.com/go-chi/chi/v5 v5.0.10 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/go-toolsmith/astcast v1.1.0 // indirect
github.com/go-toolsmith/astcopy v1.1.0 // indirect
github.com/go-toolsmith/astequal v1.1.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ connectrpc.com/connect v1.12.0 h1:HwKdOY0lGhhoHdsza+hW55aqHEC64pYpObRNoAgn70g=
connectrpc.com/connect v1.12.0/go.mod h1:3AGaO6RRGMx5IKFfqbe3hvK1NqLosFNP2BxDYTPmNPo=
connectrpc.com/otelconnect v0.6.0 h1:VJAdQL9+sgdUw9+7+J+jq8pQo/h1S7tSFv2+vDcR7bU=
connectrpc.com/otelconnect v0.6.0/go.mod h1:jdcs0uiwXQVmSMgTJ2dAaWR5VbpNd7QKNkuoH7n86RA=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
Expand Down Expand Up @@ -64,6 +66,8 @@ github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/go-toolsmith/astcast v1.1.0 h1:+JN9xZV1A+Re+95pgnMgDboWNVnIMMQXwfBwLRPgSC8=
Expand Down

0 comments on commit d56f2c1

Please sign in to comment.