-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (42 loc) · 1.1 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
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/luisgomez29/webscreenshot/screenshot"
"log"
"net/http"
"net/url"
)
func main() {
r := mux.NewRouter().StrictSlash(true)
r.HandleFunc("/screenshot", ScreenshotHandler).Queries("url", "{url}").Methods("GET")
fmt.Println("server listening on port :8000")
log.Fatal(http.ListenAndServe(":8000", r))
}
// Response to requests
func response(w http.ResponseWriter, errorCode int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(errorCode)
json.NewEncoder(w).Encode(data)
}
func ScreenshotHandler(w http.ResponseWriter, r *http.Request) {
rawURL, err := url.ParseRequestURI(r.URL.Query().Get("url"))
if err != nil {
response(w, http.StatusBadRequest, map[string]string{
"error": "invalid URL",
})
return
}
img, err := screenshot.GenerateScreenshot(rawURL)
if err != nil {
response(w, http.StatusBadRequest, map[string]string{
"error": "invalid URL",
})
return
}
response(w, http.StatusOK, map[string]string{
"screenshot": img,
"url": rawURL.String(),
})
}