Skip to content

Commit

Permalink
Added ENV dump.
Browse files Browse the repository at this point in the history
  • Loading branch information
cn0047 committed Dec 23, 2022
1 parent 4649fdb commit 3779cf0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ hrd
[![Maintainability](https://api.codeclimate.com/v1/badges/1d9d3d6acf37cde6e37e/maintainability)](https://codeclimate.com/github/cn007b/hrd/maintainability)

hrd - HTTP Request Dump.

## Usage

````sh
# basic app
docker pull ghcr.io/thepkg/hrd:v1.1.4

# with ENV dump
docker pull ghcr.io/thepkg/hrd:v1.1.5
````
28 changes: 23 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
)

const (
// header holds header delimiter, just to separate output data.
header = "===============[ HTTP REQUEST ]==============="
// header1 holds ENV header delimiter, just to separate output data.
header1 = "===============[ ENV ]==============="
// header2 holds HTTP header delimiter, just to separate output data.
header2 = "===============[ HTTP REQUEST ]==============="
)

var (
Expand Down Expand Up @@ -49,20 +51,36 @@ func runApp() {

// dumpHandler represents main HTTP handler.
func dumpHandler(w http.ResponseWriter, r *http.Request) {
// Get ENV dump for current environment.
envDump := dumpEnv()

// Get HTTP dump for current request.
dump, err := httputil.DumpRequest(r, true)
httpDump, err := httputil.DumpRequest(r, true)
if err != nil {
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
return
}

dump := fmt.Sprintf("%s \n\n %s \n\n %s \n\n %s", header1, envDump, header2, httpDump)

// Log HTTP dump just for local environment.
if AppEnv == "local" {
log.Printf("[%s] \n\n%s", header, dump)
log.Print(dump)
}

// Print HTTP dump to response.
if _, err := fmt.Fprintf(w, "%s \n\n%s", header, dump); err != nil {
if _, err := fmt.Fprint(w, dump); err != nil {
log.Printf("failed to print header, error: %#v\n", err)
}
}

// dumpEnv returns ENV dump.
func dumpEnv() string {
dump := ""

for _, e := range os.Environ() {
dump += e + "\n"
}

return dump
}

0 comments on commit 3779cf0

Please sign in to comment.