From 6fbe68a0a46de4ef6eecff765e704f398452d98f Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Mon, 8 Aug 2022 23:13:33 +0200 Subject: [PATCH] move config to main file and add database connection --- cmd/bot/main.go | 79 ++++++++++++++++++++++++++++++++++++--- internal/config/config.go | 26 ------------- migrations/README.md | 27 +++++++++++++ pkg/commands/coinflip.go | 3 -- 4 files changed, 100 insertions(+), 35 deletions(-) delete mode 100644 internal/config/config.go create mode 100644 migrations/README.md diff --git a/cmd/bot/main.go b/cmd/bot/main.go index a31d62c..b4e76b1 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -1,29 +1,68 @@ package main import ( + "context" + "database/sql" + "log" + "os" + "time" + "github.com/gempir/go-twitch-irc/v3" - "github.com/lyx0/nourybot/internal/config" + "github.com/joho/godotenv" + _ "github.com/lib/pq" "github.com/lyx0/nourybot/pkg/common" "go.uber.org/zap" ) +type config struct { + twitchUsername string + twitchOauth string + environment string + db struct { + dsn string + maxOpenConns int + maxIdleConns int + maxIdleTime string + } +} + type Application struct { TwitchClient *twitch.Client Logger *zap.SugaredLogger + Db *sql.DB } func main() { - cfg := config.New() + var cfg config - tc := twitch.NewClient(cfg.TwitchUsername, cfg.TwitchOauth) + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env file") + } + + cfg.twitchUsername = os.Getenv("TWITCH_USERNAME") + cfg.twitchOauth = os.Getenv("TWITCH_OAUTH") + cfg.environment = "Development" + cfg.db.dsn = os.Getenv("DB_DSN") + cfg.db.maxOpenConns = 25 + cfg.db.maxIdleConns = 25 + cfg.db.maxIdleTime = "15m" + + tc := twitch.NewClient(cfg.twitchUsername, cfg.twitchOauth) sugar := zap.NewExample().Sugar() defer sugar.Sync() + // Start time common.StartTime() + db, err := openDB(cfg) + if err != nil { + sugar.Fatal(err) + } app := &Application{ TwitchClient: tc, Logger: sugar, + Db: db, } // Received a PrivateMessage (normal chat message). @@ -39,16 +78,44 @@ func main() { // Successfully connected to Twitch app.TwitchClient.OnConnect(func() { app.Logger.Infow("Successfully connected to Twitch Servers", - "Bot username", cfg.TwitchUsername, - "Environment", cfg.Environment, + "Bot username", cfg.twitchUsername, + "Environment", cfg.environment, ) common.Send("nourylul", "xd", app.TwitchClient) }) app.TwitchClient.Join("nourylul") - err := app.TwitchClient.Connect() + err = app.TwitchClient.Connect() if err != nil { panic(err) } } + +func openDB(cfg config) (*sql.DB, error) { + db, err := sql.Open("postgres", cfg.db.dsn) + if err != nil { + return nil, err + } + + db.SetMaxOpenConns(cfg.db.maxOpenConns) + + db.SetMaxIdleConns(cfg.db.maxIdleConns) + + duration, err := time.ParseDuration(cfg.db.maxIdleTime) + if err != nil { + return nil, err + } + + db.SetConnMaxIdleTime(duration) + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + err = db.PingContext(ctx) + if err != nil { + return nil, err + } + + return db, nil +} diff --git a/internal/config/config.go b/internal/config/config.go deleted file mode 100644 index b33210e..0000000 --- a/internal/config/config.go +++ /dev/null @@ -1,26 +0,0 @@ -package config - -import ( - "log" - "os" - - "github.com/joho/godotenv" -) - -type Config struct { - TwitchUsername string - TwitchOauth string - Environment string -} - -func New() *Config { - err := godotenv.Load() - if err != nil { - log.Fatal("Error loading .env file") - } - twitchUsername := os.Getenv("TWITCH_USERNAME") - twitchOauth := os.Getenv("TWITCH_OAUTH") - environment := "Development" - - return &Config{twitchUsername, twitchOauth, environment} -} diff --git a/migrations/README.md b/migrations/README.md new file mode 100644 index 0000000..090377e --- /dev/null +++ b/migrations/README.md @@ -0,0 +1,27 @@ +# Migrations + +Tool: [golang-migrate](https://github.com/golang-migrate/migrate/tree/master/cmd/migrate) + +``` +$ sudo -u postgres psql +psql (14.3) +Type "help" for help. + +postgres=# CREATE DATABASE nourybot; +CREATE DATABASE +postgres=# \c nourybot; +You are now connected to database "nourybot" as user "postgres". +nourybot=# CREATE ROLE username WITH LOGIN PASSWORD 'password'; +CREATE ROLE +nourybot=# CREATE EXTENSION IF NOT EXISTS citext; +CREATE EXTENSION +nourybot=# +``` + +``` +$ psql --host=localhost --dbname=nourybot --username=username +psql (14.3) +Type "help" for help. + +nourybot=> +``` \ No newline at end of file diff --git a/pkg/commands/coinflip.go b/pkg/commands/coinflip.go index 4d30256..d7a4d32 100644 --- a/pkg/commands/coinflip.go +++ b/pkg/commands/coinflip.go @@ -1,15 +1,12 @@ package commands import ( - "fmt" - "github.com/gempir/go-twitch-irc/v3" "github.com/lyx0/nourybot/pkg/common" ) func Coinflip(target string, tc *twitch.Client) { flip := common.GenerateRandomNumber(2) - fmt.Println(flip) switch flip { case 0: