-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
71 lines (68 loc) · 1.55 KB
/
main.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
68
69
70
71
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"runtime"
"strconv"
"sync"
"time"
"github.com/gustavohmsilva/Praticando-Go/Aula_XIV/client/config"
"github.com/gustavohmsilva/Praticando-Go/Aula_XIV/client/dataparser"
"github.com/gustavohmsilva/Praticando-Go/Aula_XV/client/uploader"
)
func main() {
conf, err := config.Load()
if err != nil {
panic(err)
}
maxProcs, err := strconv.Atoi(conf.GetString("max_procs"))
if err != nil {
panic(err)
}
runtime.GOMAXPROCS(maxProcs)
f, err := dataparser.Read(conf.GetString("filename"))
if err != nil {
panic(err)
}
ps, err := dataparser.CSV(f, true)
if err != nil {
panic(err)
}
jsonPs, err := json.Marshal(ps)
if err != nil {
panic(err)
}
response, err := http.Post(
conf.GetString("endpoint"),
"application/json",
bytes.NewBuffer(jsonPs),
)
status := response.StatusCode
fmt.Println(status, response.Body)
path := fmt.Sprintf("%s/", conf.GetString("images_path"))
startTime := time.Now()
var wg sync.WaitGroup
wg.Add(len(ps))
for _, p := range ps {
go func(ep, p, i string, w *sync.WaitGroup) {
defer w.Done()
fileToUpload, err := uploader.NewUplRequestSrc(ep, "file", p, i)
if err != nil {
fmt.Printf(err.Error())
}
req, err := uploader.NewFileReq(fileToUpload)
if err != nil {
fmt.Printf(err.Error())
}
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf(err.Error())
}
fmt.Println(res.StatusCode, res.Body)
}(conf.GetString("image_endpoint"), path, p.Image, &wg)
}
wg.Wait()
fmt.Println(time.Since(startTime))
}