15 Commits

10 changed files with 252 additions and 65 deletions

View File

@ -12,8 +12,8 @@ jobs:
name: Main Process name: Main Process
runs-on: ubuntu-latest runs-on: ubuntu-latest
env: env:
GO_VERSION: 1.17 GO_VERSION: 1.19
GOLANGCI_LINT_VERSION: v1.46.2 GOLANGCI_LINT_VERSION: v1.50.0
CGO_ENABLED: 0 CGO_ENABLED: 0
steps: steps:
@ -32,7 +32,7 @@ jobs:
# https://github.com/marketplace/actions/cache # https://github.com/marketplace/actions/cache
- name: Cache Go modules - name: Cache Go modules
uses: actions/cache@v2 uses: actions/cache@v3
with: with:
path: ~/go/pkg/mod path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

View File

@ -15,7 +15,7 @@ jobs:
strategy: strategy:
matrix: matrix:
go-version: [ 1.17, 1.x ] go-version: [ 1.19, 1.x ]
os: [ubuntu-latest, macos-latest, windows-latest] os: [ubuntu-latest, macos-latest, windows-latest]
steps: steps:
@ -31,7 +31,7 @@ jobs:
# https://github.com/marketplace/actions/cache # https://github.com/marketplace/actions/cache
- name: Cache Go modules - name: Cache Go modules
uses: actions/cache@v2 uses: actions/cache@v3
with: with:
# In order: # In order:
# * Module download cache # * Module download cache

View File

@ -9,8 +9,7 @@ jobs:
name: Release Process name: Release Process
runs-on: ubuntu-latest runs-on: ubuntu-latest
env: env:
GO_VERSION: 1.17 GO_VERSION: 1.19
SEIHON_VERSION: v0.9.0
CGO_ENABLED: 0 CGO_ENABLED: 0
steps: steps:
@ -27,18 +26,30 @@ jobs:
with: with:
fetch-depth: 0 fetch-depth: 0
# Install Docker image multi-arch builder - name: dockerhub-login
- name: Install seihon ${{ env.SEIHON_VERSION }} uses: docker/login-action@v1
run: | with:
curl -sSfL https://raw.githubusercontent.com/ldez/seihon/master/godownloader.sh | sh -s -- -b $(go env GOPATH)/bin ${SEIHON_VERSION} username: ${{ secrets.DOCKER_USERNAME }}
seihon --version password: ${{ secrets.DOCKER_PASSWORD }}
- name: Docker Login - name: ghcr-login
uses: docker/login-action@v1
with:
registry: ghcr.io
username: traefiker
password: ${{ secrets.GHCR_TOKEN }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
# https://goreleaser.com/ci/actions/
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env: env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} GITHUB_TOKEN: ${{ secrets.GH_TOKEN_REPO }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
run: |
echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USERNAME}" --password-stdin
- name: Deploy Docker Images (seihon)
run: make publish-images

View File

@ -1,48 +1,93 @@
run: run:
deadline: 2m timeout: 2m
skip-files: [] skip-files: []
linters-settings: linters-settings:
govet: govet:
check-shadowing: true enable-all: true
disable:
- fieldalignment
gocyclo: gocyclo:
min-complexity: 12 min-complexity: 16
goconst: goconst:
min-len: 3 min-len: 3
min-occurrences: 3 min-occurrences: 3
misspell: misspell:
locale: US locale: US
funlen:
lines: -1
statements: 40
gofumpt:
extra-rules: true
depguard:
list-type: blacklist
include-go-root: false
packages:
- github.com/pkg/errors
godox:
keywords:
- FIXME
gocritic:
enabled-tags:
- diagnostic
- style
- performance
disabled-checks:
- sloppyReassign
- rangeValCopy
- octalLiteral
- paramTypeCombine # already handle by gofumpt.extra-rules
settings:
hugeParam:
sizeThreshold: 100
forbidigo:
forbid:
- '^print(ln)?$'
- '^panic$'
- '^spew\.Print(f|ln)?$'
- '^spew\.Dump$'
linters: linters:
enable-all: true enable-all: true
disable: disable:
- deadcode # deprecated
- exhaustivestruct # deprecated
- golint # deprecated - golint # deprecated
- scopelint # deprecated - ifshort # deprecated
- interfacer # deprecated - interfacer # deprecated
- maligned # deprecated - maligned # deprecated
- lll - nosnakecase # deprecated
- gosec - scopelint # deprecated
- scopelint # deprecated
- structcheck # deprecated
- varcheck # deprecated
- execinquery # not relevant (SQL)
- rowserrcheck # not relevant (SQL)
- sqlclosecheck # not relevant (SQL)
- cyclop # duplicate of gocyclo
- dupl - dupl
- prealloc - exhaustive
- exhaustruct
- forbidigo
- gochecknoglobals - gochecknoglobals
- gochecknoinits - gochecknoinits
- gomnd
- wsl
- nlreturn
- testpackage
- paralleltest
- tparallel
- goerr113 - goerr113
- wrapcheck - gomnd
- exhaustive - gosec
- exhaustivestruct - lll
- forbidigo
- varnamelen
- nilnil - nilnil
- ifshort - nlreturn
- exhaustruct - paralleltest
- prealloc
- testpackage
- tparallel
- varnamelen
- wrapcheck
- wsl
issues: issues:
exclude-use-default: false exclude-use-default: false
max-per-linter: 0 max-per-linter: 0
max-same-issues: 0 max-same-issues: 0
exclude: [] exclude:
- 'package-comments: should have a package comment'

