diff --git a/handelers/konstanter.go b/handelers/constants.go similarity index 77% rename from handelers/konstanter.go rename to handelers/constants.go index 7f9f16e11be79fa6566e478c1e1e6ff7116a6b10..dcd066ea32bb48f5ba04e9c447679aa8f1b75b59 100644 --- a/handelers/konstanter.go +++ b/handelers/constants.go @@ -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 diff --git a/handelers/statushandler.go b/handelers/statushandler.go index 84e350bedce7b211520c4fd4650e86ce6c8fc43c..08ca969028f07cad4eaa1ddf0b7edd8e9b88b656 100644 --- a/handelers/statushandler.go +++ b/handelers/statushandler.go @@ -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