-
Notifications
You must be signed in to change notification settings - Fork 3
/
location_http.go
36 lines (30 loc) · 912 Bytes
/
location_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
package tmpl
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
)
type HTTPLocation string
var isHttpLocation = regexp.MustCompile(`(?i)^https?://`)
// Load tries to GET given URL and return it's body content. The location must respond with HTTP status code 200
func (s HTTPLocation) Load() ([]byte, error) {
if res, err := http.Get(string(s)); err != nil {
return nil, fmt.Errorf("could not get \"%s\": %s", s, err)
} else if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("not ok response from \"%s\": %s", s, res.Status)
} else if raw, err := ioutil.ReadAll(res.Body); err != nil {
return nil, fmt.Errorf("could not read \"%s\": %s", s, err)
} else {
return raw, nil
}
}
func BuildHTTPLocation(location string) Location {
if isHttpLocation.MatchString(location) {
return HTTPLocation(location)
}
return nil
}
func init() {
Locations = append(Locations, BuildHTTPLocation)
}