65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
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"
|
|
)
|
|
|
|
func main() {
|
|
//gin.SetMode(gin.ReleaseMode)
|
|
r := gin.Default()
|
|
var err error
|
|
|
|
//get configuration
|
|
cfg := models.Config{}
|
|
if err := env.Parse(&cfg); err != nil {
|
|
log.Printf("%+v\n", err)
|
|
}
|
|
|
|
//Start postgres connection
|
|
pool := createDatabasePool(cfg)
|
|
|
|
r.LoadHTMLGlob("html/**/*")
|
|
r.Static("/static", "html/static")
|
|
//r.StaticFile("/favicon.ico", "html/favicon.ico")
|
|
//routes.RoutesInit(r, pool)
|
|
routes.RoutesInit(r, pool, &cfg)
|
|
|
|
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
|
|
} |