Skip to content

Commit fa11c64

Browse files
authored
Merge pull request #8 from KittyBot-Org/db-refactor
Db refactor
2 parents d814b9c + 54ec76b commit fa11c64

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1139
-742
lines changed

.github/workflows/docker-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
types: [published]
66

77
env:
8-
BOT_IMAGE_NAME: dbot
8+
BOT_IMAGE_NAME: bot
99
BACKEND_IMAGE_NAME: backend
1010

1111
jobs:
@@ -15,7 +15,7 @@ jobs:
1515
- uses: actions/checkout@v2
1616

1717
- name: Build image
18-
run: docker build . --build-arg VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') --file cmd/dbot/Dockerfile --tag BOT_IMAGE_NAME
18+
run: docker build . --build-arg VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') --file cmd/bot/Dockerfile --tag BOT_IMAGE_NAME
1919

2020
- name: Log into registry
2121
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin

.github/workflows/go-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ jobs:
2626
restore-keys: |
2727
${{ runner.os }}-go-
2828
29-
- name: dbot go build
30-
run: go build cmd/dbot/main.go
29+
- name: bot go build
30+
run: go build cmd/bot/main.go
3131

3232
- name: backend go build
3333
run: go build cmd/backend/main.go

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ Optional CLI Flags:
1818
- `--exit-after-sync=true`: Exit after db & commands sync.
1919

2020
```bash
21-
$ jet -dsn=postgresql://user:pass@localhost:5432/jetdb -schema=dvds -path=./internal/db/.gen
21+
$ go install github.com/go-jet/jet/v2/cmd/jet@latest
22+
```
23+
24+
```bash
25+
$ jet -dsn=postgresql://user:pass@localhost:5432/jetdb?sslmode=disable -schema=public -path=./internal/db/.gen
2226
```

cmd/backend/main.go

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package main
22

33
import (
44
"flag"
5-
"github.com/KittyBot-Org/KittyBotGo/internal/backend"
6-
"github.com/KittyBot-Org/KittyBotGo/internal/modules"
7-
routes2 "github.com/KittyBot-Org/KittyBotGo/internal/routes"
85
"os"
96
"os/signal"
107
"syscall"
118

9+
"github.com/KittyBot-Org/KittyBotGo/internal/backend"
1210
"github.com/KittyBot-Org/KittyBotGo/internal/config"
1311
"github.com/KittyBot-Org/KittyBotGo/internal/db"
12+
"github.com/KittyBot-Org/KittyBotGo/internal/modules"
13+
"github.com/KittyBot-Org/KittyBotGo/internal/routes"
1414
"github.com/disgoorg/log"
15+
_ "github.com/lib/pq"
1516
)
1617

