-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
137 lines (115 loc) · 2.99 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
package main
import (
"github.com/gorilla/mux"
"image/color"
"log"
"net/http"
"os"
"strconv"
"strings"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/screenshot", ScreenshotHandler).Methods("GET")
fs := http.FileServer(http.Dir("./static/"))
r.Handle("/", &Server{r}).Handler(fs)
r.PathPrefix("/css").Handler(fs)
r.PathPrefix("/js").Handler(fs)
r.PathPrefix("/images").Handler(fs)
r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusPermanentRedirect)
})
port := os.Getenv("PORT")
if port == "" {
port = "80"
}
log.Fatal(http.ListenAndServe(":"+port, r))
}
type Server struct {
r *mux.Router
}
type ScreenshotOption struct {
url string
width int64
height int64
scrollY int64
selector string
quality int64
hide []string
background BackgroundOption
}
type BackgroundOption struct {
color color.RGBA
width int
height int
}
type ImageBuffer = []byte
const (
defaultWidth int64 = 1920
defaultHeight int64 = 1080
defaultQuality int64 = 100
defaultMinWidth int64 = 1820
defaultMinHeight int64 = 980
)
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("origin"); origin != "" {
r.Header.Set("Access-Control-Allow-Origin", origin)
r.Header.Set("Access-Control-Allow-Methods", "GET")
}
s.r.ServeHTTP(w, r)
}
func ScreenshotHandler(w http.ResponseWriter, r *http.Request) {
v := r.URL.Query()
opts := ScreenshotOption{}
if opts.url = v.Get("url"); opts.url == "" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("Bad Request: URL not found"))
return
}
quality := v.Get("quality")
if opts.quality = defaultQuality; quality != "" {
if i, err := strconv.ParseInt(quality, 10, 64); err == nil {
opts.quality = i
}
}
if hide := v.Get("hide"); hide != "" {
opts.hide = strings.Split(hide, "\r\n")
}
opts.selector = v.Get("selector")
if scrollY := v.Get("scrollY"); scrollY != "" {
if i, err := strconv.ParseInt(scrollY, 10, 64); err == nil {
opts.scrollY = i
}
}
width := v.Get("width")
if opts.width = defaultWidth; width != "" {
if i, err := strconv.ParseInt(width, 10, 64); err == nil {
opts.width = i
}
}
height := v.Get("height")
if opts.height = defaultHeight; height != "" {
if i, err := strconv.ParseInt(height, 10, 64); err == nil {
opts.height = i
}
}
if bgColor := v.Get("bgColor"); bgColor != "" {
c, err := ParseHexColor(bgColor)
if err == nil && !IsNoBackgroundColor(c) {
opts.width = defaultMinWidth
opts.height = defaultMinHeight
opts.background.color = c
opts.background.width = int(defaultWidth)
opts.background.height = int(defaultHeight)
}
}
buffer, err := TakeScreenshot(opts)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("Internal Server Error: " + err.Error()))
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(buffer)))
w.WriteHeader(http.StatusOK)
_, _ = w.Write(buffer)
}