2022-02-28 10:18:44 +03:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/json"
|
2026-07-16 10:08:54 +03:00
|
|
|
"io"
|
2022-02-28 10:18:44 +03:00
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2026-07-22 13:08:38 +03:00
|
|
|
"micrach/config"
|
2022-02-28 10:18:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Connect() {
|
2026-07-22 13:08:38 +03:00
|
|
|
body, _ := json.Marshal(map[string]string{
|
|
|
|
|
"name": config.App.Gateway.BoardName,
|
|
|
|
|
"url": config.App.Gateway.BoardUrl,
|
2022-02-28 10:18:44 +03:00
|
|
|
})
|
2026-07-22 13:08:38 +03:00
|
|
|
req, err := http.NewRequest(http.MethodPost, config.App.Gateway.Url+"/api/boards/me", bytes.NewReader(body))
|
2022-02-28 10:18:44 +03:00
|
|
|
if err != nil {
|
2026-07-22 13:08:38 +03:00
|
|
|
log.Println("gateway - connect error:", err)
|
|
|
|
|
return
|
2022-02-28 10:18:44 +03:00
|
|
|
}
|
2026-07-22 13:08:38 +03:00
|
|
|
req.Header.Set("Authorization", "Bearer "+config.App.Gateway.Token)
|
2022-02-28 10:18:44 +03:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2026-07-22 13:08:38 +03:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2022-02-28 10:18:44 +03:00
|
|
|
if err != nil {
|
2026-07-22 13:08:38 +03:00
|
|
|
log.Println("gateway - connect error:", err)
|
|
|
|
|
return
|
2022-02-28 10:18:44 +03:00
|
|
|
}
|
2026-07-16 10:08:54 +03:00
|
|
|
defer resp.Body.Close()
|
2026-07-22 13:08:38 +03:00
|
|
|
if resp.StatusCode >= 300 {
|
|
|
|
|
b, _ := io.ReadAll(resp.Body)
|
|
|
|
|
log.Println("gateway - connect failed:", resp.StatusCode, string(b))
|
|
|
|
|
return
|
2022-02-28 10:18:44 +03:00
|
|
|
}
|
|
|
|
|
log.Println("gateway - online")
|
|
|
|
|
}
|