/
router.go
240 lines (210 loc) · 5.3 KB
/
router.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
/*
* Copyright (c) 2018 LI Zhennan
*
* Use of this work is governed by an MIT License.
* You may find a license copy in project root.
*/
package main
import (
"bytes"
"context"
"fmt"
"image"
"io"
"net/http"
"os"
"os/signal"
"sync/atomic"
"syscall"
"time"
"github.com/pkg/errors"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/nanmu42/qrcode-api"
"go.uber.org/zap"
"github.com/gin-gonic/gin"
)
// requestCounter is used to count request num
// should only be affected by atomic action
var requestCounter uint64
func setupRouter() (router *gin.Engine) {
if C.Debug {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
router = gin.New()
router.HandleMethodNotAllowed = true
router.Use(gin.Recovery())
// set debug mode if in need
if C.Debug {
router.Use(gin.Logger())
}
// log requests
router.Use(RequestLogger(logger))
// setup routes
router.GET("/encode", EncodeQRCode)
router.POST("/decode", DecodeQRCode)
return
}
func startAPI(handler http.Handler, port string) {
// timeout for safe exit
const shutdownTimeout = 2 * time.Minute
var exitSignals = []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT}
server := &http.Server{
Addr: port,
Handler: handler,
// ReadTimeout time limit to read the request
ReadTimeout: 10 * time.Second,
// WriteTimeout time limit for request reading and response
WriteTimeout: 40 * time.Second,
// IdleTimeout keep-alive waiting time
IdleTimeout: 60 * time.Second,
// MaxHeaderBytes max header is 8KB
MaxHeaderBytes: 1 << 13,
}
go func() {
// service connections
fmt.Println("API starting...")
logger.Info("API starting...")
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Printf("API HTTP service: %v", err)
logger.Fatal("API HTTP service fatal error",
zap.Error(err),
)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of `shutdownTimeout` seconds.
quit := make(chan os.Signal, 1)
signal.Notify(quit, exitSignals...)
<-quit
fmt.Println("API is exiting safely...")
logger.Info("API is exiting safely...")
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
fmt.Println("API exiting timed out:", err)
logger.Fatal("API exiting timed out",
zap.Error(err),
)
}
logger.Info("API exited successfully. :)")
fmt.Println("API exited successfully. :)")
return
}
// RequestLogger logs every request via zap
func RequestLogger(l *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
// request timer
receivedAt := time.Now()
// counter++
sequence := atomic.AddUint64(&requestCounter, 1)
// before
c.Next()
// after
l.Info("request",
zap.Uint64("seq", sequence),
zap.String("path", c.Request.URL.Path),
zap.Int("status", c.Writer.Status()),
zap.String("IP", c.ClientIP()),
zap.String("UA", c.Request.UserAgent()),
zap.String("ref", c.Request.Referer()),
zap.Duration("lapse", time.Now().Sub(receivedAt)),
zap.Strings("err", c.Errors.Errors()),
)
}
}
// EncodeQRCode controller to encode QR code per request
func EncodeQRCode(c *gin.Context) {
var err error
encoder, err := ParseEncodeRequest(c.Request.URL.Query())
if err != nil {
c.Error(err)
c.String(http.StatusBadRequest, err.Error())
return
}
var buf bytes.Buffer
gotType, err := encoder.Encode(&buf)
if err != nil {
c.Error(err)
c.String(http.StatusInternalServerError, err.Error())
return
}
switch gotType {
case qrcode.TypePNG:
c.DataFromReader(http.StatusOK, int64(buf.Len()), "image/png", &buf, map[string]string{})
case qrcode.TypeString:
c.DataFromReader(http.StatusOK, int64(buf.Len()), "text/plain; charset=utf-8", &buf, map[string]string{})
}
return
}
// DecodeQRCode controller to decode QR Code
func DecodeQRCode(c *gin.Context) {
var err error
// avoid too big image
if c.Request.ContentLength >= maxDecodeFileByte {
err = errors.New("request is too big(content-length)")
c.Error(err)
c.Request.Body.Close()
c.JSON(http.StatusRequestEntityTooLarge, DecodeResponse{
OK: false,
Desc: "request is too big",
Content: nil,
})
return
}
var buf bytes.Buffer
_, readErr := io.CopyN(&buf, c.Request.Body, maxDecodeFileByte)
if readErr == nil {
err = errors.New("request is too big(actually read)")
c.Error(err)
c.Request.Body.Close()
c.JSON(http.StatusRequestEntityTooLarge, DecodeResponse{
OK: false,
Desc: "request is too big",
Content: nil,
})
return
}
if readErr != io.EOF {
err = errors.Wrap(readErr, "body read error")
c.Error(err)
c.JSON(http.StatusOK, DecodeResponse{
OK: false,
Desc: err.Error(),
Content: nil,
})
return
}
// decode image
input, _, err := image.Decode(&buf)
if err != nil {
err = errors.Wrap(err, "file decoding error")
c.Error(err)
c.JSON(http.StatusOK, DecodeResponse{
OK: false,
Desc: err.Error(),
Content: nil,
})
return
}
contents, err := qrcode.DecodeQRCode(input)
if err != nil {
err = errors.Wrap(err, "QR Code scanning error")
c.Error(err)
c.JSON(http.StatusOK, DecodeResponse{
OK: false,
Desc: err.Error(),
Content: nil,
})
return
}
c.JSON(http.StatusOK, DecodeResponse{
OK: true,
Desc: "",
Content: contents,
})
return
}