-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathmain.go
More file actions
176 lines (142 loc) · 3.94 KB
/
Copy pathmain.go
File metadata and controls
176 lines (142 loc) · 3.94 KB
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
package main
import (
"bytes"
"fmt"
"image/color"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/gin-gonic/gin"
qrcode "github.com/uncopied/go-qrcode"
)
func init() {
initLog()
}
func initLog() {
// setup logrus
logLevel, err := log.ParseLevel(os.Getenv("LOG_LEVEL"))
if err != nil {
logLevel = log.InfoLevel
}
log.SetLevel(logLevel)
}
func LoggingMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
// Starting time
startTime := time.Now()
// Processing request
// ctx.Next() // bug?
// End Time
endTime := time.Now()
// execution time
latencyTime := endTime.Sub(startTime)
// Request method
reqMethod := ctx.Request.Method
// Request route
reqUri := ctx.Request.RequestURI
// status code
statusCode := ctx.Writer.Status()
// Request IP
clientIP := ctx.ClientIP()
reqBody, _ := io.ReadAll(ctx.Request.Body)
ctx.Request.Body = io.NopCloser(bytes.NewReader(reqBody))
log.WithFields(log.Fields{
"6_BODY": string(reqBody),
"5_METHOD": reqMethod,
"2_URI": reqUri,
"3_STATUS": statusCode,
"4_LATENCY": latencyTime,
"1_CLIENT_IP": clientIP,
}).Info("HTTP REQUEST")
ctx.Next()
}
}
// https://stackoverflow.com/questions/54197913/parse-hex-string-to-image-color
func ParseHexColor(s string) (c color.RGBA, err error) {
c.A = 0xff
switch len(s) {
case 6:
_, err = fmt.Sscanf(s, "%02x%02x%02x", &c.R, &c.G, &c.B)
case 3:
_, err = fmt.Sscanf(s, "%1x%1x%1x", &c.R, &c.G, &c.B)
// Double the hex digits:
c.R *= 17
c.G *= 17
c.B *= 17
default:
err = fmt.Errorf("invalid length, must be 7 or 4")
}
return
}
func getQRCode(c *gin.Context) {
var data string = c.Query("data")
size, _ := strconv.Atoi(c.Query("size"))
var ecc string = strings.ToUpper(c.Query("ecc"))
var recovery_level qrcode.RecoveryLevel = qrcode.Low
// margin, _ := strconv.Atoi(c.Query("margin")), no margin is supported yet in go-qrcode
var color string = c.Query("color")
var bgcolor string = c.Query("bgcolor")
// qzone, no qzone is supported yet in go-qrcode
var format string = strings.ToLower(c.Query("format"))
if (strings.Compare(ecc, "L") == 0) ||
(strings.Compare(ecc, "M") == 0) ||
(strings.Compare(ecc, "Q") == 0) ||
(strings.Compare(ecc, "H") == 0) {
if strings.Compare(ecc, "L") == 0 {
recovery_level = qrcode.Low
} else if strings.Compare(ecc, "M") == 0 {
recovery_level = qrcode.Medium
} else if strings.Compare(ecc, "Q") == 0 {
recovery_level = qrcode.High
} else if strings.Compare(ecc, "H") == 0 {
recovery_level = qrcode.Highest
} else {
log.Error("ECC aka RecoveryLevel is not supported")
}
}
mycolor, _ := ParseHexColor(color)
mybgcolor, _ := ParseHexColor(bgcolor)
var q *qrcode.QRCode
q, err := qrcode.New(data, recovery_level)
checkError(err)
q.ForegroundColor = mycolor
q.BackgroundColor = mybgcolor
if strings.Compare(format, "svg") == 0 {
qrSVG, err := q.SVG()
checkError(err)
c.Header("Content-Disposition", "inline; filename=qrcode.svg")
c.Data(http.StatusOK, "application/octet-stream", []byte(qrSVG))
} else /* if strings.Compare(format, "eps") == 0 {
qrEPS, err := q.EPS()
checkError(err)
c.Header("Content-Disposition", "inline; filename=qrcode.eps")
c.Data(http.StatusOK, "application/octet-stream", []byte(qrEPS))
} else if strings.Compare(format, "pdf") == 0 {
qrPDF, err := q.PDF()
checkError(err)
c.Header("Content-Disposition", "inline; filename=qrcode.pdf")
c.Data(http.StatusOK, "application/octet-stream", []byte(qrPDF))
} else */{
var png []byte
png, err = q.PNG(size)
checkError(err)
c.Header("Content-Disposition", "inline; filename=qrcode.png")
c.Data(http.StatusOK, "application/octet-stream", png)
}
}
func main() {
router := gin.New()
router.Use(gin.Recovery())
router.Use(LoggingMiddleware())
router.GET("/api/qrcode", getQRCode)
router.Run(":6868")
}
func checkError(err error) {
if err != nil {
log.Error(err)
}
}