Skip to content

Commit 932937c

Browse files
committed
feat: add query parameter to print env vars
1 parent eeea0e7 commit 932937c

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ Returns the whoami information (request and network information).
1616
The optional `wait` query parameter can be provided to tell the server to wait before sending the response.
1717
The duration is expected in Go's [`time.Duration`](https://golang.org/pkg/time/#ParseDuration) format (e.g. `/?wait=100ms` to wait 100 milliseconds).
1818

19+
The optional `env` query parameter can be set to `true` to add the environment variables to the response.
20+
1921
#### `/api`
2022

2123
Returns the whoami information as JSON.
2224

25+
The optional `env` query parameter can be set to `true` to add the environment variables to the response.
26+
2327
#### `/bench`
2428

2529
Always return the same response (`1`).

app.go

+30-9
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ func init() {
5656

5757
// Data whoami information.
5858
type Data struct {
59-
Hostname string `json:"hostname,omitempty"`
60-
IP []string `json:"ip,omitempty"`
61-
Headers http.Header `json:"headers,omitempty"`
62-
URL string `json:"url,omitempty"`
63-
Host string `json:"host,omitempty"`
64-
Method string `json:"method,omitempty"`
65-
Name string `json:"name,omitempty"`
66-
RemoteAddr string `json:"remoteAddr,omitempty"`
59+
Hostname string `json:"hostname,omitempty"`
60+
IP []string `json:"ip,omitempty"`
61+
Headers http.Header `json:"headers,omitempty"`
62+
URL string `json:"url,omitempty"`
63+
Host string `json:"host,omitempty"`
64+
Method string `json:"method,omitempty"`
65+
Name string `json:"name,omitempty"`
66+
RemoteAddr string `json:"remoteAddr,omitempty"`
67+
Environ map[string]string `json:"environ,omitempty"`
6768
}
6869

6970
func main() {
@@ -207,7 +208,9 @@ func dataHandler(w http.ResponseWriter, r *http.Request) {
207208
}
208209

209210
func whoamiHandler(w http.ResponseWriter, r *http.Request) {
210-
wait := r.URL.Query().Get("wait")
211+
queryParams := r.URL.Query()
212+
213+
wait := queryParams.Get("wait")
211214
if len(wait) > 0 {
212215
duration, err := time.ParseDuration(wait)
213216
if err == nil {
@@ -231,11 +234,28 @@ func whoamiHandler(w http.ResponseWriter, r *http.Request) {
231234
http.Error(w, err.Error(), http.StatusInternalServerError)
232235
return
233236
}
237+
238+
if ok, _ := strconv.ParseBool(queryParams.Get("env")); ok {
239+
for _, env := range os.Environ() {
240+
_, _ = fmt.Fprintln(w, env)
241+
}
242+
}
234243
}
235244

236245
func apiHandler(w http.ResponseWriter, r *http.Request) {
246+
queryParams := r.URL.Query()
247+
237248
hostname, _ := os.Hostname()
238249

250+
environ := make(map[string]string)
251+
252+
if ok, _ := strconv.ParseBool(queryParams.Get("env")); ok {
253+
for _, env := range os.Environ() {
254+
before, after, _ := strings.Cut(env, "=")
255+
environ[before] = after
256+
}
257+
}
258+
239259
data := Data{
240260
Hostname: hostname,
241261
IP: getIPs(),
@@ -245,6 +265,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
245265
Method: r.Method,
246266
Name: name,
247267
RemoteAddr: r.RemoteAddr,
268+
Environ: environ,
248269
}
249270

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

0 commit comments

Comments
 (0)