Skip to content

Commit

Permalink
feat: add query parameter to print env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jul 8, 2023
1 parent eeea0e7 commit 932937c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ Returns the whoami information (request and network information).
The optional `wait` query parameter can be provided to tell the server to wait before sending the response.
The duration is expected in Go's [`time.Duration`](https://golang.org/pkg/time/#ParseDuration) format (e.g. `/?wait=100ms` to wait 100 milliseconds).

The optional `env` query parameter can be set to `true` to add the environment variables to the response.

#### `/api`

Returns the whoami information as JSON.

The optional `env` query parameter can be set to `true` to add the environment variables to the response.

#### `/bench`

Always return the same response (`1`).
Expand Down
39 changes: 30 additions & 9 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ func init() {

// Data whoami information.
type Data struct {
Hostname string `json:"hostname,omitempty"`
IP []string `json:"ip,omitempty"`
Headers http.Header `json:"headers,omitempty"`
URL string `json:"url,omitempty"`
Host string `json:"host,omitempty"`
Method string `json:"method,omitempty"`
Name string `json:"name,omitempty"`
RemoteAddr string `json:"remoteAddr,omitempty"`
Hostname string `json:"hostname,omitempty"`
IP []string `json:"ip,omitempty"`
Headers http.Header `json:"headers,omitempty"`
URL string `json:"url,omitempty"`
Host string `json:"host,omitempty"`
Method string `json:"method,omitempty"`
Name string `json:"name,omitempty"`
RemoteAddr string `json:"remoteAddr,omitempty"`
Environ map[string]string `json:"environ,omitempty"`
}

func main() {
Expand Down Expand Up @@ -207,7 +208,9 @@ func dataHandler(w http.ResponseWriter, r *http.Request) {
}

func whoamiHandler(w http.ResponseWriter, r *http.Request) {
wait := r.URL.Query().Get("wait")
queryParams := r.URL.Query()

wait := queryParams.Get("wait")
if len(wait) > 0 {
duration, err := time.ParseDuration(wait)
if err == nil {
Expand All @@ -231,11 +234,28 @@ func whoamiHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if ok, _ := strconv.ParseBool(queryParams.Get("env")); ok {
for _, env := range os.Environ() {
_, _ = fmt.Fprintln(w, env)
}
}
}

func apiHandler(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()

hostname, _ := os.Hostname()

environ := make(map[string]string)

if ok, _ := strconv.ParseBool(queryParams.Get("env")); ok {
for _, env := range os.Environ() {
before, after, _ := strings.Cut(env, "=")
environ[before] = after
}
}

data := Data{
Hostname: hostname,
IP: getIPs(),
Expand All @@ -245,6 +265,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
Method: r.Method,
Name: name,
RemoteAddr: r.RemoteAddr,
Environ: environ,
}

w.Header().Set("Content-Type", "application/json")
Expand Down

0 comments on commit 932937c

Please sign in to comment.