From f61a72d0b657974eaa385aa6b9a9af63e48424c0 Mon Sep 17 00:00:00 2001 From: Mathilde Hertaas <maimh@stud.ntnu.no> Date: Wed, 5 Mar 2025 11:53:17 +0100 Subject: [PATCH] fixed handeling of correct up time count --- handelers/statushandler.go | 19 ++++++++++--------- main.go | 10 +++++++++- main/main.go | 26 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 main/main.go diff --git a/handelers/statushandler.go b/handelers/statushandler.go index 0b91a11..84e350b 100644 --- a/handelers/statushandler.go +++ b/handelers/statushandler.go @@ -2,13 +2,13 @@ package handelers import ( "net/http" - "time" "fmt" "encoding/json" + "time" ) -var serviceStartTime time.Time + // Funksjon for å hente statuskode fra et API func getAPIStatus(url string) (int, error) { @@ -22,14 +22,10 @@ func getAPIStatus(url string) (int, error) { return resp.StatusCode, nil } -// Funksjon for å beregne oppetid -func getUptime() int64 { - return int64(time.Since(serviceStartTime).Seconds()) -} + // Handler for Diagnostics endpoint -func DiagnosticsHandler(w http.ResponseWriter, r *http.Request) { - serviceStartTime = time.Now() +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 { @@ -48,7 +44,7 @@ func DiagnosticsHandler(w http.ResponseWriter, r *http.Request) { "countriesnowapi": fmt.Sprintf("%d", countriesNowStatus), "restcountriesapi": fmt.Sprintf("%d", restCountriesStatus), "version": "v1", // Versjon av API - "uptime": getUptime(), // Oppetid i sekunder + "uptime": getUptime(serviceStartTime), // Oppetid i sekunder } // Sett Content-Type til application/json @@ -56,4 +52,9 @@ func DiagnosticsHandler(w http.ResponseWriter, r *http.Request) { // Returner JSON-responsen json.NewEncoder(w).Encode(response) +} + +// Funksjon for å beregne oppetid +func getUptime(serviceStartTime time.Time) int64 { + return int64(time.Since(serviceStartTime).Seconds()) // Bruk serviceStartTime fra parameter } \ No newline at end of file diff --git a/main.go b/main.go index c437f0c..90fff95 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,19 @@ package main import ( "fmt" "net/http" + "time" "git.gvk.idi.ntnu.no/Mathilde/cloud/handelers" ) +var serviceStartTime time.Time + + func main() { - http.HandleFunc("/status/", handelers.DiagnosticsHandler) + serviceStartTime = time.Now() + + http.HandleFunc("/status/", func(w http.ResponseWriter, r *http.Request) { + handelers.DiagnosticsHandler(w, r, serviceStartTime) + }) http.HandleFunc("/", handelers.HomeHandler) http.HandleFunc("/countryinfo/v1/info/", handelers.CountryInfoHandler) diff --git a/main/main.go b/main/main.go new file mode 100644 index 0000000..9501725 --- /dev/null +++ b/main/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "net/http" + "time" + "git.gvk.idi.ntnu.no/Mathilde/cloud/handelers" +) + +var serviceStartTime time.Time + +func main() { + serviceStartTime = time.Now() + + // Send serviceStartTime til DiagnosticsHandler + http.HandleFunc("/status/", func(w http.ResponseWriter, r *http.Request) { + handelers.DiagnosticsHandler(w, r, serviceStartTime) + }) + http.HandleFunc("/", handelers.HomeHandler) + http.HandleFunc("/countryinfo/v1/info/", handelers.CountryInfoHandler) + + fmt.Println("Server running on port 8080...") + if err := http.ListenAndServe(":8080", nil); err != nil { + fmt.Printf("Server failed to start: %s\n", err) + } +} \ No newline at end of file -- GitLab