feat: move database init in services

master
fbonhomm 2019-08-26 19:57:56 +02:00
parent bc733dc35a
commit 6b729565f1
2 changed files with 36 additions and 1 deletions

View File

@ -0,0 +1,35 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
*/
package services
import (
"fmt"
"github.com/fbonhomm/api-go/source/models"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"log"
"os"
)
var Db *gorm.DB
var Err error
func Database() {
Db, Err = gorm.Open(
"postgres",
fmt.Sprintf(
"host=%s port=%s dbname=%s user=%s password=%s sslmode=disable",
os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_NAME"),
os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD")),
)
if Err != nil {
log.Panic("Error connect database: ", Err)
}
Db.AutoMigrate(&models.User{})
}

View File

@ -20,7 +20,7 @@ var PublicKeyRefresh *ecdsa.PublicKey
var PrivateKeyRefresh *ecdsa.PrivateKey var PrivateKeyRefresh *ecdsa.PrivateKey
var err error var err error
func init() { func Jwt() {
var tmp []byte var tmp []byte
tmp, _ = ioutil.ReadFile(ROOT + "/jwt/access.public.pem") tmp, _ = ioutil.ReadFile(ROOT + "/jwt/access.public.pem")