forked from rainycape/gondola
/
service.go
43 lines (40 loc) · 935 Bytes
/
service.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
package assets
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
)
// Service indicates the base URL for the assets
// service to use. POST calls will be made to:
//
// Reducer + "css"
// Reducer + "js"
// Reducer + "less"
// Reducer + "coffee"
// ...
//
// The code to reduce or compile will be sent in
// the form parameter named "code".
var Service = "http://assets.gondolaweb.com/"
func assetsService(path string, w io.Writer, r io.Reader) (int, int, error) {
code, err := ioutil.ReadAll(r)
if err != nil {
return 0, 0, err
}
form := url.Values{
"code": []string{string(code)},
}
resp, err := http.PostForm(Service+path, form)
if err != nil {
return 0, 0, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
msg, _ := ioutil.ReadAll(resp.Body)
return 0, 0, fmt.Errorf("invalid %s code: %s", path, string(msg))
}
n, err := io.Copy(w, resp.Body)
return len(code), int(n), err
}