142
.goreleaser.yml Normal file
View File

@ -0,0 +1,142 @@
project_name: whoami
builds:
- binary: whoami
goos:
- windows
- darwin
- linux
goarch:
- amd64
- arm
- arm64
goarm:
- '7'
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^doc:'
- '^chore:'
- '^chore(deps):'
- '^test:'
- '^tests:'
archives:
- id: whoami
name_template: '{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm}}v{{ .Arm }}{{ end }}'
format: tar.gz
format_overrides:
- goos: windows
format: zip
files:
- LICENSE
docker_manifests:
- name_template: 'traefik/whoami:{{ .Tag }}'
image_templates:
- 'traefik/whoami:{{ .Tag }}-amd64'
- 'traefik/whoami:{{ .Tag }}-arm64'
- 'traefik/whoami:{{ .Tag }}-armv7'
- name_template: 'traefik/whoami:latest'
image_templates:
- 'traefik/whoami:{{ .Tag }}-amd64'
- 'traefik/whoami:{{ .Tag }}-arm64'
- 'traefik/whoami:{{ .Tag }}-armv7'
- name_template: 'traefik/whoami:v{{ .Major }}.{{ .Minor }}'
image_templates:
- 'traefik/whoami:v{{ .Major }}.{{ .Minor }}-amd64'
- 'traefik/whoami:v{{ .Major }}.{{ .Minor }}-arm64'
- 'traefik/whoami:v{{ .Major }}.{{ .Minor }}-armv7'
- name_template: 'ghcr.io/traefik/whoami:{{ .Tag }}'
image_templates:
- 'ghcr.io/traefik/whoami:{{ .Tag }}-amd64'
- 'ghcr.io/traefik/whoami:{{ .Tag }}-arm64'
- 'ghcr.io/traefik/whoami:{{ .Tag }}-armv7'
- name_template: 'ghcr.io/traefik/whoami:latest'
image_templates:
- 'ghcr.io/traefik/whoami:{{ .Tag }}-amd64'
- 'ghcr.io/traefik/whoami:{{ .Tag }}-arm64'
- 'ghcr.io/traefik/whoami:{{ .Tag }}-armv7'
- name_template: 'ghcr.io/traefik/whoami:v{{ .Major }}.{{ .Minor }}'
image_templates:
- 'ghcr.io/traefik/whoami:v{{ .Major }}.{{ .Minor }}-amd64'
- 'ghcr.io/traefik/whoami:v{{ .Major }}.{{ .Minor }}-arm64'
- 'ghcr.io/traefik/whoami:v{{ .Major }}.{{ .Minor }}-armv7'
dockers:
- use: buildx
goos: linux
goarch: amd64
dockerfile: buildx.Dockerfile
image_templates:
- 'traefik/whoami:latest-amd64'
- 'traefik/whoami:{{ .Tag }}-amd64'
- 'traefik/whoami:v{{ .Major }}.{{ .Minor }}-amd64'
- 'ghcr.io/traefik/whoami:latest-amd64'
- 'ghcr.io/traefik/whoami:{{ .Tag }}-amd64'
- 'ghcr.io/traefik/whoami:v{{ .Major }}.{{ .Minor }}-amd64'
build_flag_templates:
- '--pull'
# https://github.com/opencontainers/image-spec/blob/main/annotations.md#pre-defined-annotation-keys
- '--label=org.opencontainers.image.title={{.ProjectName}}'
- '--label=org.opencontainers.image.description=Tiny Go webserver that prints OS information and HTTP request to output'
- '--label=org.opencontainers.image.source={{.GitURL}}'
- '--label=org.opencontainers.image.url={{.GitURL}}'
- '--label=org.opencontainers.image.documentation={{.GitURL}}'
- '--label=org.opencontainers.image.created={{.Date}}'
- '--label=org.opencontainers.image.revision={{.FullCommit}}'
- '--label=org.opencontainers.image.version={{.Version}}'
- '--platform=linux/amd64'
- use: buildx
goos: linux
goarch: arm64
dockerfile: buildx.Dockerfile
image_templates:
- 'traefik/whoami:latest-arm64'
- 'traefik/whoami:{{ .Tag }}-arm64'
- 'traefik/whoami:v{{ .Major }}.{{ .Minor }}-arm64'
- 'ghcr.io/traefik/whoami:latest-arm64'
- 'ghcr.io/traefik/whoami:{{ .Tag }}-arm64'
- 'ghcr.io/traefik/whoami:v{{ .Major }}.{{ .Minor }}-arm64'
build_flag_templates:
- '--pull'
# https://github.com/opencontainers/image-spec/blob/main/annotations.md#pre-defined-annotation-keys
- '--label=org.opencontainers.image.title={{.ProjectName}}'
- '--label=org.opencontainers.image.description=Tiny Go webserver that prints OS information and HTTP request to output'
- '--label=org.opencontainers.image.source={{.GitURL}}'
- '--label=org.opencontainers.image.url={{.GitURL}}'
- '--label=org.opencontainers.image.documentation={{.GitURL}}'
- '--label=org.opencontainers.image.created={{.Date}}'
- '--label=org.opencontainers.image.revision={{.FullCommit}}'
- '--label=org.opencontainers.image.version={{.Version}}'
- '--platform=linux/arm64'
- use: buildx
goos: linux
goarch: arm
goarm: '7'
dockerfile: buildx.Dockerfile
image_templates:
- 'traefik/whoami:latest-armv7'
- 'traefik/whoami:{{ .Tag }}-armv7'
- 'traefik/whoami:v{{ .Major }}.{{ .Minor }}-armv7'
- 'ghcr.io/traefik/whoami:latest-armv7'
- 'ghcr.io/traefik/whoami:{{ .Tag }}-armv7'
- 'ghcr.io/traefik/whoami:v{{ .Major }}.{{ .Minor }}-armv7'
build_flag_templates:
- '--pull'
# https://github.com/opencontainers/image-spec/blob/main/annotations.md#pre-defined-annotation-keys
- '--label=org.opencontainers.image.title={{.ProjectName}}'
- '--label=org.opencontainers.image.description=Tiny Go webserver that prints OS information and HTTP request to output'
- '--label=org.opencontainers.image.source={{.GitURL}}'
- '--label=org.opencontainers.image.url={{.GitURL}}'
- '--label=org.opencontainers.image.documentation={{.GitURL}}'
- '--label=org.opencontainers.image.created={{.Date}}'
- '--label=org.opencontainers.image.revision={{.FullCommit}}'
- '--label=org.opencontainers.image.version={{.Version}}'
- '--platform=linux/arm/v7'

