4 Commits

3 changed files with 16 additions and 3 deletions

View File

@ -25,11 +25,13 @@
disable = [ disable = [
"maligned", "maligned",
"lll", "lll",
"gas", "gosec",
"dupl", "dupl",
"prealloc", "prealloc",
"gochecknoglobals", "gochecknoglobals",
"gochecknoinits", "gochecknoinits",
"gomnd",
"wsl",
] ]
[issues] [issues]

View File

@ -9,10 +9,10 @@ Tiny Go webserver that prints os information and HTTP request to output
### Paths ### Paths
- `/data?size=n`: creates a response with a size `n`. - `/data?size=n[&unit=u]`: creates a response with a size `n`. The unit of measure, if specified, accepts the following values: `KB`, `MB`, `GB`, `TB` (optional, default: bytes).
- `/echo`: webSocket echo. - `/echo`: webSocket echo.
- `/bench`: always return the same response (`1`). - `/bench`: always return the same response (`1`).
- `/`: returns the whoami information (request and network information). - `/[?wait=d]`: 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).
- `/api`: returns the whoami information as JSON. - `/api`: returns the whoami information as JSON.
- `/health`: heath check - `/health`: heath check
- `GET`, `HEAD`, ...: returns a response with the status code defined by the `POST` - `GET`, `HEAD`, ...: returns a response with the status code defined by the `POST`
@ -23,6 +23,7 @@ Tiny Go webserver that prints os information and HTTP request to output
- `cert`: give me a certificate. - `cert`: give me a certificate.
- `key`: give me a key. - `key`: give me a key.
- `port`: give me a port number. (default: 80) - `port`: give me a port number. (default: 80)
- `name`: give me a name. (it can be also defined with `WHOAMI_NAME` environment variable)
## Examples ## Examples
@ -63,3 +64,7 @@ $ curl -v http://localhost:80/health
< Date: Mon, 16 Sep 2019 22:52:40 GMT < Date: Mon, 16 Sep 2019 22:52:40 GMT
< Content-Length: 0 < Content-Length: 0
``` ```
```console
docker run -d -P -v ./certs:/certs --name iamfoo containous/whoami --cert /certs/cert.cer --key /certs/key.key
```

6
app.go
View File

@ -31,11 +31,13 @@ const (
var cert string var cert string
var key string var key string
var port string var port string
var name string
func init() { func init() {
flag.StringVar(&cert, "cert", "", "give me a certificate") flag.StringVar(&cert, "cert", "", "give me a certificate")
flag.StringVar(&key, "key", "", "give me a key") flag.StringVar(&key, "key", "", "give me a key")
flag.StringVar(&port, "port", "80", "give me a port number") flag.StringVar(&port, "port", "80", "give me a port number")
flag.StringVar(&name, "name", os.Getenv("WHOAMI_NAME"), "give me a name")
} }
var upgrader = websocket.Upgrader{ var upgrader = websocket.Upgrader{
@ -149,6 +151,10 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) {
} }
} }
if name != "" {
_, _ = fmt.Fprintln(w, "Name:", name)
}
hostname, _ := os.Hostname() hostname, _ := os.Hostname()
_, _ = fmt.Fprintln(w, "Hostname:", hostname) _, _ = fmt.Fprintln(w, "Hostname:", hostname)