-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (64 loc) · 1.2 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
package main
import (
"database/sql"
"fmt"
"log"
"os"
"strconv"
_ "github.com/go-sql-driver/mysql"
"github.com/labstack/echo/v4"
)
type dbConfig struct {
User string
Pass string
Host string
Port int
Database string
}
func newDBConfig() (*dbConfig, error) {
user := os.Getenv("MYSQL_USER")
pass := os.Getenv("MYSQL_PASS")
host := os.Getenv("MYSQL_HOST")
port, err := strconv.Atoi(os.Getenv("MYSQL_PORT"))
if err != nil {
return nil, err
}
database := os.Getenv("MYSQL_DBNAME")
return &dbConfig{
user,
pass,
host,
port,
database,
}, nil
}
func initializeDB(config *dbConfig) (*sql.DB, error) {
connectionString := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s",
config.User,
config.Pass,
config.Host,
config.Port,
config.Database,
)
return sql.Open("mysql", connectionString)
}
const ServerPort = 8080
func main() {
config, err := newDBConfig()
if err != nil {
log.Fatal(err)
}
db, err := initializeDB(config)
if err != nil {
log.Fatal(err)
}
defer func() {
if err = db.Close(); err != nil {
fmt.Println(err.Error())
}
}()
e := echo.New()
InitializeRootHandler(db, e).RegisterAll()
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", ServerPort)))
}