/
main.go
123 lines (99 loc) · 2.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
"github.com/labstack/gommon/log"
heartbeat "github.com/qbert/heartbeat-golang"
"github.com/qbert/zbraStuff/models"
"net/http"
"syscall"
)
var (
port string
ip string
e *echo.Echo
dbuser string
dbpassword string
dbhost string
dbport string
dbname string
db *gorm.DB
)
func configEnv() {
port = getEnv("OPENSHIFT_GO_PORT", "C9_PORT", "1323")
ip = getEnv("OPENSHIFT_GO_IP", "C9_IP", "localhost")
dbuser = getEnv("OPENSHIFT_MYSQL_DB_USERNAME", "C9_USER", "qbert")
dbpassword = getEnv("OPENSHIFT_MYSQL_DB_PASSWORD", "C9_DB_PASSWORD", "neinegal")
dbhost = getEnv("OPENSHIFT_MYSQL_DB_HOST", "C9_DB_HOST", "localhost")
dbport = getEnv("OPENSHIFT_MYSQL_DB_PORT", "C9_DB_PORT", "3306")
dbname = getEnv("OPENSHIFT_MYSQL_DB_NAME", "C9_DB_NAME", "zbrastuff_db")
}
func configDb() {
var dbconnect string
dbconnect = dbuser
if dbpassword != "" {
dbconnect += ":" + dbpassword
}
dbconnect += "@"
if dbhost != "" {
dbconnect += "tcp(" + dbhost
if dbport != "" {
dbconnect += ":" + dbport
}
dbconnect += ")"
}
dbconnect += "/" + dbname + "?charset=utf8&parseTime=True"
log.Info("Attempting to connect to db with:%s", dbconnect)
dbm, err := gorm.Open("mysql", dbconnect)
if err != nil {
panic("Unable to connect to the database")
}
dbm.DB().Ping()
dbm.DB().SetMaxIdleConns(10)
dbm.DB().SetMaxOpenConns(100)
dbm.LogMode(true)
dbm.SingularTable(true)
dbm.Set("gorm:table_options", "ENGINE=InnoDB")
// and fix db on startup
dbm.AutoMigrate(&models.User{})
db = &dbm
}
func configEcho() {
// Echo instance
e = echo.New()
// Middleware
e.Use(mw.Logger())
e.Use(mw.Recover())
// Routes
e.Get("/", hello)
}
func runHeartbeat() {
log.Info("Running Heartbeat on:%s:%s/heartbeat", ip, port)
e.Get("/heartbeat", heartbeat.Handler)
}
func runEcho() {
log.Info("Running Echo Server on:%s:%s", ip, port)
e.Run(ip + ":" + port)
}
func main() {
configEnv()
configDb()
configEcho()
runHeartbeat()
runEcho()
}
func getEnv(e1, e2, d string) string {
if r, found := syscall.Getenv(e1); found {
return r
}
if r, found := syscall.Getenv(e2); found {
return r
}
return d
}
// Handler
func hello(c *echo.Context) error {
return c.String(http.StatusOK, "Hello, World! I was here\n")
}