-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
344 lines (293 loc) · 8.32 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
cowsay "github.com/Code-Hex/Neo-cowsay"
"github.com/gomodule/redigo/redis"
"github.com/gorilla/mux"
"github.com/kelseyhightower/envconfig"
"go.uber.org/zap"
"cloud.google.com/go/compute/metadata"
"cloud.google.com/go/logging"
_ "github.com/jackc/pgx/v4/stdlib"
)
const (
CacheHitHeader = "x-cow-cache-hit"
)
type SQLConn struct {
Database string `envconfig:"DB_NAME"`
User string `envconfig:"DB_USER"`
Password string `envconfig:"DB_PASS"`
Socket string `envconfig:"DB_SOCKET"`
}
type RedisConn struct {
Host string `envconfig:"REDIS_HOST"`
Port int `envconfig:"REDIS_PORT"`
}
func main() {
logger, _ := zap.NewDevelopment()
projectID, err := metadata.ProjectID()
if err != nil {
logger.Fatal("Failed to query project ID", zap.Error(err))
}
logClient, err := logging.NewClient(context.Background(), projectID)
if err != nil {
logger.Fatal("Failed to create logging client", zap.Error(err))
}
defer logClient.Close()
auditLogger := logClient.Logger("lumberjack-auditlog", logging.CommonLabels(map[string]string{"app_auditing": "true"}))
defer auditLogger.Flush()
var sqlconn SQLConn
if err := envconfig.Process("", &sqlconn); err != nil {
logger.Fatal("Invalid SQL config", zap.Error(err))
}
var redisconn RedisConn
if err := envconfig.Process("", &redisconn); err != nil {
logger.Fatal("Invalid Redis config", zap.Error(err))
}
project := os.Getenv("PROJECT_ID")
if project == "" {
}
db, err := initDB(sqlconn)
if err != nil {
logger.Fatal("Failed to init DB connection", zap.Error(err))
}
redispool, err := initRedis(redisconn)
if err != nil {
logger.Fatal("Failed to init Redis connection", zap.Error(err))
}
wcs := &whatcowsay{
audit: auditLogger,
logger: logger,
db: db,
redispool: redispool,
}
wcs.setupRoutes()
logger.Info("Listening on port 8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
logger.Error("Exited", zap.Error(err))
}
}
type whatcowsay struct {
audit *logging.Logger
logger *zap.Logger
db *sql.DB
redispool *redis.Pool
r *mux.Router
}
func (wcs *whatcowsay) setupRoutes() {
wcs.r = mux.NewRouter()
cowPath := wcs.r.Path("/cows/{cow}").Subrouter()
cowPath.Methods(http.MethodPost).HandlerFunc(wcs.handleCowUpsert)
cowPath.Methods(http.MethodGet).HandlerFunc(wcs.handleCowSay)
wcs.r.HandleFunc("/admin", wcs.handleAdmin)
http.Handle("/", wcs.r)
}
func (wcs *whatcowsay) handleCowUpsert(w http.ResponseWriter, req *http.Request) {
wcs.audit.Log(logging.Entry{HTTPRequest: &logging.HTTPRequest{Request: req}})
cow := mux.Vars(req)["cow"]
if wcs.redispool == nil && wcs.db == nil {
http.Error(w, "No persistent store available to store what the cow is going to say", http.StatusInternalServerError)
return
}
b, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(w, "Failed to read HTTP request body: "+err.Error(), http.StatusInternalServerError)
return
}
say, err := saySomething(string(b))
if err != nil {
http.Error(w, "Failed to generate figure: "+err.Error(), http.StatusInternalServerError)
return
}
if wcs.db == nil {
wcs.logger.Warn("DB is not used; will save to cache", zap.String("figure", cow))
if err := wcs.setRedisVal(cow, say); err != nil {
http.Error(w, "Failed to save to cache: "+err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
return
}
if err := wcs.passThroughSaveVal(cow, say); err != nil {
http.Error(w, "Failed to save to db: "+err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
func (wcs *whatcowsay) handleCowSay(w http.ResponseWriter, req *http.Request) {
wcs.audit.Log(logging.Entry{HTTPRequest: &logging.HTTPRequest{Request: req}})
cow := mux.Vars(req)["cow"]
if wcs.redispool == nil && wcs.db == nil {
t, err := saySomething(fmt.Sprintf("Hi, I'm %s. Temporary. Don't count on me~", cow))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(t))
return
}
if wcs.redispool != nil {
t, err := wcs.getRedisVal(cow)
// If we can get the value from the cache.
if err == nil && t != "" {
w.Header().Set(CacheHitHeader, "true")
w.WriteHeader(http.StatusOK)
w.Write([]byte(t))
return
}
if err != nil {
wcs.logger.Warn("Redis error", zap.Error(err))
}
}
t, err := wcs.passThroughGetVal(cow)
if err == sql.ErrNoRows {
http.Error(w, "No figure found!", http.StatusNotFound)
return
}
if err != nil {
http.Error(w, "DB error: "+err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(t))
}
func (wcs *whatcowsay) getRedisVal(key string) (string, error) {
if wcs.redispool == nil {
return "", errors.New("Redis is not used")
}
c := wcs.redispool.Get()
defer c.Close()
c.Send("GET", key)
c.Flush()
return redis.String(c.Receive())
}
func (wcs *whatcowsay) setRedisVal(key, val string) error {
if wcs.redispool == nil {
return errors.New("Redis is not used")
}
c := wcs.redispool.Get()
defer c.Close()
c.Send("SET", key, val)
c.Send("EXPIRE", key, "20")
c.Flush()
if _, err := c.Receive(); err != nil {
return err
}
return nil
}
func (wcs *whatcowsay) passThroughSaveVal(key, val string) error {
q := `INSERT INTO cows (name, message)
VALUES ($1, $2)
ON CONFLICT (name)
DO UPDATE SET message = $2;`
if _, err := wcs.db.Exec(q, key, val); err != nil {
return err
}
if err := wcs.setRedisVal(key, val); err != nil {
wcs.logger.Warn("Failed to pass-through save figure in cache", zap.String("figure", key))
}
return nil
}
func (wcs *whatcowsay) passThroughGetVal(key string) (string, error) {
var text string
err := wcs.db.QueryRow("SELECT message FROM cows WHERE name = $1", key).Scan(&text)
if err != nil {
return "", err
}
if err := wcs.setRedisVal(key, text); err != nil {
wcs.logger.Warn("Failed to pass-through save figure in cache", zap.String("figure", key))
}
return text, nil
}
func (wcs *whatcowsay) handleAdmin(w http.ResponseWriter, req *http.Request) {
http.Error(w, "not implemented", http.StatusNotAcceptable)
}
func saySomething(text string) (string, error) {
return cowsay.Say(
cowsay.Phrase(text),
cowsay.BallonWidth(40),
cowsay.Type(pickCow()),
)
}
// To censor the original inappropreiate figures -_-.
func pickCow() string {
allowed := []string{
"cows/beavis.zen.cow",
"cows/bud-frogs.cow",
"cows/bunny.cow",
"cows/daemon.cow",
"cows/default.cow",
"cows/docker.cow",
"cows/dragon.cow",
"cows/elephant.cow",
"cows/flaming-sheep.cow",
"cows/ghostbusters.cow",
"cows/gopher.cow",
"cows/hellokitty.cow",
"cows/kitty.cow",
"cows/koala.cow",
"cows/meow.cow",
"cows/sage.cow",
"cows/sheep.cow",
"cows/skeleton.cow",
"cows/squirrel.cow",
"cows/stegosaurus.cow",
"cows/turkey.cow",
"cows/turtle.cow",
}
cand := allowed[rand.Intn(len(allowed))]
for _, n := range cowsay.AssetNames() {
if n == cand {
return cand
}
}
return "cows/default.cow"
}
func initRedis(redisconn RedisConn) (*redis.Pool, error) {
if redisconn.Host == "" {
return nil, nil
}
redisAddr := fmt.Sprintf("%s:%d", redisconn.Host, redisconn.Port)
const maxConnections = 20
return &redis.Pool{
MaxIdle: maxConnections,
Dial: func() (redis.Conn, error) { return redis.Dial("tcp", redisAddr) },
}, nil
}
func initDB(sqlconn SQLConn) (*sql.DB, error) {
if sqlconn.Database == "" {
return nil, nil
}
db, err := initSocketConnectionPool(sqlconn)
if err != nil {
return nil, err
}
if _, err = db.Exec(`CREATE TABLE IF NOT EXISTS cows ( name VARCHAR(255) NOT NULL, message TEXT NOT NULL, PRIMARY KEY (name) );`); err != nil {
return nil, fmt.Errorf("DB.Exec: unable to create table: %w", err)
}
return db, nil
}
func initSocketConnectionPool(sqlconn SQLConn) (*sql.DB, error) {
var dbURI string
dbURI = fmt.Sprintf("user=%s password=%s database=%s host=%s", sqlconn.User, sqlconn.Password, sqlconn.Database, sqlconn.Socket)
// dbPool is the pool of database connections.
dbPool, err := sql.Open("pgx", dbURI)
if err != nil {
return nil, fmt.Errorf("sql.Open: %v", err)
}
configureConnectionPool(dbPool)
return dbPool, nil
}
func configureConnectionPool(dbPool *sql.DB) {
dbPool.SetMaxIdleConns(5)
dbPool.SetMaxOpenConns(7)
dbPool.SetConnMaxLifetime(1800)
}