Skip to content
Snippets Groups Projects
Commit 9a65f259 authored by Mathilde Hertaas's avatar Mathilde Hertaas
Browse files

constants and english in status

parent 3bd06dcc
No related branches found
No related tags found
Loading
......@@ -27,6 +27,18 @@ const POPULATION_DESCRIPTION = "It requires the ISO 3166-2 country code and a op
const POPULATION_URL = "http://localhost:8080/countryinfo/v1/population/{ISO_3166-2_country_code}{?startYear-endYear}"
const POPULATION_URL_EXAMPLE = "http://localhost:8080/countryinfo/v1/population/NO?2010-2015}"
//information for client
//status
const CNA = "countriesnowapi"
const RCA = "restcountriesapi"
const VERSION = "version"
const UPTIME = "uptime"
const VERSJON = "v1"
const INTRO = "API status"
//information from server to client
const FORMAT_JSON = "application/json"
const HEADER_CONTENT_TYPE = "Content-Type"
\ No newline at end of file
const HEADER_CONTENT_TYPE = "Content-Type"
//extern API
const COUNTRIESNOW_API = "http://129.241.150.113:3500/api/v0.1/countries"
const RESTCOUNTRIES_API = "http://129.241.150.113:8080/v3.1/all"
\ No newline at end of file
......@@ -2,59 +2,43 @@ package handelers
import (
"net/http"
"fmt"
"encoding/json"
"time"
)
// Funksjon for å hente statuskode fra et API
// Function for getting status code from API
func getAPIStatus(url string) (int, error) {
resp, err := http.Get(url)
if err != nil {
return 0, err
}
defer resp.Body.Close()
// Returner HTTP-statuskoden
return resp.StatusCode, nil
}
// Handler for Diagnostics endpoint
func DiagnosticsHandler(w http.ResponseWriter, r *http.Request, serviceStartTime time.Time) {
// Hent statuskoder fra eksterne API-er
countriesNowStatus, err := getAPIStatus("http://129.241.150.113:3500/api/v0.1/countries")
if err != nil {
http.Error(w, "Error fetching CountriesNow status", http.StatusInternalServerError)
return
}
countriesNowStatus, err1 := getAPIStatus(COUNTRIESNOW_API)
restCountriesStatus, err2 := getAPIStatus(RESTCOUNTRIES_API)
restCountriesStatus, err := getAPIStatus("http://129.241.150.113:8080/v3.1/all") //funker med all
if err != nil {
http.Error(w, "Error fetching RestCountries status", http.StatusInternalServerError)
return
if err1 != nil {
countriesNowStatus = 0
}
// Bygg responsen med statusinformasjon
response := map[string]interface{}{
"countriesnowapi": fmt.Sprintf("%d", countriesNowStatus),
"restcountriesapi": fmt.Sprintf("%d", restCountriesStatus),
"version": "v1", // Versjon av API
"uptime": getUptime(serviceStartTime), // Oppetid i sekunder
if err2 != nil {
restCountriesStatus = 0
}
// Sett Content-Type til application/json
w.Header().Set("Content-Type", "application/json")
// Returner JSON-responsen
json.NewEncoder(w).Encode(response)
response := map[string]any{
CNA: countriesNowStatus,
RCA: restCountriesStatus,
VERSION: VERSJON,
UPTIME: getUptime(serviceStartTime) ,
}
w.Header().Set(FORMAT_JSON, FORMAT_JSON)
formattedResponse, _ := json.MarshalIndent(response, "", " ")
w.Write(formattedResponse)
}
// Funksjon for å beregne oppetid
func getUptime(serviceStartTime time.Time) int64 {
return int64(time.Since(serviceStartTime).Seconds()) // Bruk serviceStartTime fra parameter
return int64(time.Since(serviceStartTime).Seconds())
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment