generated from wuhan005/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
95 lines (77 loc) · 1.97 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"bytes"
"io"
"net/http"
"os"
"github.com/flamego/flamego"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
log "unknwon.dev/clog/v2"
"github.com/asoul-sig/asoul-video/pkg/model"
"github.com/asoul-sig/acao/source"
)
func main() {
defer log.Stop()
err := log.NewConsole()
if err != nil {
panic(err)
}
reportType := os.Getenv("SOURCE_REPORT_TYPE")
src, ok := source.Sources[reportType]
if !ok {
log.Fatal("Report type not found")
}
f := flamego.Classic()
f.Post("/invoke", func() {
resultChan := make(chan source.Result, 5)
go src.Scrap(resultChan)
for result := range resultChan {
if result.End {
close(resultChan)
break
}
var err error
for i := 1; i <= 5; i++ { // Retry 5 times.
log.Trace("Report data to backend...")
if err = reportData(model.ReportType(reportType), result.Data); err != nil {
log.Warn("Failed to report data: %v, retry %d / 5", err, i)
continue
}
}
if err != nil {
log.Error("Failed to report data: %v", err)
}
}
})
f.Run(9000)
}
func reportData(reportType model.ReportType, reportData jsoniter.RawMessage) error {
reportURL := os.Getenv("SOURCE_REPORT_URL")
reportKey := os.Getenv("SOURCE_REPORT_KEY")
bodyBytes, err := jsoniter.Marshal(map[string]interface{}{
"type": reportType,
"data": reportData,
})
if err != nil {
return errors.Wrap(err, "encode JSON")
}
req, err := http.NewRequest(http.MethodPost, reportURL, bytes.NewReader(bodyBytes))
if err != nil {
return errors.Wrap(err, "new request")
}
req.Header.Set("Authorization", reportKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "request")
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode/100 != 2 {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "read response body")
}
return errors.Errorf("unexpected status code %d: %q", resp.StatusCode, string(bodyBytes))
}
return nil
}