Skip to content
Snippets Groups Projects
Commit b1eb249d authored by Andreas Magnarson Nypan's avatar Andreas Magnarson Nypan
Browse files

Upload New File

parent 00a2c1dc
No related branches found
No related tags found
No related merge requests found
Pipeline #30877 canceled
package handlers
import (
"CountryAPI/utils"
"encoding/json"
"log"
"net/http"
"sync"
"time"
)
func HandleStatus(rw http.ResponseWriter, r *http.Request) {
var wg sync.WaitGroup
var countriesNowStatus, restCountriesStatus int
urls := map[string]*int{
"http://129.241.150.113:3500/api/v0.1/countries/": &countriesNowStatus,
"http://129.241.150.113:8080/v3.1/all": &restCountriesStatus,
}
client := &http.Client{}
for url, status := range urls {
wg.Add(1)
go func(url string, status *int) {
defer wg.Done()
r, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Printf("Error creating request", url, err)
}
resp, err := client.Do(r)
if err != nil {
log.Printf("Error creating response", url, err)
}
*status = resp.StatusCode
}(url, status)
}
wg.Wait()
endpoint := map[string]interface{}{
"countriesnowapi": countriesNowStatus,
"restcountriesapi": restCountriesStatus,
"version": "v1",
"uptime": int64(time.Since(utils.StartTime).Seconds()), //using time since to check the uptime in s
}
json.NewEncoder(rw).Encode(endpoint)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment