From 77acae5381b2e9112780658e89e911fc4e16612b Mon Sep 17 00:00:00 2001 From: Tristan Morgan Date: Thu, 8 Sep 2022 10:30:22 +1000 Subject: [PATCH] Debug path. --- ASSESSMENT.md | 2 ++ doc/readme.md | 2 ++ ui/api.go | 9 ++++----- ui/ui.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/ASSESSMENT.md b/ASSESSMENT.md index 13b9ee94..9f257cb8 100644 --- a/ASSESSMENT.md +++ b/ASSESSMENT.md @@ -86,6 +86,8 @@ update `conf.toml` with database settings `/healthcheck/` - Used to validate the health of the application +`/debug` - Used to print a debug message for the application + ## Finally More details about configuring the application can be found in the [document folder](doc/config.md) diff --git a/doc/readme.md b/doc/readme.md index 698d0bb3..0fdbd7a4 100644 --- a/doc/readme.md +++ b/doc/readme.md @@ -66,6 +66,8 @@ update `conf.toml` with database settings (details on how to configure the appli `/healthcheck/` - Used to validate the health of the application +`/debug` - Used to print a debug message for the application + ## Repository structure ``` sh diff --git a/ui/api.go b/ui/api.go index d209d976..166cad9c 100644 --- a/ui/api.go +++ b/ui/api.go @@ -134,11 +134,10 @@ func addTask(cfg Config) http.Handler { // // Delete a Task by ID // -// Responses: -// -// 204: -// 404: -// 500: +// Responses: +// 204: +// 404: +// 500: func deleteTask(cfg Config) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/ui/ui.go b/ui/ui.go index edddc2dc..4d727fea 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -21,9 +21,12 @@ package ui import ( + "encoding/json" "fmt" + "io/ioutil" "net" "net/http" + "os" "time" "github.com/gorilla/mux" @@ -32,8 +35,11 @@ import ( // Config configuration for ui package type Config struct { - Assets http.FileSystem - DB db.Config + Assets http.FileSystem + DB db.Config + Hostname string + Publicip string + Timestamp int64 } // Start - start web server and handle web requets @@ -48,6 +54,8 @@ func Start(cfg Config, listener net.Listener) { mainRouter.Handle("/healthcheck", healthcheckHandler(cfg)) mainRouter.Handle("/healthcheck/", healthcheckHandler(cfg)) + mainRouter.Handle("/debug", debugHandler(cfg)) + apiRouter := mainRouter.PathPrefix("/api").Subrouter() apiHandler(cfg, apiRouter) @@ -72,3 +80,35 @@ func healthcheckHandler(cfg Config) http.Handler { fmt.Fprintf(w, "OK") }) } + +func debugHandler(cfg Config) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + + cfg.DB.DbUser = "********" + cfg.DB.DbPassword = "********" + cfg.Publicip = publicIP() + cfg.Hostname, _ = os.Hostname() + cfg.Timestamp = time.Now().Unix() + bytes, err := json.Marshal(cfg) + + if err != nil { + fmt.Fprintf(w, "{\"error\": \"JSON Marshal error\"}") + return + } + fmt.Fprintf(w, string(bytes)) + }) +} + +func publicIP() string { + url := "https://checkip.amazonaws.com/" + resp, err := http.Get(url) + if err != nil { + return "connect-error" + } + defer resp.Body.Close() + ip, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "read-error" + } + return string(ip) +}