diff options
Diffstat (limited to 'bitmask/main.go')
-rw-r--r-- | bitmask/main.go | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/bitmask/main.go b/bitmask/main.go index e303adb..8acab2f 100644 --- a/bitmask/main.go +++ b/bitmask/main.go @@ -45,6 +45,12 @@ func Init() (*Bitmask, error) { client := &http.Client{ Timeout: timeout, } + + err := waitForBitmaskd() + if err != nil { + return nil, err + } + apiToken, err := getToken() if err != nil { return nil, err @@ -68,6 +74,20 @@ func (b *Bitmask) Close() { } } +func waitForBitmaskd() error { + var err error + for i := 0; i < 30; i++ { + resp, err := http.Post(url, "", nil) + if err == nil { + resp.Body.Close() + return nil + } + log.Printf("Bitmask is not ready (iteration %i): %v", i, err) + time.Sleep(1 * time.Second) + } + return err +} + func (b *Bitmask) send(parts ...interface{}) (map[string]interface{}, error) { resJSON, err := send(b.apiToken, b.client, parts...) if err != nil { @@ -118,7 +138,15 @@ func parseResponse(resJSON []byte) (interface{}, error) { } func getToken() (string, error) { + var err error path := path.Join(ConfigPath, "authtoken") - b, err := ioutil.ReadFile(path) - return string(b), err + for i := 0; i < 30; i++ { + b, err := ioutil.ReadFile(path) + if err == nil { + return string(b), nil + } + log.Printf("Auth token is not ready (iteration %i): %v", i, err) + time.Sleep(1 * time.Second) + } + return "", err } |