-
Notifications
You must be signed in to change notification settings - Fork 8
/
handle_explore.go
56 lines (44 loc) · 1.04 KB
/
handle_explore.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
package imageproxy
import (
"bytes"
"html/template"
"log"
"net/http"
_ "embed"
"github.com/go-chi/chi"
)
//go:embed explore.html
var explore string
func (s *Service) handleExplore() http.HandlerFunc {
ts, err := template.New("explore").Parse(explore)
if err != nil {
log.Fatalf("unable to build explore template: %s", err)
}
type templateData struct {
Req *Request
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
targetURL := chi.URLParam(r, "*")
rawQuery := r.URL.RawQuery
req, err := NewRequest(
targetURL,
SetRawQueryString(rawQuery),
SetEnableTransform(s.enableResize),
SetService(s),
)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
td := templateData{Req: req}
buf := new(bytes.Buffer)
err = ts.ExecuteTemplate(buf, "explore", td)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := buf.WriteTo(w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
}