From 9a65f259f856a0bc971b1997a659866a72a3be6c Mon Sep 17 00:00:00 2001 From: Mathilde Hertaas <maimh@stud.ntnu.no> Date: Thu, 6 Mar 2025 17:30:32 +0100 Subject: [PATCH] constants and english in status --- handelers/{konstanter.go => constants.go} | 16 ++++++- handelers/statushandler.go | 52 ++++++++--------------- 2 files changed, 32 insertions(+), 36 deletions(-) rename handelers/{konstanter.go => constants.go} (77%) diff --git a/handelers/konstanter.go b/handelers/constants.go similarity index 77% rename from handelers/konstanter.go rename to handelers/constants.go index 7f9f16e..dcd066e 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 84e350b..08ca969 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 -- GitLab