feat: add query parameter to print env vars

This commit is contained in:
Fernandez Ludovic
2023-07-08 15:55:47 +02:00
parent eeea0e75c9
commit 932937c4e8
2 changed files with 34 additions and 9 deletions

View File

@ -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 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 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` #### `/api`
Returns the whoami information as JSON. 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` #### `/bench`
Always return the same response (`1`). Always return the same response (`1`).

39
app.go
View File

@ -56,14 +56,15 @@ func init() {
// Data whoami information. // Data whoami information.
type Data struct { type Data struct {
Hostname string `json:"hostname,omitempty"` Hostname string `json:"hostname,omitempty"`
IP []string `json:"ip,omitempty"` IP []string `json:"ip,omitempty"`
Headers http.Header `json:"headers,omitempty"` Headers http.Header `json:"headers,omitempty"`
URL string `json:"url,omitempty"` URL string `json:"url,omitempty"`
Host string `json:"host,omitempty"` Host string `json:"host,omitempty"`
Method string `json:"method,omitempty"` Method string `json:"method,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
RemoteAddr string `json:"remoteAddr,omitempty"` RemoteAddr string `json:"remoteAddr,omitempty"`
Environ map[string]string `json:"environ,omitempty"`
} }
func main() { func main() {
@ -207,7 +208,9 @@ func dataHandler(w http.ResponseWriter, r *http.Request) {
} }
func whoamiHandler(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 { if len(wait) > 0 {
duration, err := time.ParseDuration(wait) duration, err := time.ParseDuration(wait)
if err == nil { if err == nil {
@ -231,11 +234,28 @@ func whoamiHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return 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) { func apiHandler(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()
hostname, _ := os.Hostname() 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{ data := Data{
Hostname: hostname, Hostname: hostname,
IP: getIPs(), IP: getIPs(),
@ -245,6 +265,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
Method: r.Method, Method: r.Method,
Name: name, Name: name,
RemoteAddr: r.RemoteAddr, RemoteAddr: r.RemoteAddr,
Environ: environ,
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")