-
Notifications
You must be signed in to change notification settings - Fork 1
/
payer.go
67 lines (64 loc) · 1.79 KB
/
payer.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
package main
import (
"flag"
"github.com/ScoreTrak/ScoreTrak/pkg/auth"
"github.com/ScoreTrak/ScoreTrak/pkg/storage"
"github.com/ScoreTrak/payments/report"
"github.com/golang-jwt/jwt/v4"
authv1 "go.buf.build/grpc/go/scoretrak/scoretrakapis/scoretrak/auth/v1"
reportv1 "go.buf.build/grpc/go/scoretrak/scoretrakapis/scoretrak/report/v1"
"google.golang.org/grpc"
"log"
"net/http"
"time"
)
var conf *Config
func main() {
var err error
confPath := flag.String("config", "configs/config.yml", "Please enter a path to config file")
flag.Parse()
conf, err = NewConfig(*confPath)
if err != nil {
panic(err)
}
db, err := storage.NewDB(conf.DB)
if err != nil {
panic(err)
}
err = initTables(db)
if err != nil {
panic(err)
}
cc, err := grpc.Dial(conf.ScoreTrakAddress, grpc.WithInsecure())
if err != nil {
log.Fatal("cannot dial server: ", err)
}
authClient := authv1.NewAuthServiceClient(cc)
log.Println("Requesting authentication token")
t, err := getAuth(authClient)
if err != nil {
panic(err)
}
token, _, err := new(jwt.Parser).ParseUnverified(t, &auth.UserClaims{})
if err != nil {
panic(err)
}
claims, ok := token.Claims.(*auth.UserClaims)
if !ok {
log.Fatalf("invalid token claims")
}
log.Println("Authentication token is valid until " + claims.ExpiresAt.Time.String())
log.Println("Requesting report")
reportClient := reportv1.NewReportServiceClient(cc)
latestReport, err := getReport(reportClient, t)
if err != nil {
log.Fatalln(err)
}
log.Printf("Got report for round %d\n", latestReport.Round)
current := report.PointsPerTeam(latestReport)
previous := getMaxPointsPerTeam(db)
diff := computePointsDiff(current, previous)
client := http.Client{Timeout: time.Duration(conf.ClientTimeout) * time.Second}
makeDeposit(client, diff)
updateMaxPointsPerTeam(db, current)
}