-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhttp.go
41 lines (33 loc) · 811 Bytes
/
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
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package config
import (
"io"
"net/http"
"os"
"strings"
"time"
)
// DefaultClient used for http with a shorter timeout.
var defaultClient = &http.Client{
Timeout: 5 * time.Second,
}
// httpFile downloads a file from HTTP
var httpFile = func(url string) (*os.File, error) {
tokens := strings.Split(url, "/")
fileName := tokens[len(tokens)-1]
output, err := os.Create(fileName)
if err != nil {
return nil, err
}
defer output.Close()
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
if _, err := io.Copy(output, response.Body); err != nil {
return nil, err
}
return output, nil
}