Skip to content

Commit

Permalink
Restructure DB connecting a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Brawl345 committed Apr 5, 2022
1 parent 8797519 commit 54894ce
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
BOT_TOKEN=123456789:abcdefghijklmnopqrstuvxwyz
MYSQL_URL=user:password@tcp(127.0.0.1:3306)/database_name?charset=utf8mb4&parseTime=True&loc=Local
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
MYSQL_USER=myuser
MYSQL_PASSWORD=mypassword
MYSQL_DB=mydb
5 changes: 3 additions & 2 deletions handler/funcs.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package handler

import (
"gopkg.in/telebot.v3"
"log"
"os"

"gopkg.in/telebot.v3"
)

var defaultSendOptions = &telebot.SendOptions{
Expand All @@ -13,7 +14,7 @@ var defaultSendOptions = &telebot.SendOptions{
}

func isDebugMode() bool {
_, exists := os.LookupEnv("TAGESSCHAU_EILBOT_DEBUG")
_, exists := os.LookupEnv("DEBUG")
return exists
}

Expand Down
15 changes: 6 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package main

import (
"github.com/Brawl345/tagesschau-eilbot/handler"
"github.com/Brawl345/tagesschau-eilbot/storage"
_ "github.com/joho/godotenv/autoload"
"log"
"os"
"os/signal"
"syscall"
"time"

"github.com/Brawl345/tagesschau-eilbot/handler"
"github.com/Brawl345/tagesschau-eilbot/storage"
_ "github.com/joho/godotenv/autoload"

"gopkg.in/telebot.v3"
)

func main() {
db, err := storage.Open(os.Getenv("TAGESSCHAU_EILBOT_MYSQL_URL"))
db, err := storage.Connect()
if err != nil {
log.Fatal(err)
}

if err := db.Ping(); err != nil {
log.Fatal(err)
}

log.Println("Database connection established")

n, err := db.Migrate()
Expand All @@ -34,7 +31,7 @@ func main() {
}

pref := telebot.Settings{
Token: os.Getenv("TAGESSCHAU_EILBOT_TOKEN"),
Token: os.Getenv("BOT_TOKEN"),
Poller: &telebot.LongPoller{Timeout: 10 * time.Second},
}

Expand Down
37 changes: 28 additions & 9 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package storage

import (
"embed"
"fmt"
"os"
"strings"
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
migrate "github.com/rubenv/sql-migrate"
"time"
)

//go:embed migrations/*
Expand All @@ -17,20 +21,35 @@ type DB struct {
System SystemStorage
}

func Open(url string) (*DB, error) {
db, err := sqlx.Open("mysql", url)
func Connect() (*DB, error) {
host := strings.TrimSpace(os.Getenv("MYSQL_HOST"))
port := strings.TrimSpace(os.Getenv("MYSQL_PORT"))
user := strings.TrimSpace(os.Getenv("MYSQL_USER"))
password := strings.TrimSpace(os.Getenv("MYSQL_PASSWORD"))
db := strings.TrimSpace(os.Getenv("MYSQL_DB"))

connectionString := fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
user,
password,
host,
port,
db,
)

conn, err := sqlx.Connect("mysql", connectionString)
if err != nil {
return nil, err
}

db.SetMaxIdleConns(100)
db.SetMaxOpenConns(100)
db.SetConnMaxIdleTime(3 * time.Minute)
conn.SetMaxIdleConns(100)
conn.SetMaxOpenConns(100)
conn.SetConnMaxIdleTime(3 * time.Minute)

return &DB{
DB: db,
Subscribers: &Subscribers{db},
System: &System{db},
DB: conn,
Subscribers: &Subscribers{DB: conn},
System: &System{DB: conn},
}, nil
}

Expand Down

0 comments on commit 54894ce

Please sign in to comment.