Skip to content
Permalink
Browse files

14-Deployment-On-Heroku heroku postgres db init

  • Loading branch information...
bonfy committed Oct 23, 2018
1 parent 8259a0b commit 43cd0173a5c698b191e42e2c4b244645be01ddd7
Showing with 57 additions and 5 deletions.
  1. +1 −0 cmd/db_init/main.go
  2. +40 −1 config/g.go
  3. +6 −1 main.go
  4. +10 −3 model/g.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() {
@@ -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"
}
@@ -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))
}
@@ -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")
}

0 comments on commit 43cd017

Please sign in to comment.
You can’t perform that action at this time.