mccutchen / go-httpbin

A reasonably complete and well-tested golang port of httpbin, with zero dependencies outside the go stdlib.
https://httpbingo.org
MIT License
596 stars 124 forks source link

feat: add /env endpoint to allow exposing operator-controlled info from the server #189

Closed mloskot closed 1 month ago

mloskot commented 1 month ago

Proposal of implementation of the feature request


I've submitted this for an initial round of review. I still have to add more cases to the test and add the endpoint to the index page, but first I'd like to receive feedback if this implementation goes in the right direction.


make run

curl http://127.0.0.1:8080/env
{
  "env": {}
}

export HTTPBIN_A=a
export HTTPBIN_B=b
export HTTPBIN_123=123

make run

curl http://127.0.0.1:8080/env
{
  "env": {
    "HTTPBIN_123": "123",
    "HTTPBIN_A": "a",
    "HTTPBIN_B": "b"
  }
}
codecov[bot] commented 1 month ago

Codecov Report

Attention: Patch coverage is 86.04651% with 6 lines in your changes missing coverage. Please review.

Project coverage is 95.07%. Comparing base (34a21a3) to head (9493ea4). Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
httpbin/cmd/cmd.go 91.42% 2 Missing and 1 partial :warning:
httpbin/options.go 0.00% 3 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #189 +/- ## ========================================== - Coverage 95.20% 95.07% -0.13% ========================================== Files 10 10 Lines 1750 1766 +16 ========================================== + Hits 1666 1679 +13 - Misses 48 51 +3 Partials 36 36 ``` | [Files with missing lines](https://app.codecov.io/gh/mccutchen/go-httpbin/pull/189?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Will+McCutchen) | Coverage Δ | | |---|---|---| | [httpbin/handlers.go](https://app.codecov.io/gh/mccutchen/go-httpbin/pull/189?src=pr&el=tree&filepath=httpbin%2Fhandlers.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Will+McCutchen#diff-aHR0cGJpbi9oYW5kbGVycy5nbw==) | `99.23% <100.00%> (+<0.01%)` | :arrow_up: | | [httpbin/httpbin.go](https://app.codecov.io/gh/mccutchen/go-httpbin/pull/189?src=pr&el=tree&filepath=httpbin%2Fhttpbin.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Will+McCutchen#diff-aHR0cGJpbi9odHRwYmluLmdv) | `100.00% <100.00%> (ø)` | | | [httpbin/cmd/cmd.go](https://app.codecov.io/gh/mccutchen/go-httpbin/pull/189?src=pr&el=tree&filepath=httpbin%2Fcmd%2Fcmd.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Will+McCutchen#diff-aHR0cGJpbi9jbWQvY21kLmdv) | `89.65% <91.42%> (+0.60%)` | :arrow_up: | | [httpbin/options.go](https://app.codecov.io/gh/mccutchen/go-httpbin/pull/189?src=pr&el=tree&filepath=httpbin%2Foptions.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Will+McCutchen#diff-aHR0cGJpbi9vcHRpb25zLmdv) | `91.66% <0.00%> (-8.34%)` | :arrow_down: |
mloskot commented 1 month ago

@mccutchen

I think I'd prefer a slightly different approach where the environment is resolved at process startup and passed explicitly to httpbin.New() via a new httpbin.WithEnv(env map[string]string) option func.

This is a very good suggestion indeed. I will update this PR with the process startup approach.

mloskot commented 1 month ago

@mccutchen I've refatored and the HTTPBIN_ vars are now collected at the startup. I'll welcome any feedback - I'm available to implement any suggestions you may have.

mloskot commented 1 month ago

Pleasure is mine, thanks!