1718
var (
@@ -29,41 +30,45 @@ func init() {
2930
func main() {
3031
var err error
3132
logger := log.New(log.Ldate | log.Ltime | log.Lshortfile)
32-
backend := &backend.Backend{
33-
Logger: logger,
34-
Version: version,
33+
34+
logger.Infof("Starting b version: %s", version)
35+
logger.Infof("Syncing DB tables? %v", *shouldSyncDBTables)
36+
logger.Infof("Exiting after syncing? %v", *exitAfterSync)
37+
38+
var cfg backend.Config
39+
if err = config.LoadConfig(&cfg); err != nil {
40+
logger.Fatal("Failed to load config: ", err)
3541
}
36-
backend.Logger.Infof("Starting backend version: %s", version)
37-
backend.Logger.Infof("Syncing DB tables? %v", *shouldSyncDBTables)
38-
backend.Logger.Infof("Exiting after syncing? %v", *exitAfterSync)
42+
logger.SetLevel(cfg.LogLevel)
3943

40-
if err = config.LoadConfig(&backend.Config); err != nil {
41-
backend.Logger.Fatal("Failed to load config: ", err)
44+
b := &backend.Backend{
45+
Logger: logger,
46+
Config: cfg,
47+
Version: version,
4248
}
43-
logger.SetLevel(backend.Config.LogLevel)
4449

45-
if backend.DB, err = db.SetupDatabase(backend.Config.Database, *shouldSyncDBTables, backend.Config.DevMode); err != nil {
46-
backend.Logger.Fatal("Failed to setup database: ", err)
50+
if b.DB, err = db.SetupDatabase(b.Config.Database); err != nil {
51+
b.Logger.Fatal("Failed to setup database: ", err)
4752
}
48-
defer backend.DB.Close()
53+
defer b.DB.Close()
4954

5055
if *exitAfterSync {
51-
backend.Logger.Infof("Exiting after syncing database tables")
56+
b.Logger.Infof("Exiting after syncing database tables")
5257
os.Exit(0)
5358
}
5459

55-
backend.LoadCommands(modules.Modules)
56-
backend.SetupRestServices()
57-
if err = backend.SetupPrometheusAPI(); err != nil {
58-
backend.Logger.Fatal("Failed to setup prometheus api: ", err)
60+
b.LoadCommands(modules.Modules)
61+
b.SetupRestServices()
62+
if err = b.SetupPrometheusAPI(); err != nil {
63+
b.Logger.Fatal("Failed to setup prometheus api: ", err)
5964
}
60-
if err = backend.SetupScheduler(); err != nil {
61-
backend.Logger.Fatal("Failed to setup scheduler: ", err)
65+
if err = b.SetupScheduler(); err != nil {
66+
b.Logger.Fatal("Failed to setup scheduler: ", err)
6267
}
63-
defer backend.Scheduler.Shutdown()
64-
backend.SetupServer(routes2.Handler(backend))
68+
defer b.Scheduler.Shutdown()
69+
b.SetupServer(routes.Handler(b))
6570

66-
backend.Logger.Info("Backend is running. Press CTRL-C to exit.")
71+
b.Logger.Info("Backend is running. Press CTRL-C to exit.")
6772
s := make(chan os.Signal, 1)
6873
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
6974
<-s

cmd/bot/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ COPY . .
88

99
RUN go mod download && \
1010
go mod verify && \
11-
go build -ldflags="-X 'main.version=${VERSION}'" -o dbot cmd/dbot/main.go
11+
go build -ldflags="-X 'main.version=${VERSION}'" -o bot cmd/bot/main.go
1212

1313
FROM alpine
1414

cmd/bot/main.go

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ package main
33
import (
44
"context"
55
"flag"
6-
"github.com/KittyBot-Org/KittyBotGo/internal/dbot"
7-
"github.com/KittyBot-Org/KittyBotGo/internal/i18n"
8-
"github.com/KittyBot-Org/KittyBotGo/internal/metrics"
9-
"github.com/KittyBot-Org/KittyBotGo/internal/modules"
106
"os"
117
"os/signal"
128
"syscall"
139

1410
"github.com/KittyBot-Org/KittyBotGo/internal/config"
1511
"github.com/KittyBot-Org/KittyBotGo/internal/db"
12+
"github.com/KittyBot-Org/KittyBotGo/internal/i18n"
13+
"github.com/KittyBot-Org/KittyBotGo/internal/kbot"
14+
"github.com/KittyBot-Org/KittyBotGo/internal/metrics"
15+
"github.com/KittyBot-Org/KittyBotGo/internal/modules"
1616
"github.com/disgoorg/log"
17+
_ "github.com/lib/pq"
1718
)
1819

1920
var (
@@ -31,61 +32,63 @@ func init() {
3132
}
3233

3334
func main() {
34-
var err error
3535
logger := log.New(log.Ldate | log.Ltime | log.Lshortfile)
36+
logger.Info("Starting discord kbot version: ", version)
37+
38+
var cfg kbot.Config
39+
if err := config.LoadConfig(&cfg); err != nil {
40+
logger.Fatal("Failed to load config: ", err)
41+
}
42+
logger.SetLevel(cfg.LogLevel)
3643

37-
bot := &dbot.Bot{
44+
logger.Info("Syncing commands? ", *shouldSyncCommands)
45+
logger.Info("Syncing DB tables? ", *shouldSyncDBTables)
46+
logger.Info("Exiting after syncing? ", *exitAfterSync)
47+
defer logger.Info("Shutting down discord kbot...")
48+
49+
b := &kbot.Bot{
3850
Logger: logger,
51+
Config: cfg,
3952
Version: version,
4053
}
41-
bot.Logger.Infof("Starting dbot version: %s", version)
42-
bot.Logger.Infof("Syncing commands? %v", *shouldSyncCommands)
43-
bot.Logger.Infof("Syncing DB tables? %v", *shouldSyncDBTables)
44-
bot.Logger.Infof("Exiting after syncing? %v", *exitAfterSync)
45-
defer bot.Logger.Info("Shutting down dbot...")
46-
47-
if err = config.LoadConfig(&bot.Config); err != nil {
48-
bot.Logger.Fatal("Failed to load config: ", err)
49-
}
50-
logger.SetLevel(bot.Config.LogLevel)
5154

52-
if err = i18n.Setup(bot); err != nil {
53-
bot.Logger.Fatal("Failed to setup i18n: ", err)
55+
if err := i18n.Setup(b); err != nil {
56+
b.Logger.Fatal("Failed to setup i18n: ", err)
5457
}
5558

56-
bot.LoadModules(modules.Modules)
57-
bot.SetupPaginator()
59+
b.LoadModules(modules.Modules)
60+
b.SetupPaginator()
5861

59-
if err = bot.SetupBot(); err != nil {
60-
bot.Logger.Fatal("Failed to setup dbot: ", err)
62+
if err := b.SetupBot(); err != nil {
63+
b.Logger.Fatal("Failed to setup discord kbot: ", err)
6164
}
62-
defer bot.Client.Close(context.TODO())
65+
defer b.Client.Close(context.TODO())
6366

6467
if *shouldSyncCommands {
65-
bot.SyncCommands()
68+
b.SyncCommands()
6669
}
6770

68-
if bot.DB, err = db.SetupDatabase(bot.Config.Database, *shouldSyncDBTables, bot.Config.DevMode); err != nil {
69-
bot.Logger.Fatal("Failed to setup database: ", err)
71+
var err error
72+
if b.DB, err = db.SetupDatabase(b.Config.Database); err != nil {
73+
b.Logger.Fatal("Failed to setup database: ", err)
7074
}
71-
defer bot.DB.Close()
75+
defer b.DB.Close()
7276

7377
if *exitAfterSync {
74-
bot.Logger.Infof("Exiting after syncing commands and database tables")
78+
b.Logger.Infof("Exiting after syncing commands and database tables")
7579
os.Exit(0)
7680
}
7781

78-
metrics.Setup(bot)
82+
metrics.Setup(b)
7983

80-
bot.SetupLavalink()
81-
defer bot.Lavalink.Close()
82-
defer bot.SavePlayers()
84+
b.SetupLavalink()
85+
defer b.Lavalink.Close()
8386

84-
if err = bot.StartBot(); err != nil {
85-
bot.Logger.Fatal("Failed to start dbot: ", err)
87+
if err = b.StartBot(); err != nil {
88+
b.Logger.Fatal("Failed to start discord kbot: ", err)
8689
}
8790

88-
bot.Logger.Info("Bot is running. Press CTRL-C to exit.")
91+
b.Logger.Info("Bot is running. Press CTRL-C to exit.")
8992
s := make(chan os.Signal, 1)
9093
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
9194
<-s

docker/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
bot:
3-
# image: docker.pkg.github.com/kittybot-org/kittybotgo/dbot:latest
3+
# image: docker.pkg.github.com/kittybot-org/kittybotgo/kbot:latest
44
build:
55
context: ..
66
dockerfile: ./cmd/bot/Dockerfile

docker/prometheus.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ scrape_configs:
99

1010
- job_name: kittybot
1111
static_configs:
12-
- targets: ['dbot:2112']
12+
- targets: ['kbot:2112']

go.mod

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,42 @@ module github.com/KittyBot-Org/KittyBotGo
33
go 1.18
44

55
require (
6-
cloud.google.com/go/pubsub v1.3.1
7-
github.com/disgoorg/disgo v0.8.8
8-
github.com/disgoorg/disgolink/disgolink v1.5.1
9-
github.com/disgoorg/disgolink/lavalink v1.5.1
6+
github.com/disgoorg/disgo v0.8.9
7+
github.com/disgoorg/disgolink/disgolink v1.5.3
8+
github.com/disgoorg/disgolink/lavalink v1.5.3
109
github.com/disgoorg/log v1.2.0
1110
github.com/disgoorg/snowflake v1.1.0
11+
github.com/disgoorg/source-extensions-plugin v1.2.1
1212
github.com/disgoorg/utils/paginator v0.0.0-20220324002608-4f4098bd27c9
13+
github.com/go-jet/jet/v2 v2.7.1
1314
github.com/gorilla/mux v1.8.0
15+
github.com/lib/pq v1.7.0
1416
github.com/lithammer/fuzzysearch v1.1.3
1517
github.com/procyon-projects/chrono v1.0.0
1618
github.com/prometheus/client_golang v1.12.1
1719
github.com/prometheus/common v0.32.1
18-
github.com/uptrace/bun v1.1.3
19-
github.com/uptrace/bun/driver/pgdriver v1.1.3
2020
golang.org/x/text v0.3.7
2121
)
2222

2323
require (
24-
cloud.google.com/go v0.65.0 // indirect
2524
github.com/beorn7/perks v1.0.1 // indirect
2625
github.com/cespare/xxhash/v2 v2.1.2 // indirect
27-
github.com/go-jet/jet/v2 v2.7.1 // indirect
28-
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
26+
github.com/davecgh/go-spew v1.1.1 // indirect
2927
github.com/golang/protobuf v1.5.2 // indirect
30-
github.com/google/go-cmp v0.5.5 // indirect
31-
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
28+
github.com/google/uuid v1.1.1 // indirect
3229
github.com/gorilla/websocket v1.5.0 // indirect
33-
github.com/jinzhu/inflection v1.0.0 // indirect
3430
github.com/json-iterator/go v1.1.12 // indirect
35-
github.com/jstemmer/go-junit-report v0.9.1 // indirect
3631
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
3732
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3833
github.com/modern-go/reflect2 v1.0.2 // indirect
34+
github.com/pmezard/go-difflib v1.0.0 // indirect
3935
github.com/prometheus/client_model v0.2.0 // indirect
4036
github.com/prometheus/procfs v0.7.3 // indirect
4137
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b // indirect
42-
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
43-
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
44-
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
45-
go.opencensus.io v0.22.4 // indirect
46-
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
38+
github.com/stretchr/testify v1.7.0 // indirect
4739
golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8 // indirect
48-
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
49-
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57 // indirect
5040
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
51-
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
52-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
5341
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886 // indirect
54-
golang.org/x/tools v0.1.8-0.20211029000441-d6a9af8af023 // indirect
55-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
56-
google.golang.org/api v0.30.0 // indirect
57-
google.golang.org/appengine v1.6.6 // indirect
58-
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect
59-
google.golang.org/grpc v1.31.0 // indirect
6042
google.golang.org/protobuf v1.26.0 // indirect
61-
mellium.im/sasl v0.2.1 // indirect
43+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
6244
)

0 commit comments

Comments
 (0)