-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
62 lines (44 loc) · 1004 Bytes
/
util.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
package main
import (
"database/sql"
"fmt"
"log"
"os"
)
func getEnv(envKey string) (envValue string) {
envValue, ok := os.LookupEnv(envKey)
if ok != true {
log.Printf("please set %s environment variable", envKey)
return
}
return envValue
}
func songHistory(list []Song, song Song) []Song {
var data []Song
if len(list) == 3 {
list = list[:len(list)-1]
}
data = append(data, song)
for _, v := range list {
data = append(data, v)
}
return data
}
func dbConnection() (*sql.DB, error) {
username := getEnv("MYSQL_USERNAME")
password := getEnv("MYSQL_PASSWORD")
host := getEnv("MYSQL_HOST")
database := getEnv("MYSQL_DATABASE")
connection := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8", username, password, host, database)
// Open and connect do DB
db, err := sql.Open("mysql", connection)
if err != nil {
return nil, err
}
// Open doesn't open a connection. Validate DSN data:
err = db.Ping()
if err != nil {
return nil, err
}
return db, nil
}