-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
279 lines (244 loc) · 6.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"github.com/google/uuid"
_ "github.com/newrelic/go-agent/v3/integrations/nrmysql"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/newrelic/newrelic-opencensus-exporter-go/nrcensus"
"github.com/soichisumi/go-util/logger"
"go.opencensus.io/trace"
"go.uber.org/zap"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
)
const (
defaultPort = "8080"
//defaultDBUser = "root"
//defaultDBPassword = ""
//defaultDBHost = ""
dataSourceName = "root:@tcp(db:3306)/test"
driverName = "nrmysql"
)
var (
db *sql.DB
captureUserID *regexp.Regexp
app *newrelic.Application
//stmtCreateUser *sql.Stmt
//stmtGetUser *sql.Stmt
//stmtListUser *sql.Stmt
)
func init() {
// ?: 0 or more. fewer is preferred
_captureUserID := regexp.MustCompile("^/users/([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12})?.*?")
captureUserID = _captureUserID
}
type User struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
}
func logInterceptor(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
//https://stackoverflow.com/questions/31884093/read-multiple-time-a-reader
b := bytes.NewBuffer(make([]byte, 0))
tr := io.TeeReader(r.Body, b)
var body string
if r.Method == http.MethodPost {
_body, err := ioutil.ReadAll(tr)
if err != nil {
logger.Error("", zap.Error(err))
} else {
body = string(_body)
}
defer r.Body.Close()
}
r.Body = ioutil.NopCloser(b)
logger.Info(
"request received",
zap.String("path", r.URL.Path),
zap.String("query", r.URL.Query().Encode()),
zap.Any("headers", r.Header),
zap.String("body", body))
f(w, r)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func createUser(ctx context.Context, u User) error {
res, err := db.ExecContext(ctx, "INSERT INTO users(id, email, name) VALUES(?, ?, ?)", u.ID, u.Email, u.Name)
if err != nil {
return err
}
logger.Info("user created.", zap.Any("", res))
return nil
}
func listUsers(ctx context.Context) ([]User, error) {
rows, err := db.QueryContext(ctx, "SELECT * FROM users")
if err != nil {
return nil, err
}
var res []User
for rows.Next() {
var (
id string
email string
name string
)
if err := rows.Scan(&id, &email, &name); err != nil {
return nil, err
}
res = append(res, User{
ID: id,
Email: email,
Name: name,
})
}
return res, nil
}
func getUser(ctx context.Context, id string) (User, error) {
u := User{}
if err := db.QueryRowContext(ctx, "SELECT * FROM users WHERE id = ?", id).Scan(&u.ID, &u.Email, &u.Name); err != nil {
return User{}, err
}
return u, nil
}
func getUserID(path string) string {
s := captureUserID.FindStringSubmatch(path)
if len(s) < 2 { // no match
return ""
}
return s[1]
}
func handleCreateUser(ctx context.Context, w http.ResponseWriter, r *http.Request) {
logger.Info("create user")
var u User
if err := json.NewDecoder(r.Body).Decode(&u); err != nil && err != io.EOF {
w.WriteHeader(http.StatusBadRequest)
logger.Error("", zap.Error(err))
return
}
// validate
if u.Email == "" || u.Name == "" {
logger.Error("email or name is empty", zap.Any("user", u))
w.WriteHeader(http.StatusBadRequest)
return
}
u.ID = uuid.New().String()
if err := createUser(ctx, u); err != nil {
w.WriteHeader(http.StatusInternalServerError)
logger.Error("", zap.Error(err))
return
}
w.WriteHeader(http.StatusOK)
}
func handleListUsers(ctx context.Context, w http.ResponseWriter, r *http.Request) {
logger.Info("list users")
users, err := listUsers(ctx)
if err != nil {
logger.Error("", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
res, err := json.Marshal(users)
if err != nil {
logger.Error("", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = w.Write(res)
if err != nil {
logger.Error("", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
func handleGetUser(ctx context.Context, w http.ResponseWriter, r *http.Request) {
logger.Info("get user")
user, err := getUser(ctx, getUserID(r.URL.Path))
if err != nil {
logger.Error("", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
res, err := json.Marshal(user)
if err != nil {
logger.Error("", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = w.Write(res)
if err != nil {
logger.Error("", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
func usersHandler(w http.ResponseWriter, r *http.Request) {
logger.Info("user handler")
ctx := r.Context()
_db, err := sql.Open(driverName, dataSourceName)
if err != nil {
logger.Fatal("", zap.Error(err))
}
db = _db
id := getUserID(r.URL.Path)
switch r.Method {
case http.MethodGet:
if id == "" {
handleListUsers(ctx, w, r)
} else {
handleGetUser(ctx, w, r)
}
case http.MethodPost:
handleCreateUser(ctx, w, r)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
return
}
func main() {
port := defaultPort
if os.Getenv("PORT") != "" {
port = os.Getenv("PORT")
}
_db, err := sql.Open(driverName, dataSourceName)
if err != nil {
logger.Fatal("", zap.Error(err))
}
db = _db
//newrelic apm
_app, err := newrelic.NewApplication(
newrelic.ConfigAppName("newrelic-go-sample"),
newrelic.ConfigLicense(os.Getenv("NEWRELIC_LICENSE_KEY")),
newrelic.ConfigDistributedTracerEnabled(true),
//newrelic.ConfigDebugLogger(os.Stdout),
)
if err != nil {
logger.Fatal("", zap.Error(err))
}
app = _app
exporter, err := nrcensus.NewExporter(
"NewRelicGoSample",
os.Getenv("NEWRELIC_API_KEY"),
)
if err != nil {
log.Fatal(err)
}
trace.RegisterExporter(exporter)
http.HandleFunc(newrelic.WrapHandleFunc(app, "/", logInterceptor(rootHandler)))
http.HandleFunc(newrelic.WrapHandleFunc(app, "/users/", logInterceptor(usersHandler)))
logger.Info("http-mock-server is listening.", zap.String("port", port))
if err := http.ListenAndServe(":"+port, nil); err != nil {
logger.Fatal(err.Error(), zap.Error(err))
}
}