diff --git a/remote/scores-sync-zqds/main.go b/remote/scores-sync-zqds/main.go index b582ab8f..de63571d 100644 --- a/remote/scores-sync-zqds/main.go +++ b/remote/scores-sync-zqds/main.go @@ -8,6 +8,7 @@ import ( "path" "syscall" + "golang.org/x/oauth2/clientcredentials" "gopkg.in/fsnotify.v1" ) @@ -27,9 +28,11 @@ func main() { roundId = v } + var clientId string if v, exists := os.LookupEnv("ZQDS_CLIENTID"); exists { clientId = v } + var clientSecret string if v, exists := os.LookupEnv("ZQDS_CLIENTSECRET"); exists { clientSecret = v } @@ -46,6 +49,13 @@ func main() { TeamsDir = path.Clean(TeamsDir) + configOauth = clientcredentials.Config{ + ClientID: clientId, + ClientSecret: clientSecret, + Scopes: []string{"score:update"}, + TokenURL: TokenURL, + } + log.Println("Registering directory events...") watcher, err := fsnotify.NewWatcher() if err != nil { @@ -57,8 +67,10 @@ func main() { log.Fatal(err) } - if _, err := os.Stat(path.Join(TeamsDir, "teams.json")); err == nil { - treatAll(path.Join(TeamsDir, "teams.json")) + if !skipInitialSync { + if _, err := os.Stat(path.Join(TeamsDir, "teams.json")); err == nil { + treatAll(path.Join(TeamsDir, "teams.json")) + } } // Register SIGUSR1, SIGUSR2 @@ -74,7 +86,7 @@ func main() { treatAll(path.Join(TeamsDir, "teams.json")) log.Println("SIGHUP treated.") case ev := <-watcher.Events: - if ev.Name == "teams.json" { + if path.Base(ev.Name) == "teams.json" { if ev.Op&watchedNotify == watchedNotify { if *debugINotify { log.Println("Treating event:", ev, "for", ev.Name) @@ -83,6 +95,7 @@ func main() { } else if ev.Op&fsnotify.Write == fsnotify.Write { log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.") watchedNotify = fsnotify.Write + go treatDiff(ev.Name) } else if *debugINotify { log.Println("Skipped teams.json event:", ev) } diff --git a/remote/scores-sync-zqds/treat.go b/remote/scores-sync-zqds/treat.go index 309015ff..f786a73d 100644 --- a/remote/scores-sync-zqds/treat.go +++ b/remote/scores-sync-zqds/treat.go @@ -23,9 +23,9 @@ var ( eventId = "" roundId = "" - clientId = "" - clientSecret = "" - TokenURL = "https://idp.well-played.gg/oauth/token" + TokenURL = "https://idp.well-played.gg/oauth/token" + + configOauth = clientcredentials.Config{} lock sync.Mutex ) @@ -91,21 +91,28 @@ func treat(team fic.ExportedTeam, tid string) { return } - config := clientcredentials.Config{ - ClientID: clientId, - ClientSecret: clientSecret, - Scopes: []string{"score:update"}, - TokenURL: TokenURL, - } - client := config.Client(context.Background()) + client := configOauth.Client(context.Background()) - if req, err := http.NewRequest("PUT", u.String(), bytes.NewReader(body)); err != nil { + req, err := http.NewRequest("PUT", u.String(), bytes.NewReader(body)) + if err != nil { log.Println("[ERR] Unable to send request: ", err.Error()) - } else if resp, err := client.Do(req); err != nil { + return + } + + req.Header.Add("Content-Type", "application/json") + + resp, err := client.Do(req) + if err != nil { log.Println("[ERR] Error during request execution: ", err.Error()) - } else { - log.Println(resp) - log.Println(ioutil.ReadAll(resp.Body)) + return + } + + if resp.StatusCode != http.StatusOK { + if v, err := ioutil.ReadAll(resp.Body); err != nil { + log.Println("An error occurs when trying to send scores, then decoding response: %w", err) + } else { + log.Printf("An error occurs when trying to send scores: %s", string(v)) + } } } }