-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (122 loc) · 3.23 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"database/sql"
"flag"
"fmt"
"io"
"net/http"
"os"
_ "github.com/go-sql-driver/mysql"
"go.uber.org/zap"
)
var (
host string
user string
pass string
dbname string
)
const (
window = 10
)
func init() {
flag.StringVar(&host, "host", "127.0.0.1:3306", "DB host")
flag.StringVar(&user, "user", "golang", "DB user")
flag.StringVar(&pass, "pass", "golang", "DB pass")
flag.StringVar(&dbname, "dbname", "sample", "DB name")
flag.Parse()
var err error
logger, err = zap.NewProduction()
if err != nil {
fmt.Println("failed to create new logger")
os.Exit(1)
}
// If environment variable set, command line argument is overridden by it.
if value, ok := os.LookupEnv("SAMPLEAPP_DB_HOST"); ok {
logger.Info("configuration overridden by environment variable 'SAMPLEAPP_DB_HOST'")
host = value
}
if value, ok := os.LookupEnv("SAMPLEAPP_DB_USER"); ok {
logger.Info("configuration overridden by environment variable 'SAMPLEAPP_DB_USER'")
user = value
}
if value, ok := os.LookupEnv("SAMPLEAPP_DB_PASS"); ok {
logger.Info("configuration overridden by environment variable 'SAMPLEAPP_DB_PASS'")
pass = value
}
if value, ok := os.LookupEnv("SAMPLEAPP_DB_NAME"); ok {
logger.Info("configuration overridden by environment variable 'SAMPLEAPP_DB_NAME'")
dbname = value
}
}
var (
logger *zap.Logger
istioTracingHeaders = []string{
"x-request-id",
"x-b3-traceid",
"x-b3-spanid",
"x-b3-parentspanid",
"x-b3-sampled",
"x-b3-flags",
"x-ot-span-context",
}
)
func main() {
defer logger.Sync()
http.HandleFunc("/", indexHandler)
logger.Info("listening on 0.0.0.0:8080")
err := http.ListenAndServe(":8080", nil)
logger.Fatal("failed to serve HTTP server", zap.Error(err))
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
dataSource := fmt.Sprintf("%s:%s@tcp(%s)/%s", user, pass, host, dbname)
db, err := sql.Open("mysql", dataSource)
if err != nil {
logger.Fatal("failed to connect to mysql", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
defer db.Close()
if err := db.Ping(); err != nil {
logger.Error("failed to establish DB connection", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
rows, err := db.Query("select id, name from sample_table")
if err != nil {
logger.Error("failed to execute select statement", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
defer rows.Close()
i := 0
result := make([]string, window)
for rows.Next() {
var id int
var name string
if err := rows.Scan(&id, &name); err != nil {
logger.Error("error occured while fetching data", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
result[i] = fmt.Sprintf("%d\t%s\n", id, name)
i++
if i >= window {
break
}
}
if err := rows.Err(); err != nil {
logger.Error("error occured while fetching data", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
for _, header := range istioTracingHeaders {
w.Header().Add(header, r.Header.Get(header))
}
w.WriteHeader(http.StatusOK)
for _, r := range result {
if n, err := io.WriteString(w, r); err != nil || n < len(r) {
logger.Error("error occured while sending data", zap.Error(err))
break
}
}
}