Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
c4809bbe21 | |||
741a054a0c | |||
9bf5b44f24 | |||
bf0a64d4c2 | |||
99174922cb | |||
c88b974352 | |||
affedf56a2 | |||
c0fd58d016 | |||
e5a3e4f79d | |||
245b477e37 | |||
3e0740e194 | |||
d8aeedd011 | |||
510476dca9 | |||
fa9f921632 |
41
.golangci.toml
Normal file
41
.golangci.toml
Normal file
@ -0,0 +1,41 @@
|
||||
[run]
|
||||
deadline = "2m"
|
||||
skip-files = []
|
||||
|
||||
[linters-settings]
|
||||
|
||||
[linters-settings.govet]
|
||||
check-shadowing = true
|
||||
|
||||
[linters-settings.gocyclo]
|
||||
min-complexity = 12.0
|
||||
|
||||
[linters-settings.maligned]
|
||||
suggest-new = true
|
||||
|
||||
[linters-settings.goconst]
|
||||
min-len = 3.0
|
||||
min-occurrences = 3.0
|
||||
|
||||
[linters-settings.misspell]
|
||||
locale = "US"
|
||||
|
||||
[linters]
|
||||
enable-all = true
|
||||
disable = [
|
||||
"maligned",
|
||||
"lll",
|
||||
"gosec",
|
||||
"dupl",
|
||||
"prealloc",
|
||||
"gochecknoglobals",
|
||||
"gochecknoinits",
|
||||
"gomnd",
|
||||
"wsl",
|
||||
]
|
||||
|
||||
[issues]
|
||||
exclude-use-default = false
|
||||
max-per-linter = 0
|
||||
max-same-issues = 0
|
||||
exclude = []
|
53
.travis.yml
Normal file
53
.travis.yml
Normal file
@ -0,0 +1,53 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- "1.x"
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $GOPATH/pkg/mod
|
||||
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
- /^v\d+\.\d+(\.\d+)?(-\S*)?$/
|
||||
|
||||
notifications:
|
||||
email:
|
||||
on_success: never
|
||||
on_failure: change
|
||||
|
||||
env:
|
||||
- GO111MODULE=on
|
||||
|
||||
before_install:
|
||||
# Install linters and misspell
|
||||
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin ${GOLANGCI_LINT_VERSION}
|
||||
- golangci-lint --version
|
||||
# Install Docker image multi-arch builder
|
||||
- curl -sfL https://raw.githubusercontent.com/ldez/seihon/master/godownloader.sh | bash -s -- -b "${GOPATH}/bin" ${SEIHON_VERSION}
|
||||
- seihon --version
|
||||
|
||||
install:
|
||||
- go mod tidy
|
||||
- git diff --exit-code go.mod
|
||||
- git diff --exit-code go.sum
|
||||
- go mod download
|
||||
|
||||
script:
|
||||
- make
|
||||
- make image
|
||||
|
||||
before_deploy:
|
||||
- >
|
||||
if ! [ "$BEFORE_DEPLOY_RUN" ]; then
|
||||
export BEFORE_DEPLOY_RUN=1;
|
||||
echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin
|
||||
fi
|
||||
|
||||
deploy:
|
||||
- provider: script
|
||||
skip_cleanup: true
|
||||
script: make publish-images
|
||||
on:
|
||||
tags: true
|
18
Dockerfile
18
Dockerfile
@ -1,10 +1,26 @@
|
||||
FROM golang:1.12 as builder
|
||||
FROM golang:1-alpine as builder
|
||||
|
||||
RUN apk --no-cache --no-progress add git ca-certificates tzdata make \
|
||||
&& update-ca-certificates \
|
||||
&& rm -rf /var/cache/apk/*
|
||||
|
||||
WORKDIR /go/whoami
|
||||
|
||||
# Download go modules
|
||||
COPY go.mod .
|
||||
COPY go.sum .
|
||||
RUN GO111MODULE=on GOPROXY=https://proxy.golang.org go mod download
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN make build
|
||||
|
||||
# Create a minimal container to run a Golang static binary
|
||||
FROM scratch
|
||||
|
||||
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
|
||||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||
COPY --from=builder /go/whoami/whoami .
|
||||
|
||||
ENTRYPOINT ["/whoami"]
|
||||
EXPOSE 80
|
||||
|
18
Makefile
18
Makefile
@ -1,12 +1,20 @@
|
||||
.PHONY: default build dependencies image
|
||||
.PHONY: default build image check publish-images
|
||||
|
||||
default: build
|
||||
TAG_NAME := $(shell git tag -l --contains HEAD)
|
||||
|
||||
default: check test build
|
||||
|
||||
test:
|
||||
go test -v -cover ./...
|
||||
|
||||
build:
|
||||
CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags="-s" -o whoami
|
||||
|
||||
dependencies:
|
||||
go mod download
|
||||
|
||||
image:
|
||||
docker build -t containous/whoami .
|
||||
|
||||
check:
|
||||
golangci-lint run
|
||||
|
||||
publish-images:
|
||||
seihon publish -v "$(TAG_NAME)" -v "latest" --image-name containous/whoami --dry-run=false
|
||||
|
49
README.md
49
README.md
@ -1,13 +1,38 @@
|
||||
# whoami
|
||||
|
||||
[](https://hub.docker.com/r/containous/whoami/)
|
||||
[](https://travis-ci.com/containous/whoami)
|
||||
|
||||
Tiny Go webserver that prints os information and HTTP request to output
|
||||
|
||||
## Usage
|
||||
|
||||
### Paths
|
||||
|
||||
- `/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.
|
||||
- `/bench`: always return the same response (`1`).
|
||||
- `/[?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.
|
||||
- `/health`: heath check
|
||||
- `GET`, `HEAD`, ...: returns a response with the status code defined by the `POST`
|
||||
- `POST`: changes the status code of the `GET` (`HEAD`, ...) response.
|
||||
|
||||
### Flags
|
||||
|
||||
- `cert`: give me a certificate.
|
||||
- `key`: give me a key.
|
||||
- `port`: give me a port number. (default: 80)
|
||||
- `name`: give me a name. (it can be also defined with `WHOAMI_NAME` environment variable)
|
||||
|
||||
## Examples
|
||||
|
||||
```console
|
||||
$ docker run -d -P --name iamfoo containous/whoami
|
||||
|
||||
$ docker inspect --format '{{ .NetworkSettings.Ports }}' iamfoo
|
||||
map[80/tcp:[{0.0.0.0 32769}]]
|
||||
|
||||
$ curl "http://0.0.0.0:32769"
|
||||
Hostname : 6e0030e67d6a
|
||||
IP : 127.0.0.1
|
||||
@ -19,3 +44,27 @@ Host: 0.0.0.0:32769
|
||||
User-Agent: curl/7.35.0
|
||||
Accept: */*
|
||||
```
|
||||
|
||||
```console
|
||||
# updates health check status
|
||||
$ curl -X POST -d '500' http://localhost:80/health
|
||||
|
||||
# calls the health check
|
||||
$ curl -v http://localhost:80/health
|
||||
* Trying ::1:80...
|
||||
* TCP_NODELAY set
|
||||
* Connected to localhost (::1) port 80 (#0)
|
||||
> GET /health HTTP/1.1
|
||||
> Host: localhost:80
|
||||
> User-Agent: curl/7.65.3
|
||||
> Accept: */*
|
||||
>
|
||||
* Mark bundle as not supporting multiuse
|
||||
< HTTP/1.1 500 Internal Server Error
|
||||
< Date: Mon, 16 Sep 2019 22:52:40 GMT
|
||||
< Content-Length: 0
|
||||
```
|
||||
|
||||
```console
|
||||
docker run -d -P -v ./certs:/certs --name iamfoo containous/whoami --cert /certs/cert.cer --key /certs/key.key
|
||||
```
|
||||
|
18
app.go
18
app.go
@ -19,6 +19,7 @@ import (
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// Units
|
||||
const (
|
||||
_ = iota
|
||||
KB int64 = 1 << (10 * iota)
|
||||
@ -30,11 +31,13 @@ const (
|
||||
var cert string
|
||||
var key string
|
||||
var port string
|
||||
var name string
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&cert, "cert", "", "give me a certificate")
|
||||
flag.StringVar(&key, "key", "", "give me a key")
|
||||
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{
|
||||
@ -110,13 +113,13 @@ func dataHandler(w http.ResponseWriter, r *http.Request) {
|
||||
unit := queryParams.Get("unit")
|
||||
switch strings.ToLower(unit) {
|
||||
case "kb":
|
||||
size = size * KB
|
||||
size *= KB
|
||||
case "mb":
|
||||
size = size * MB
|
||||
size *= MB
|
||||
case "gb":
|
||||
size = size * GB
|
||||
size *= GB
|
||||
case "tb":
|
||||
size = size * TB
|
||||
size *= TB
|
||||
}
|
||||
|
||||
attachment, err := strconv.ParseBool(queryParams.Get("attachment"))
|
||||
@ -148,6 +151,10 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if name != "" {
|
||||
_, _ = fmt.Fprintln(w, "Name:", name)
|
||||
}
|
||||
|
||||
hostname, _ := os.Hostname()
|
||||
_, _ = fmt.Fprintln(w, "Hostname:", hostname)
|
||||
|
||||
@ -211,6 +218,7 @@ func apiHandler(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@ -247,7 +255,7 @@ func healthHandler(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
func fillContent(length int64) io.ReadSeeker {
|
||||
charset := "-ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
b := make([]byte, length, length)
|
||||
b := make([]byte, length)
|
||||
|
||||
for i := range b {
|
||||
b[i] = charset[i%len(charset)]
|
||||
|
4
go.mod
4
go.mod
@ -1,5 +1,5 @@
|
||||
module github.com/containous/whoami
|
||||
|
||||
go 1.12
|
||||
go 1.13
|
||||
|
||||
require github.com/gorilla/websocket v1.2.0
|
||||
require github.com/gorilla/websocket v1.4.1
|
||||
|
4
go.sum
4
go.sum
@ -1,2 +1,2 @@
|
||||
github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ=
|
||||
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
|
||||
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
|
24
tmpl.Dockerfile
Normal file
24
tmpl.Dockerfile
Normal file
@ -0,0 +1,24 @@
|
||||
FROM golang:1-alpine as builder
|
||||
|
||||
RUN apk --no-cache --no-progress add git ca-certificates tzdata make \
|
||||
&& update-ca-certificates \
|
||||
&& rm -rf /var/cache/apk/*
|
||||
|
||||
WORKDIR /go/whoami
|
||||
|
||||
# Download go modules
|
||||
COPY go.mod .
|
||||
COPY go.sum .
|
||||
RUN GO111MODULE=on GOPROXY=https://proxy.golang.org go mod download
|
||||
|
||||
COPY . .
|
||||
RUN GOARCH={{ .GoARCH }} GOARM={{ .GoARM }} make build
|
||||
|
||||
FROM scratch
|
||||
|
||||
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
|
||||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||
COPY --from=builder /go/whoami/whoami .
|
||||
|
||||
ENTRYPOINT ["/whoami"]
|
||||
EXPOSE 80
|
Reference in New Issue
Block a user