Skip to content

Commit

Permalink
Merge pull request #110 from servian/debug_path
Browse files Browse the repository at this point in the history
Debug path
  • Loading branch information
tristanmorgan authored Sep 19, 2022
2 parents 86768d5 + 77acae5 commit 0be36d5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
2 changes: 2 additions & 0 deletions ASSESSMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions doc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions ui/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 42 additions & 2 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
package ui

import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"time"

"github.com/gorilla/mux"
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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)
}

0 comments on commit 0be36d5

Please # to comment.