-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
180 lines (151 loc) · 3.77 KB
/
http.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
package api
import (
"encoding/json"
"net"
"net/http"
"runtime"
"strconv"
"time"
"github.com/go-chi/chi"
"github.com/rs/xid"
)
// limiter
func (mux *Server) limiter(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
// get client ip addr
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
mux.app.logger.Info("limiter", err.Error(), map[string]string{
"func": "SplitHostPort",
})
}
// get rate
rate := mux.app.limiter.FindIPAddr(ip)
// check rate
if !rate.Allow() {
mux.writer(w, http.StatusTooManyRequests, "slow down")
return
}
// next
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
// takeScreenShot
func (mux *Server) takeScreenShot(w http.ResponseWriter, r *http.Request) {
data := struct {
URL string `json:"url"`
Width int64 `json:"width"`
Height int64 `json:"height"`
}{}
// json decoder
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
if err := dec.Decode(&data); err != nil {
mux.writer(w, http.StatusInternalServerError, err.Error())
return
}
if data.Width == 0 {
data.Width = 1024
}
if data.Height == 0 {
data.Height = 800
}
// img
img := &Image{
UUID: xid.New().String(),
Status: ImageStatusPending,
Message: "",
Binary: nil,
CreatedAt: time.Now(),
}
// save img
if err := mux.app.store.SaveImage(img); err != nil {
mux.app.logger.Error("takeScreenShot", err.Error(), map[string]string{
"func": "SaveImage",
})
}
go func(i *Image) {
data, err := mux.app.Capture(data.URL, data.Width, data.Height)
if err != nil {
// save image error
img.Status = ImageStatusFail
img.Message = err.Error()
if err := mux.app.store.SaveImage(img); err != nil {
mux.app.logger.Error("goroutine", err.Error(), map[string]string{
"func": "SaveImage",
})
}
mux.app.logger.Error("goroutine", err.Error(), map[string]string{
"func": "Capture",
})
return
}
// save img success
img.Status = ImageStatusSuccess
img.Message = ""
img.Binary = data
if err := mux.app.store.SaveImage(img); err != nil {
mux.app.logger.Error("goroutine", err.Error(), map[string]string{
"func": "SaveImage",
})
}
}(img)
mux.writer(w, http.StatusOK, struct {
UUID string `json:"uuid"`
}{
UUID: img.UUID,
})
}
// findScreenShot
func (mux *Server) findScreenShot(w http.ResponseWriter, r *http.Request) {
uuid := chi.URLParam(r, "uuid")
if uuid == "" {
mux.writer(w, http.StatusInternalServerError, "invalid uuid")
return
}
// find screenshot
img, err := mux.app.store.FindImage(uuid)
if err != nil {
mux.writer(w, http.StatusNotFound, err.Error())
return
}
// if image fail
if img.Status == ImageStatusFail {
mux.writer(w, http.StatusInternalServerError, img.Message)
return
}
// if image pending
if img.Status == ImageStatusPending {
mux.writer(w, http.StatusInternalServerError, "screenshot not yet captured.")
return
}
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Length", strconv.Itoa(len(img.Binary)))
w.Write(img.Binary)
}
// stats
func (mux *Server) stats(w http.ResponseWriter, r *http.Request) {
sstats, err := mux.app.store.Statistic()
if err != nil {
mux.writer(w, http.StatusInternalServerError, err.Error())
return
}
memstats := new(runtime.MemStats)
runtime.ReadMemStats(memstats)
// astats
astats := map[string]interface{}{
"Goroutine": runtime.NumGoroutine(),
"CPUs": runtime.NumCPU(),
"System": PrintBytes(memstats.Sys),
"StackSystem": PrintBytes(memstats.StackSys),
"GCSize": PrintBytes(memstats.GCSys),
"CompletedGC": memstats.NumGC,
}
data := map[string]interface{}{
"Store": sstats,
"Limiter": mux.app.limiter.Statistic(),
"Resources": astats,
}
mux.writer(w, http.StatusOK, data)
}