From 43cd0173a5c698b191e42e2c4b244645be01ddd7 Mon Sep 17 00:00:00 2001 From: bonfy Date: Tue, 23 Oct 2018 16:19:19 +0800 Subject: [PATCH] 14-Deployment-On-Heroku heroku postgres db init --- cmd/db_init/main.go | 1 + config/g.go | 41 ++++++++++++++++++++++++++++++++++++++++- main.go | 7 ++++++- model/g.go | 13 ++++++++++--- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/cmd/db_init/main.go b/cmd/db_init/main.go index ebd542e..c989585 100644 --- a/cmd/db_init/main.go +++ b/cmd/db_init/main.go @@ -5,6 +5,7 @@ import ( "github.com/bonfy/go-mega-code/model" _ "github.com/jinzhu/gorm/dialects/mysql" + _ "github.com/jinzhu/gorm/dialects/postgres" ) func main() { diff --git a/config/g.go b/config/g.go index c71b05d..ef35f4a 100644 --- a/config/g.go +++ b/config/g.go @@ -2,13 +2,24 @@ package config import ( "fmt" + "log" + "os" + "strconv" "github.com/spf13/viper" ) func init() { projectName := "go-mega" - getConfig(projectName) + dbType := GetDBType() + log.Println("OS DBTYPE:", dbType) + + if IsHeroku() { + log.Println("Get Env from os.env") + } else { + log.Println("Init viper") + getConfig(projectName) + } } func getConfig(projectName string) { @@ -35,8 +46,21 @@ func GetMysqlConnectingString() string { return fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=%s&parseTime=true&loc=Local", usr, pwd, host, db, charset) } +// GetHerokuConnectingString func +func GetHerokuConnectingString() string { + return os.Getenv("DATABASE_URL") +} + // GetSMTPConfig func func GetSMTPConfig() (server string, port int, user, pwd string) { + if IsHeroku() { + server = os.Getenv("MAIL_SMTP") + port, _ = strconv.Atoi(os.Getenv("MAIL_SMTP_PORT")) + user = os.Getenv("MAIL_USER") + pwd = os.Getenv("MAIL_PASSWORD") + return + } + server = viper.GetString("mail.smtp") port = viper.GetInt("mail.smtp-port") user = viper.GetString("mail.user") @@ -46,6 +70,21 @@ func GetSMTPConfig() (server string, port int, user, pwd string) { // GetServerURL func func GetServerURL() (url string) { + if IsHeroku() { + url = os.Getenv("SERVER_URL") + return + } url = viper.GetString("server.url") return } + +// GetDBType func +func GetDBType() string { + dbtype := os.Getenv("DBTYPE") + return dbtype +} + +// IsHeroku func +func IsHeroku() bool { + return GetDBType() == "heroku" +} diff --git a/main.go b/main.go index df4141b..4764ae0 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,15 @@ package main import ( + "log" "net/http" + "os" "github.com/bonfy/go-mega-code/controller" "github.com/bonfy/go-mega-code/model" "github.com/gorilla/context" _ "github.com/jinzhu/gorm/dialects/mysql" + _ "github.com/jinzhu/gorm/dialects/postgres" ) func main() { @@ -18,5 +21,7 @@ func main() { // Setup Controller controller.Startup() - http.ListenAndServe(":8888", context.ClearHandler(http.DefaultServeMux)) + port := os.Getenv("PORT") + log.Println("Running on port: ", port) + http.ListenAndServe(":"+port, context.ClearHandler(http.DefaultServeMux)) } diff --git a/model/g.go b/model/g.go index 8c919ce..faed6d2 100644 --- a/model/g.go +++ b/model/g.go @@ -16,9 +16,16 @@ func SetDB(database *gorm.DB) { // ConnectToDB func func ConnectToDB() *gorm.DB { - connectingStr := config.GetMysqlConnectingString() - log.Println("Connet to db...") - db, err := gorm.Open("mysql", connectingStr) + if config.IsHeroku() { + return ConnectToDBByDBType("postgres", config.GetHerokuConnectingString()) + } + return ConnectToDBByDBType("mysql", config.GetMysqlConnectingString()) +} + +// ConnectToDBByDBType func +func ConnectToDBByDBType(dbtype, connectingStr string) *gorm.DB { + log.Println("DB Type:", dbtype, "\nConnet to db...") + db, err := gorm.Open(dbtype, connectingStr) if err != nil { panic("Failed to connect database") }