-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
189 lines (148 loc) · 4.26 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
package main
import (
"github.com/xtlsoft/router"
"net/http"
"strconv"
"io/ioutil"
"os"
"bytes"
"image/png"
"image/jpeg"
"fmt"
"image/gif"
"flag"
"github.com/skip2/go-qrcode"
"encoding/base64"
)
type req struct {
writer http.ResponseWriter
request *http.Request
}
func checkCache(r *req, data string, size int, levelString string, typ string) bool {
fileName := fmt.Sprintf("%s-%dpx-level%s.%s", base64.StdEncoding.EncodeToString([]byte(data)), size, levelString, typ)
if d, err := ioutil.ReadFile("./data/cache/" + fileName); err == nil {
r.writer.Write(d)
var mime string
switch typ {
case "jpg":
mime = "image/jpeg"
case "png":
mime = "image/png"
case "gif":
mime = "image/gif"
default:
mime = "text/plain"
}
r.writer.Header().Add("content-type", mime)
return true
}else{
return false
}
}
func generateQr(r *req, data string, size int, levelString string, typ string) {
var level qrcode.RecoveryLevel
switch levelString {
case "1":
level = qrcode.Low
case "2":
level = qrcode.Medium
case "3":
level = qrcode.High
case "4":
level = qrcode.Highest
}
qr, _ := qrcode.New(data, level)
img := qr.Image(size)
buf := bytes.NewBufferString("")
switch typ {
case "jpg":
jpeg.Encode(buf, img, &jpeg.Options{Quality: jpeg.DefaultQuality})
r.writer.Header().Add("content-type", "image/jpeg")
case "png":
png.Encode(buf, img)
r.writer.Header().Add("content-type", "image/png")
case "gif":
gif.Encode(buf, img, &gif.Options{})
r.writer.Header().Add("content-type", "image/gif")
}
fileName := fmt.Sprintf("%s-%dpx-level%s.%s", base64.StdEncoding.EncodeToString([]byte(data)), size, levelString, typ)
ioutil.WriteFile("./data/cache/" + fileName, buf.Bytes(), os.ModeAppend)
r.writer.Write(buf.Bytes())
}
func readFile(path string) string {
r, _ := ioutil.ReadFile(path)
return string(r)
}
func toInt(str string) int {
r, _ := strconv.Atoi(str)
return r
}
func main() {
port := flag.String("port", ":21384", "Port to listen.")
node := flag.String("node", "Default", "Server node name.")
flag.Parse()
r := router.New()
r.SetHandler(
func(isMatched bool, request interface{}, controller interface{}, variables map[string]string) interface{} {
c, r := controller.(func(*req, map[string]string)), request.(*req)
r.writer.Header().Add("X-Powered-By", "Mixcm-QRServer")
r.writer.Header().Add("Server", "Mixcm-QRServer")
r.writer.Header().Add("X-Mixcm-Node", *node)
c(r, variables)
return nil
},
)
r.Any(func (r *req, vars map[string]string){
r.writer.WriteHeader(404)
r.writer.Write([]byte(readFile("./template/404.html")))
})
// Main Code
r.Group("/", func (g *router.Group){
g.Get("/", func (r *req, vars map[string]string){
r.writer.Header().Add("location", "/doc")
r.writer.WriteHeader(302)
})
g.Get("/generate", func (r *req, vars map[string]string){
data, size, typ := r.request.FormValue("data"),
toInt(r.request.FormValue("size")),
r.request.FormValue("type")
if data == "" || typ == "" {
r.writer.Write([]byte("Invalid Request."))
r.writer.WriteHeader(500)
return
}
if !checkCache(r, data, size, r.request.FormValue("level"), typ){
generateQr(r, data, size, r.request.FormValue("level"), typ)
}
r.writer.WriteHeader(200)
})
g.Get("/qr/{data}/{@int:size}px-level{[1-4]:level}.{@ident:type}", func(r *req, vars map[string]string){
dBytes, err := base64.StdEncoding.DecodeString(vars["data"])
if err != nil {
r.writer.Write([]byte("Invalid Request"))
r.writer.WriteHeader(500)
return
}
size, _ := strconv.Atoi(vars["size"])
data, level, typ := string(dBytes),
vars["level"],
vars["type"]
if !checkCache(r, data, size, level, typ){
generateQr(r, data, size, level, typ)
}
r.writer.WriteHeader(200)
})
})
// End Main Code
http.HandleFunc("/", func(w http.ResponseWriter, ree *http.Request){
re := &req{
writer: w,
request: ree,
}
r.Handle(ree.Method, ree.RequestURI, re)
})
http.Handle("/assets", http.StripPrefix("/assets", http.FileServer(http.Dir("./assets"))))
http.Handle("/doc", http.StripPrefix("/doc", http.FileServer(http.Dir("./doc"))))
println("Server started at " + *port)
http.ListenAndServe(*port, nil)
}