Select Git revision
proguard-rules.pro
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
diag.go 1.25 KiB
package pkg
import (
"fmt"
"net/http"
"time"
)
const gbifApi = "http://api.gbif.org/v1/"
const restApi = "https://restcountries.eu/rest/v2"
// starts counting time since app deployment
var startTime time.Time
func init() {
startTime = time.Now()
}
type Diag struct {
Gbif int `json:"gbif,omitempty"`
Restcountries int `json:"restcountries,omitempty"`
Version string `json:"version,omitempty"`
Uptime int `json:"uptime,omitempty"`
}
func GetDiag(d* Diag) error {
if err := getGbifStatus(d); err != nil{
return fmt.Errorf("error occured while contacting GBIF API: %s", err)
}
if err := getRestStatus(d); err != nil {
return fmt.Errorf("error occured while contacting Restcountries API: %s", err)
}
d.Version = "v1"
getUptime(d)
return nil
}
// Gets GBIF-api status code
func getGbifStatus(d *Diag) error {
resp, err := http.Get(gbifApi)
if err != nil {
return err
}
d.Gbif = resp.StatusCode
return nil
}
// Gets country-api status code
func getRestStatus(d *Diag) error {
resp, err := http.Get(restApi)
if err != nil {
return err
}
d.Restcountries = resp.StatusCode
return nil
}
// Returns application uptime in seconds
func getUptime(d *Diag) {
up := time.Since(startTime).Seconds()
d.Uptime = int(up)
}