move config to main file and add database connection

This commit is contained in:
lyx0 2022-08-08 23:13:33 +02:00
parent 214d405743
commit 6fbe68a0a4
4 changed files with 100 additions and 35 deletions

View file

@ -1,29 +1,68 @@
package main package main
import ( import (
"context"
"database/sql"
"log"
"os"
"time"
"github.com/gempir/go-twitch-irc/v3" "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" "github.com/lyx0/nourybot/pkg/common"
"go.uber.org/zap" "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 { type Application struct {
TwitchClient *twitch.Client TwitchClient *twitch.Client
Logger *zap.SugaredLogger Logger *zap.SugaredLogger
Db *sql.DB
} }
func main() { 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() sugar := zap.NewExample().Sugar()
defer sugar.Sync() defer sugar.Sync()
// Start time
common.StartTime() common.StartTime()
db, err := openDB(cfg)
if err != nil {
sugar.Fatal(err)
}
app := &Application{ app := &Application{
TwitchClient: tc, TwitchClient: tc,
Logger: sugar, Logger: sugar,
Db: db,
} }
// Received a PrivateMessage (normal chat message). // Received a PrivateMessage (normal chat message).
@ -39,16 +78,44 @@ func main() {
// Successfully connected to Twitch // Successfully connected to Twitch
app.TwitchClient.OnConnect(func() { app.TwitchClient.OnConnect(func() {
app.Logger.Infow("Successfully connected to Twitch Servers", app.Logger.Infow("Successfully connected to Twitch Servers",
"Bot username", cfg.TwitchUsername, "Bot username", cfg.twitchUsername,
"Environment", cfg.Environment, "Environment", cfg.environment,
) )
common.Send("nourylul", "xd", app.TwitchClient) common.Send("nourylul", "xd", app.TwitchClient)
}) })
app.TwitchClient.Join("nourylul") app.TwitchClient.Join("nourylul")
err := app.TwitchClient.Connect() err = app.TwitchClient.Connect()
if err != nil { if err != nil {
panic(err) 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
}

View file

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

27
migrations/README.md Normal file
View file

@ -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=>
```

View file

@ -1,15 +1,12 @@
package commands package commands
import ( import (
"fmt"
"github.com/gempir/go-twitch-irc/v3" "github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/pkg/common" "github.com/lyx0/nourybot/pkg/common"
) )
func Coinflip(target string, tc *twitch.Client) { func Coinflip(target string, tc *twitch.Client) {
flip := common.GenerateRandomNumber(2) flip := common.GenerateRandomNumber(2)
fmt.Println(flip)
switch flip { switch flip {
case 0: case 0: