score-sync-zqds: It works! + some optimizations
This commit is contained in:
parent
22e25a384a
commit
debfd2a894
|
@ -8,6 +8,7 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"golang.org/x/oauth2/clientcredentials"
|
||||||
"gopkg.in/fsnotify.v1"
|
"gopkg.in/fsnotify.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,9 +28,11 @@ func main() {
|
||||||
roundId = v
|
roundId = v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var clientId string
|
||||||
if v, exists := os.LookupEnv("ZQDS_CLIENTID"); exists {
|
if v, exists := os.LookupEnv("ZQDS_CLIENTID"); exists {
|
||||||
clientId = v
|
clientId = v
|
||||||
}
|
}
|
||||||
|
var clientSecret string
|
||||||
if v, exists := os.LookupEnv("ZQDS_CLIENTSECRET"); exists {
|
if v, exists := os.LookupEnv("ZQDS_CLIENTSECRET"); exists {
|
||||||
clientSecret = v
|
clientSecret = v
|
||||||
}
|
}
|
||||||
|
@ -46,6 +49,13 @@ func main() {
|
||||||
|
|
||||||
TeamsDir = path.Clean(TeamsDir)
|
TeamsDir = path.Clean(TeamsDir)
|
||||||
|
|
||||||
|
configOauth = clientcredentials.Config{
|
||||||
|
ClientID: clientId,
|
||||||
|
ClientSecret: clientSecret,
|
||||||
|
Scopes: []string{"score:update"},
|
||||||
|
TokenURL: TokenURL,
|
||||||
|
}
|
||||||
|
|
||||||
log.Println("Registering directory events...")
|
log.Println("Registering directory events...")
|
||||||
watcher, err := fsnotify.NewWatcher()
|
watcher, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -57,8 +67,10 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(path.Join(TeamsDir, "teams.json")); err == nil {
|
if !skipInitialSync {
|
||||||
treatAll(path.Join(TeamsDir, "teams.json"))
|
if _, err := os.Stat(path.Join(TeamsDir, "teams.json")); err == nil {
|
||||||
|
treatAll(path.Join(TeamsDir, "teams.json"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register SIGUSR1, SIGUSR2
|
// Register SIGUSR1, SIGUSR2
|
||||||
|
@ -74,7 +86,7 @@ func main() {
|
||||||
treatAll(path.Join(TeamsDir, "teams.json"))
|
treatAll(path.Join(TeamsDir, "teams.json"))
|
||||||
log.Println("SIGHUP treated.")
|
log.Println("SIGHUP treated.")
|
||||||
case ev := <-watcher.Events:
|
case ev := <-watcher.Events:
|
||||||
if ev.Name == "teams.json" {
|
if path.Base(ev.Name) == "teams.json" {
|
||||||
if ev.Op&watchedNotify == watchedNotify {
|
if ev.Op&watchedNotify == watchedNotify {
|
||||||
if *debugINotify {
|
if *debugINotify {
|
||||||
log.Println("Treating event:", ev, "for", ev.Name)
|
log.Println("Treating event:", ev, "for", ev.Name)
|
||||||
|
@ -83,6 +95,7 @@ func main() {
|
||||||
} else if ev.Op&fsnotify.Write == fsnotify.Write {
|
} 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.")
|
log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.")
|
||||||
watchedNotify = fsnotify.Write
|
watchedNotify = fsnotify.Write
|
||||||
|
go treatDiff(ev.Name)
|
||||||
} else if *debugINotify {
|
} else if *debugINotify {
|
||||||
log.Println("Skipped teams.json event:", ev)
|
log.Println("Skipped teams.json event:", ev)
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,9 +23,9 @@ var (
|
||||||
eventId = ""
|
eventId = ""
|
||||||
roundId = ""
|
roundId = ""
|
||||||
|
|
||||||
clientId = ""
|
TokenURL = "https://idp.well-played.gg/oauth/token"
|
||||||
clientSecret = ""
|
|
||||||
TokenURL = "https://idp.well-played.gg/oauth/token"
|
configOauth = clientcredentials.Config{}
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
)
|
)
|
||||||
|
@ -91,21 +91,28 @@ func treat(team fic.ExportedTeam, tid string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config := clientcredentials.Config{
|
client := configOauth.Client(context.Background())
|
||||||
ClientID: clientId,
|
|
||||||
ClientSecret: clientSecret,
|
|
||||||
Scopes: []string{"score:update"},
|
|
||||||
TokenURL: TokenURL,
|
|
||||||
}
|
|
||||||
client := config.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())
|
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())
|
log.Println("[ERR] Error during request execution: ", err.Error())
|
||||||
} else {
|
return
|
||||||
log.Println(resp)
|
}
|
||||||
log.Println(ioutil.ReadAll(resp.Body))
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue