Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
99174922cb | |||
c88b974352 | |||
affedf56a2 | |||
c0fd58d016 | |||
e5a3e4f79d | |||
245b477e37 | |||
3e0740e194 | |||
d8aeedd011 | |||
510476dca9 | |||
fa9f921632 | |||
27713d1e8d | |||
5e438f6f9e |
39
.golangci.toml
Normal file
39
.golangci.toml
Normal file
@ -0,0 +1,39 @@
|
||||
[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",
|
||||
"gas",
|
||||
"dupl",
|
||||
"prealloc",
|
||||
"gochecknoglobals",
|
||||
"gochecknoinits",
|
||||
]
|
||||
|
||||
[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.11 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
|
||||
|
44
README.md
44
README.md
@ -1,13 +1,37 @@
|
||||
# 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`: creates a response with a size `n`.
|
||||
- `/echo`: webSocket echo.
|
||||
- `/bench`: always return the same response (`1`).
|
||||
- `/`: returns the whoami information (request and network information).
|
||||
- `/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)
|
||||
|
||||
## 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 +43,23 @@ 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
|
||||
```
|
||||
|
21
app.go
21
app.go
@ -19,6 +19,7 @@ import (
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// Units
|
||||
const (
|
||||
_ = iota
|
||||
KB int64 = 1 << (10 * iota)
|
||||
@ -63,7 +64,7 @@ func main() {
|
||||
func benchHandler(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprint(w, "1")
|
||||
_, _ = fmt.Fprint(w, "1")
|
||||
}
|
||||
|
||||
func echoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@ -110,13 +111,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"))
|
||||
@ -149,7 +150,7 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
hostname, _ := os.Hostname()
|
||||
fmt.Fprintln(w, "Hostname:", hostname)
|
||||
_, _ = fmt.Fprintln(w, "Hostname:", hostname)
|
||||
|
||||
ifaces, _ := net.Interfaces()
|
||||
for _, i := range ifaces {
|
||||
@ -163,10 +164,11 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) {
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
fmt.Fprintln(w, "IP:", ip)
|
||||
_, _ = fmt.Fprintln(w, "IP:", ip)
|
||||
}
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintln(w, "RemoteAddr:", req.RemoteAddr)
|
||||
if err := req.Write(w); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@ -204,10 +206,13 @@ func apiHandler(w http.ResponseWriter, req *http.Request) {
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
if ip != nil {
|
||||
data.IP = append(data.IP, ip.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@ -244,7 +249,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,3 +1,5 @@
|
||||
module github.com/containous/whoami
|
||||
|
||||
require github.com/gorilla/websocket v1.2.0
|
||||
go 1.13
|
||||
|
||||
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