-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
58 lines (50 loc) · 1.19 KB
/
util.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
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"runtime/debug"
)
func prepareHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if h := r.Header.Get("X-Forwarded-Host"); h != "" {
baseUrl, _ = url.Parse("http://" + h)
} else {
baseUrl, _ = url.Parse("http://" + r.Host)
}
fn(w, r)
}
}
func myHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
fmt.Fprintf(os.Stderr, "%+v", err)
debug.PrintStack()
http.Error(w, http.StatusText(500), 500)
}
}()
prepareHandler(fn)(w, r)
}
}
func pathURIEscape(s string) string {
return (&url.URL{Path: s}).String()
}
func notFound(w http.ResponseWriter) {
code := http.StatusNotFound
http.Error(w, http.StatusText(code), code)
}
func badRequest(w http.ResponseWriter) {
code := http.StatusBadRequest
http.Error(w, http.StatusText(code), code)
}
func forbidden(w http.ResponseWriter) {
code := http.StatusForbidden
http.Error(w, http.StatusText(code), code)
}
func panicIf(err error) {
if err != nil {
panic(err)
}
}