View File

@ -1,6 +1,4 @@
.PHONY: default build image check publish-images .PHONY: default check test build image
TAG_NAME := $(shell git tag -l --contains HEAD)
IMAGE_NAME := traefik/whoami IMAGE_NAME := traefik/whoami
@ -17,6 +15,3 @@ check:
image: image:
docker build -t $(IMAGE_NAME) . docker build -t $(IMAGE_NAME) .
publish-images:
seihon publish -v "$(TAG_NAME)" -v "latest" --image-name $(IMAGE_NAME) --dry-run=false

View File

@ -104,6 +104,6 @@ services:
image: traefik/whoami image: traefik/whoami
command: command:
# It tells whoami to start listening on 2001 instead of 80 # It tells whoami to start listening on 2001 instead of 80
- --port 2001 - --port=2001
- --name iamfoo - --name=iamfoo
``` ```

8
app.go
View File

@ -7,7 +7,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -51,6 +50,9 @@ func init() {
var upgrader = websocket.Upgrader{ var upgrader = websocket.Upgrader{
ReadBufferSize: 1024, ReadBufferSize: 1024,
WriteBufferSize: 1024, WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
} }
func main() { func main() {
@ -85,7 +87,7 @@ func main() {
} }
func setupMutualTLS(ca string) *tls.Config { func setupMutualTLS(ca string) *tls.Config {
clientCACert, err := ioutil.ReadFile(ca) clientCACert, err := os.ReadFile(ca)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -312,7 +314,7 @@ func healthHandler(w http.ResponseWriter, req *http.Request) {
func getEnv(key, fallback string) string { func getEnv(key, fallback string) string {
value := os.Getenv(key) value := os.Getenv(key)
if len(value) == 0 { if value == "" {
return fallback return fallback
} }
return value return value

View File

@ -1,25 +1,17 @@
# syntax=docker/dockerfile:1.2
FROM golang:1-alpine as builder FROM golang:1-alpine as builder
RUN apk --no-cache --no-progress add git ca-certificates tzdata make \ RUN apk --no-cache --no-progress add git ca-certificates tzdata make \
&& update-ca-certificates \ && update-ca-certificates \
&& rm -rf /var/cache/apk/* && rm -rf /var/cache/apk/*
WORKDIR /go/whoami # syntax=docker/dockerfile:1.2
# Create a minimal container to run a Golang static binary
# 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 FROM scratch
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /go/whoami/whoami . COPY whoami /
ENTRYPOINT ["/whoami"] ENTRYPOINT ["/whoami"]
EXPOSE 80 EXPOSE 80

2
go.mod
View File

@ -1,5 +1,5 @@
module github.com/traefik/whoami module github.com/traefik/whoami
go 1.17 go 1.19
require github.com/gorilla/websocket v1.4.2 require github.com/gorilla/websocket v1.4.2