patchman/server/database/connection.go

27 lines
693 B
Go
Raw Permalink Normal View History

2023-10-26 14:56:36 +02:00
package database
import (
"fmt"
gormsessions "github.com/gin-contrib/sessions/gorm"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
)
var DB *gorm.DB
var SessionStore gormsessions.Store
func Connect() {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable",
viper.Get("database.ip"), viper.Get("database.user"),
viper.Get("database.password"), viper.Get("database.name"),
viper.Get("database.port"))
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
log.Fatalf("Can't connect to DB: %v", err)
}
DB = db
SessionStore = gormsessions.NewStore(db, true, []byte(viper.GetString("sessionsecret")))
}