Skip to content

Commit

Permalink
14-Deployment-On-Heroku heroku postgres db init
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfy committed Oct 23, 2018
1 parent 8259a0b commit 43cd017
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/db_init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
41 changes: 40 additions & 1 deletion config/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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")
Expand All @@ -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"
}
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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))
}
13 changes: 10 additions & 3 deletions model/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit 43cd017

Please sign in to comment.