forked from evergreen-ci/evergreen
/
http.go
67 lines (61 loc) · 1.93 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
package util
import (
"crypto/tls"
"encoding/json"
"github.com/gorilla/mux"
"net/http"
"strings"
)
// WriteJSON writes a json response with the supplied code on the given writer.
func WriteJSON(w *http.ResponseWriter, data interface{}, status int) {
// write the response
(*w).Header().Add("Content-Type", "application/json")
(*w).Header().Add("Connection", "close")
jsonBytes, err := json.Marshal(data)
if err != nil {
(*w).WriteHeader(500)
(*w).Write([]byte("{}"))
return
}
(*w).WriteHeader(status)
(*w).Write([]byte(jsonBytes))
}
// MakeTlsConfig creates a TLS Config from a certificate and key.
func MakeTlsConfig(cert string, key string) (*tls.Config, error) {
// Adapted from http.ListenAndServeTLS
tlsConfig := &tls.Config{}
tlsConfig.NextProtos = []string{"http/1.1"}
tlsConfig.Certificates = make([]tls.Certificate, 1)
var err error
tlsConfig.Certificates[0], err =
tls.X509KeyPair([]byte(cert), []byte(key))
if err != nil {
return nil, err
}
return tlsConfig, nil
}
// MountHandler routes all requests to the given mux.Router under the prefix to be handled by
// the http.Handler, which the request's path rooted under that prefix.
// So for example, if a router configured with the path /foo is given to
// MountHandler(r, "/bar/baz", newHandler)
// Then a request to the router at /foo/bar/baz/hello will be handled by newHandler,
// appearing with the path "/baz/hello"
func MountHandler(r *mux.Router, prefix string, h http.Handler) http.Handler {
root := r.PathPrefix(prefix)
root.Handler(
http.HandlerFunc(func(w http.ResponseWriter, rq *http.Request) {
flattened := make([]string, 0)
for k, v := range mux.Vars(rq) {
flattened = append(flattened, k)
flattened = append(flattened, v)
}
url, err := root.URL(flattened...)
if err != nil {
http.NotFound(w, rq)
return
}
strip := strings.TrimSuffix(url.String(), "/")
http.StripPrefix(strip, h).ServeHTTP(w, rq)
}))
return r
}