Add migrations
This commit is contained in:
63
main.go
63
main.go
@@ -1,9 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/caarlos0/env/v6"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"log"
|
||||
"time"
|
||||
"trikotwaschliste/database/migrations"
|
||||
"trikotwaschliste/models"
|
||||
"trikotwaschliste/routes"
|
||||
)
|
||||
|
||||
@@ -12,45 +18,50 @@ func main() {
|
||||
|
||||
//gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.Default()
|
||||
var err error
|
||||
|
||||
//get configuration
|
||||
//cfg := model.Config{}
|
||||
//if err := env.Parse(&cfg); err != nil {
|
||||
// log.Printf("%+v\n", err)
|
||||
//}
|
||||
cfg := models.Config{}
|
||||
if err := env.Parse(&cfg); err != nil {
|
||||
log.Printf("%+v\n", err)
|
||||
}
|
||||
|
||||
//Start postgres connection
|
||||
//connPoolConfig := pgx.ConnPoolConfig{
|
||||
// ConnConfig: pgx.ConnConfig{
|
||||
// Host: cfg.DB_HOST,
|
||||
// Port: cfg.DB_PORT,
|
||||
// User: cfg.DB_USER,
|
||||
// Password: cfg.DB_PASSWORD,
|
||||
// Database: cfg.DB_DATABASE,
|
||||
// },
|
||||
// MaxConnections: cfg.DB_MAXCONNECTIONS,
|
||||
//}
|
||||
//pool, err := pgx.NewConnPool(connPoolConfig)
|
||||
//for err != nil {
|
||||
// pool, err = pgx.NewConnPool(connPoolConfig)
|
||||
// if err != nil {
|
||||
// log.Println(err.Error())
|
||||
// log.Println("Unable to create connection pool. Retrying in 10sec...")
|
||||
// }
|
||||
// time.Sleep(10 * time.Second)
|
||||
//}
|
||||
pool := createDatabasePool(cfg)
|
||||
|
||||
|
||||
var err error
|
||||
r.LoadHTMLGlob("html/**/*")
|
||||
r.Static("/static", "html/static")
|
||||
//r.StaticFile("/favicon.ico", "html/favicon.ico")
|
||||
//routes.RoutesInit(r, pool)
|
||||
routes.RoutesInit(r)
|
||||
routes.RoutesInit(r, pool)
|
||||
|
||||
err = r.Run("0.0.0.0:8082")
|
||||
|
||||
if err != nil {
|
||||
log.Println("Something went wrong!")
|
||||
}
|
||||
}
|
||||
|
||||
func createDatabasePool(cfg models.Config) *pgxpool.Pool {
|
||||
log.Println("Checking for migration")
|
||||
migrations.RunMigration(cfg)
|
||||
//create the database url
|
||||
dburl := "postgresql://" + cfg.DB_USER + ":" + cfg.DB_PASSWORD + "@" + cfg.DB_HOST + ":" + fmt.Sprint(cfg.DB_PORT) + "/" + cfg.DB_DATABASE
|
||||
config, err := pgxpool.ParseConfig(dburl)
|
||||
if err != nil {
|
||||
log.Println("Can't parse pgx connection.")
|
||||
return nil
|
||||
}
|
||||
//Set the max amount of database connections
|
||||
config.MaxConns = int32(cfg.DB_MAXCONNECTIONS)
|
||||
//create connection pool
|
||||
pool, err := pgxpool.ConnectConfig(context.Background(), config)
|
||||
for err != nil {
|
||||
pool, err = pgxpool.ConnectConfig(context.Background(), config)
|
||||
if err != nil {
|
||||
log.Println("Can't establish postgres connection.")
|
||||
}
|
||||
time.Sleep(60 * time.Second)
|
||||
}
|
||||
return pool
|
||||
}
|
Reference in New Issue
Block a user