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
No related merge requests found
...@@ -27,6 +27,18 @@ const POPULATION_DESCRIPTION = "It requires the ISO 3166-2 country code and a op ...@@ -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 = "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}" 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 FORMAT_JSON = "application/json"
const HEADER_CONTENT_TYPE = "Content-Type" 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 ...@@ -2,59 +2,43 @@ package handelers
import ( import (
"net/http" "net/http"
"fmt"
"encoding/json" "encoding/json"
"time" "time"
) )
// Function for getting status code from API
// Funksjon for å hente statuskode fra et API
func getAPIStatus(url string) (int, error) { func getAPIStatus(url string) (int, error) {
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer resp.Body.Close() defer resp.Body.Close()
// Returner HTTP-statuskoden
return resp.StatusCode, nil return resp.StatusCode, nil
} }
// Handler for Diagnostics endpoint
func DiagnosticsHandler(w http.ResponseWriter, r *http.Request, serviceStartTime time.Time) { func DiagnosticsHandler(w http.ResponseWriter, r *http.Request, serviceStartTime time.Time) {
// Hent statuskoder fra eksterne API-er countriesNowStatus, err1 := getAPIStatus(COUNTRIESNOW_API)
countriesNowStatus, err := getAPIStatus("http://129.241.150.113:3500/api/v0.1/countries") restCountriesStatus, err2 := getAPIStatus(RESTCOUNTRIES_API)
if err != nil {
http.Error(w, "Error fetching CountriesNow status", http.StatusInternalServerError)
return
}
restCountriesStatus, err := getAPIStatus("http://129.241.150.113:8080/v3.1/all") //funker med all if err1 != nil {
if err != nil { countriesNowStatus = 0
http.Error(w, "Error fetching RestCountries status", http.StatusInternalServerError)
return
} }
if err2 != nil {
// Bygg responsen med statusinformasjon restCountriesStatus = 0
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
} }
// Sett Content-Type til application/json response := map[string]any{
w.Header().Set("Content-Type", "application/json") CNA: countriesNowStatus,
RCA: restCountriesStatus,
VERSION: VERSJON,
UPTIME: getUptime(serviceStartTime) ,
}
// Returner JSON-responsen w.Header().Set(FORMAT_JSON, FORMAT_JSON)
json.NewEncoder(w).Encode(response) formattedResponse, _ := json.MarshalIndent(response, "", " ")
w.Write(formattedResponse)
} }
// Funksjon for å beregne oppetid
func getUptime(serviceStartTime time.Time) int64 { 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