feat: add a name to be more human friendly. (#29)

This commit is contained in:
Ludovic Fernandez
2020-03-11 13:11:36 +01:00
committed by GitHub
parent 741a054a0c
commit c4809bbe21
2 changed files with 11 additions and 0 deletions

View File

@ -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)