-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (71 loc) · 1.85 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
package main
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"net/http"
"os"
"strconv"
)
var db *sql.DB
func main() {
dbConnection := os.Getenv("DB_CONNECTION")
if dbConnection != "" {
db = connect()
}
http.HandleFunc("/", health)
http.HandleFunc("/db", hello)
log.Println("Server listening on port 8080...")
http.ListenAndServe(":8080", nil)
}
func health(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
hostname, _ := os.Hostname()
m := make(map[string]interface{})
m["server"] = hostname
resp, _ := json.Marshal(m)
fmt.Fprintf(w, string(resp))
}
func hello(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
rows, err := db.Query(`SELECT SLEEP(2);`)
if err != nil {
log.Println(err)
fmt.Fprintf(w, "Error.")
return
}
defer rows.Close()
hostname, _ := os.Hostname()
m := make(map[string]interface{})
m["server"] = hostname
m["db"] = "OK"
resp, _ := json.Marshal(m)
fmt.Fprintf(w, string(resp))
}
func connect() *sql.DB {
dbHost := os.Getenv("DB_HOST")
dbPort := os.Getenv("DB_PORT")
dbDatabase := os.Getenv("DB_DATABASE")
dbUsername := os.Getenv("DB_USERNAME")
dbPassword := os.Getenv("DB_PASSWORD")
dbMaxConnections := os.Getenv("DB_CONNECTION_MAX")
maxConnection, _ := strconv.Atoi(dbMaxConnections)
log.Printf("DB Host: %s\n", dbHost)
log.Printf("DB Port: %s\n", dbPort)
log.Printf("DB Database: %s\n", dbDatabase)
log.Printf("DB Username: %s\n", dbUsername)
log.Printf("DB Max Connection: %d\n", maxConnection)
db, err := sql.Open("mysql", dbUsername+":"+dbPassword+"@tcp("+dbHost+":"+dbPort+")/"+dbDatabase)
db.SetMaxOpenConns(maxConnection)
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
log.Fatal(err)
}
log.Println("Database connected.")
return db
}