1 Commits

Author SHA1 Message Date
2f65441981 Enable benchHandler on "/bench/" in addition of "/bench"
Adding the trailing slash allows to trigger the bench handler for
performance measurements in situations where a trailing slash
character '/' is added to the request.

For example, in traefik addPrefix middleware, if you specify a
"/bench" prefix, a request on "http://localhost" will be transformed
from "GET /" to "GET /bench/" with no way to remove the trailing
'/' (at least without adding another rewrite rule, which would
modify measurements).
2021-04-27 15:39:57 +02:00
7 changed files with 63 additions and 103 deletions

View File

@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
go-version: [ 1.17, 1.x ]
go-version: [ 1.14, 1.15, 1.x ]
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
@ -29,16 +29,11 @@ jobs:
- name: Cache Go modules
uses: actions/cache@v2
with:
# In order:
# * Module download cache
# * Build cache (Linux)
# * Build cache (Mac)
# * Build cache (Windows)
path: |
~/go/pkg/mod
~/.cache/go-build
~/Library/Caches/go-build
%LocalAppData%\go-build
~/go/pkg/mod # Module download cache
~/.cache/go-build # Build cache (Linux)
~/Library/Caches/go-build # Build cache (Mac)
'%LocalAppData%\go-build' # Build cache (Windows)
key: ${{ runner.os }}-${{ matrix.go-version }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-${{ matrix.go-version }}-go-

View File

@ -14,9 +14,9 @@ jobs:
name: Main Process
runs-on: ubuntu-latest
env:
GO_VERSION: 1.17
GOLANGCI_LINT_VERSION: v1.43.0
SEIHON_VERSION: v0.8.5
GO_VERSION: 1.15
GOLANGCI_LINT_VERSION: v1.33.0
SEIHON_VERSION: v0.5.1
CGO_ENABLED: 0
steps:
@ -47,6 +47,7 @@ jobs:
go mod tidy
git diff --exit-code go.mod
git diff --exit-code go.sum
go mod download
# https://golangci-lint.run/usage/install#other-ci
- name: Install golangci-lint ${{ env.GOLANGCI_LINT_VERSION }}

49
.golangci.toml Normal file
View File

@ -0,0 +1,49 @@
[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",
"nlreturn",
"testpackage",
"paralleltest",
"tparallel",
"goerr113",
"wrapcheck",
"exhaustive",
"exhaustivestruct",
]
[issues]
exclude-use-default = false
max-per-linter = 0
max-same-issues = 0
exclude = []

View File

@ -1,46 +0,0 @@
run:
deadline: 2m
skip-files: []
linters-settings:
govet:
check-shadowing: true
gocyclo:
min-complexity: 12
goconst:
min-len: 3
min-occurrences: 3
misspell:
locale: US
linters:
enable-all: true
disable:
- golint # deprecated
- scopelint # deprecated
- interfacer # deprecated
- maligned # deprecated
- lll
- gosec
- dupl
- prealloc
- gochecknoglobals
- gochecknoinits
- gomnd
- wsl
- nlreturn
- testpackage
- paralleltest
- tparallel
- goerr113
- wrapcheck
- exhaustive
- exhaustivestruct
- forbidigo
- varnamelen
- nilnil
issues:
exclude-use-default: false
max-per-linter: 0
max-same-issues: 0
exclude: []

View File

@ -22,7 +22,7 @@ Tiny Go webserver that prints os information and HTTP request to output
- `cert`: give me a certificate.
- `key`: give me a key.
- `port`: give me a port number. (it can be also defined with `WHOAMI_PORT` environment variable) (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

45
app.go
View File

@ -2,13 +2,10 @@ package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
@ -34,7 +31,6 @@ const (
var (
cert string
key string
ca string
port string
name string
)
@ -42,8 +38,7 @@ var (
func init() {
flag.StringVar(&cert, "cert", "", "give me a certificate")
flag.StringVar(&key, "key", "", "give me a key")
flag.StringVar(&ca, "cacert", "", "give me a CA chain, enforces mutual TLS")
flag.StringVar(&port, "port", getEnv("WHOAMI_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")
}
@ -58,6 +53,7 @@ func main() {
http.HandleFunc("/data", dataHandler)
http.HandleFunc("/echo", echoHandler)
http.HandleFunc("/bench", benchHandler)
http.HandleFunc("/bench/", benchHandler)
http.HandleFunc("/", whoamiHandler)
http.HandleFunc("/api", apiHandler)
http.HandleFunc("/health", healthHandler)
@ -65,38 +61,11 @@ func main() {
fmt.Println("Starting up on port " + port)
if len(cert) > 0 && len(key) > 0 {
server := &http.Server{
Addr: ":" + port,
}
if len(ca) > 0 {
server.TLSConfig = setupMutualTLS(ca)
}
log.Fatal(server.ListenAndServeTLS(cert, key))
log.Fatal(http.ListenAndServeTLS(":"+port, cert, key, nil))
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func setupMutualTLS(ca string) *tls.Config {
clientCACert, err := ioutil.ReadFile(ca)
if err != nil {
log.Fatal(err)
}
clientCertPool := x509.NewCertPool()
clientCertPool.AppendCertsFromPEM(clientCACert)
tlsConfig := &tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: clientCertPool,
PreferServerCipherSuites: true,
MinVersion: tls.VersionTLS12,
}
return tlsConfig
}
func benchHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Content-Type", "text/plain")
@ -306,11 +275,3 @@ func fillContent(length int64) io.ReadSeeker {
return bytes.NewReader(b)
}
func getEnv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}

2
go.mod
View File

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