diff --git a/datastores/data_store.go b/datastores/data_store.go index 69a6665..40f9570 100644 --- a/datastores/data_store.go +++ b/datastores/data_store.go @@ -20,11 +20,13 @@ import ( "github.com/titouanfreville/popcubeapi/models" u "github.com/titouanfreville/popcubeapi/utils" // Importing sql driver. They are used by gorm package and used by default from blank. + "log" + _ "github.com/go-sql-driver/mysql" "github.com/jinzhu/gorm" ) -// Store interface the Stores and usefull DB functions +// StoreInterface interface the Stores and usefull DB functions type StoreInterface interface { Avatar() AvatarStore Channel() ChannelStore @@ -62,6 +64,11 @@ func (store StoreImpl) InitConnection(user string, dbname string, password strin db.Callback().Update().Remove("gorm:update_time_stamp") db.Callback().Update().Remove("gorm:save_associations") + if err := db.DB().Ping(); err != nil { + log.Print("Can't connect to database") + log.Print(host) + return nil + } return db } @@ -86,7 +93,7 @@ func (store StoreImpl) roleInitSave(role models.Role, db *gorm.DB) *u.AppError { // InitDatabase initialise a connection to the database and the database. func (store StoreImpl) InitDatabase(user string, dbname string, password string, host string, port string) { db := store.InitConnection(user, dbname, password, host, port) - + db.Debug().DB().Ping() // Create correct tables // db.AutoMigrate(&models.Avatar{}, &models.Channel{}, &models.Emoji{}, &models.Folder{}, // models.Member{}, &models.Message{}, &models.Organisation{}, &models.Parameter{}, @@ -99,16 +106,18 @@ func (store StoreImpl) InitDatabase(user string, dbname string, password string, // Will not update LastUpdate on .Save() call db.Callback().Update().Remove("gorm:update_time_stamp") db.Callback().Update().Remove("gorm:save_associations") - /* - if db.NewRecord(models.Owner) { - store.roleInitSave(models.Owner, db) - } - if db.NewRecord(&models.Admin) { - store.roleInitSave(models.Admin, db) - } - if db.NewRecord(&models.Standart) { - store.roleInitSave(models.Standart, db) - }*/ + + db.Debug().DB().Ping() + + if db.NewRecord(models.Owner) { + store.roleInitSave(models.Owner, db) + } + if db.NewRecord(&models.Admin) { + store.roleInitSave(models.Admin, db) + } + if db.NewRecord(&models.Standart) { + store.roleInitSave(models.Standart, db) + } } // CloseConnection close database connection diff --git a/docker-compose.yml b/docker-compose.yml index 5003be2..07c5200 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,6 @@ services: image: registry.le-corre.eu:5000/innerapi:slate-alpha-doc volumes: - "./swagger.json:/tmp/swagger" - # - "./docker_resources/slate_resources:/home/slate" ports: - 4567:4567 back: @@ -15,6 +14,12 @@ services: context: . dockerfile: docker/gobase.Dockerfile env_file: .env + volumes: + - "./api:/go/src/github.com/titouanfreville/popcubeapi/api" + - "./datastores:/go/src/github.com/titouanfreville/popcubeapi/datastores" + - "./models:/go/src/github.com/titouanfreville/popcubeapi/models" + - "./utils:/go/src/github.com/titouanfreville/popcubeapi/utils" + - "./main.go:/go/src/github.com/titouanfreville/popcubeapi/main.go" image: registry.le-corre.eu:5000/go:base ports: - 3000:3000 @@ -30,6 +35,7 @@ services: - "./datastores:/go/src/github.com/titouanfreville/popcubeapi/datastores" - "./models:/go/src/github.com/titouanfreville/popcubeapi/models" - "./utils:/go/src/github.com/titouanfreville/popcubeapi/utils" + - "./main.go:/go/src/github.com/titouanfreville/popcubeapi/main.go" ports: - 8082:8080 golint: @@ -42,6 +48,7 @@ services: - "./datastores:/go/src/github.com/titouanfreville/popcubeapi/datastores" - "./models:/go/src/github.com/titouanfreville/popcubeapi/models" - "./utils:/go/src/github.com/titouanfreville/popcubeapi/utils" + - "./main.go:/go/src/github.com/titouanfreville/popcubeapi/main.go" database: image: mariadb:10.1 env_file: .env