From 7f5ef5ee207e11322909b054042d9a81e4ebfd71 Mon Sep 17 00:00:00 2001 From: zcubbs Date: Sun, 15 Oct 2023 14:13:03 +0200 Subject: [PATCH] :sparkles: Initial commit --- .editorconfig | 19 ++++++++ .github/workflows/lint.yaml | 44 +++++++++++++++++ .github/workflows/release.yaml | 81 +++++++++++++++++++++++++++++++ .github/workflows/scan.yaml | 44 +++++++++++++++++ .github/workflows/test.yaml | 47 ++++++++++++++++++ .gitignore | 2 + .golangci.yaml | 9 ++++ .gosec.config.json | 5 ++ LICENSE | 21 ++++++++ README.md | 13 +++++ Taskfile.yaml | 41 ++++++++++++++++ go.mod | 3 ++ go.sum | 51 +++++++++++++++++++ middleware.go | 89 ++++++++++++++++++++++++++++++++++ 14 files changed, 469 insertions(+) create mode 100644 .editorconfig create mode 100644 .github/workflows/lint.yaml create mode 100644 .github/workflows/release.yaml create mode 100644 .github/workflows/scan.yaml create mode 100644 .github/workflows/test.yaml create mode 100644 .gitignore create mode 100644 .golangci.yaml create mode 100644 .gosec.config.json create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Taskfile.yaml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 middleware.go diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..512c302 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,19 @@ +root = true + +[*] +insert_final_newline = true +charset = utf-8 +trim_trailing_whitespace = true +indent_style = space +indent_size = 2 + +[{Makefile,go.mod,go.sum,*.go,.gitmodules}] +indent_style = tab +indent_size = 4 + +[*.md] +indent_size = 4 +trim_trailing_whitespace = false + +[Taskfile.yaml] +indent_style = tab diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..a1267f6 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,44 @@ +name: Lint + +on: + workflow_dispatch: + pull_request: + branches: [ main ] + push: + branches: + - main + paths-ignore: + - 'README.md' + - 'docs/**' + - '.github/**' + - 'LICENSE' + - 'Taskfile.yaml' + - '.gitignore' + - '.gosec.config.json' + - '.editorconfig' + - '.goreleaser.yaml' + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - name: Checkout Code + uses: actions/checkout@v3.5.3 + + - name: Set up Go + uses: actions/setup-go@v4.1.0 + with: + go-version: '1.20' + + - name: Install Task + uses: arduino/setup-task@v1.0.3 + with: + version: 3.x + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Install Linter + run: task tools:install:golangci-lint + + - name: Lint + run: task lint diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..895c458 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,81 @@ +name: Release + +on: + workflow_dispatch: +permissions: + contents: write +jobs: + release: + if : github.triggering_actor == 'zcubbs' + + runs-on: ubuntu-latest + steps: + - name: Set Static Major and Minor Versions + id: static_version + run: | + echo "::set-output name=major::0" + echo "::set-output name=minor::8" + + - name: Get latest release version + id: latest_version + run: | + latest_tag=$(curl --silent "https://api.github.com/repos/${{ github.repository }}/releases/latest" | jq .tag_name -r) + echo "::set-output name=version::$latest_tag" + continue-on-error: true + + - name: Calculate new version + id: new_version + run: | + static_minor=${{ steps.static_version.outputs.minor }} + latest_minor=$(echo "${{ steps.latest_version.outputs.version }}" | cut -d. -f2) + patch=$(echo "${{ steps.latest_version.outputs.version }}" | cut -d. -f3 | tr -d 'v') + + if [[ "$static_minor" != "$latest_minor" ]]; then + patch=0 + else + let "patch+=1" + fi + + new_version="v${{ steps.static_version.outputs.major }}.$static_minor.${patch}" + echo "::set-output name=version::$new_version" + + - name: Checkout Code + uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0 + + - name: Set up Go + uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 + with: + go-version: '1.21' + + # remove tests in order to clean dependencies + - name: Remove xxx_test.go files + run: rm -rf *_test.go ./examples ./images + + # cleanup test dependencies + - name: Cleanup dependencies + run: go mod tidy + + - name: List files + run: tree -Cfi + - name: Write new go.mod into logs + run: cat go.mod + - name: Write new go.sum into logs + run: cat go.sum + + - name: Create tag + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + git config --global user.name '${{ github.triggering_actor }}' + git config --global user.email "${{ github.triggering_actor}}@users.noreply.github.com" + + git add . + git commit --allow-empty -m 'bump ${{ steps.new_version.outputs.version }}' + git tag ${{ steps.new_version.outputs.version }} + git push origin ${{ steps.new_version.outputs.version }} + + - name: Release + uses: softprops/action-gh-release@de2c0eb89ae2a093876385947365aca7b0e5f844 # v0.1.15 + with: + name: ${{ steps.new_version.outputs.version }} + tag_name: ${{ steps.new_version.outputs.version }} diff --git a/.github/workflows/scan.yaml b/.github/workflows/scan.yaml new file mode 100644 index 0000000..2d9e052 --- /dev/null +++ b/.github/workflows/scan.yaml @@ -0,0 +1,44 @@ +name: Scan + +on: + workflow_dispatch: + pull_request: + branches: [ main ] + push: + branches: + - main + paths-ignore: + - 'README.md' + - 'docs/**' + - '.github/**' + - 'LICENSE' + - 'Taskfile.yaml' + - '.gitignore' + - '.gosec.config.json' + - '.editorconfig' + - '.goreleaser.yaml' + +jobs: + scan: + runs-on: ubuntu-latest + + steps: + - name: Checkout Code + uses: actions/checkout@v3.5.3 + + - name: Set up Go + uses: actions/setup-go@v4.1.0 + with: + go-version: '1.20' + + - name: Install Task + uses: arduino/setup-task@v1.0.3 + with: + version: 3.x + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Install Security Scanner + run: task tools:install:gosec + + - name: Run Go Security Scanner + run: task scan diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..aa84ebb --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,47 @@ +name: Test + +on: + workflow_dispatch: + pull_request: + branches: [ main ] + push: + branches: + - main + paths-ignore: + - 'README.md' + - 'docs/**' + - '.github/**' + - 'LICENSE' + - 'Taskfile.yaml' + - '.gitignore' + - '.gosec.config.json' + - '.editorconfig' + - '.goreleaser.yaml' + +jobs: + test: + runs-on: ubuntu-latest + services: + mailhog: + image: mailhog/mailhog + ports: + - 1025:1025 + - 8025:8025 + + steps: + - name: Checkout Code + uses: actions/checkout@v3.5.3 + + - name: Set up Go + uses: actions/setup-go@v4.1.0 + with: + go-version: '1.20' + + - name: Install Task + uses: arduino/setup-task@v1.0.3 + with: + version: 3.x + repo-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Test + run: task test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c749986 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.idea/ +*.iml diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..cd83e79 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,9 @@ +linters-settings: + gosec: + # To specify a set of rules to explicitly exclude. + # Available rules: https://github.com/securego/gosec#available-rules + excludes: + - G204 # Audit use of command execution + - G304 # File path provided as taint input + - G303 # Creating tempfile using a predictable path + - G306 # Poor file permissions used when writing to a new file diff --git a/.gosec.config.json b/.gosec.config.json new file mode 100644 index 0000000..7339e33 --- /dev/null +++ b/.gosec.config.json @@ -0,0 +1,5 @@ +{ + "global": { + "exclude": "G204,G302,G303,G304,G306,G404,G402" + } +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ed0644f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Zakaria EL BOUWAB + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..320ee50 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Go K8s + +A K8s collection of Go Packages. + +[![tag](https://img.shields.io/github/tag/zcubbs/go-k8s)](https://github.com/zcubbs/go-k8s/releases) +![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.21-%23007d9c) +[![GoDoc](https://godoc.org/github.com/zcubbs/go-k8s?status.svg)](https://pkg.go.dev/github.com/zcubbs/go-k8s) +[![Lint](https://github.com/zcubbs/go-k8s/actions/workflows/lint.yaml/badge.svg)](https://github.com/zcubbs/go-k8s/actions/workflows/lint.yaml) +[![Scan](https://github.com/zcubbs/go-k8s/actions/workflows/scan.yaml/badge.svg?branch=main)](https://github.com/zcubbs/go-k8s/actions/workflows/scan.yaml) +![Build Status](https://github.com/zcubbs/go-k8s/actions/workflows/test.yaml/badge.svg) +[![Go Report Card](https://goreportcard.com/badge/github.com/zcubbs/go-k8s)](https://goreportcard.com/report/github.com/zcubbs/go-k8s) +[![Contributors](https://img.shields.io/github/contributors/zcubbs/go-k8s)](https://github.com/zcubbs/go-k8s/graphs/contributors) +[![License](https://img.shields.io/github/license/zcubbs/go-k8s.svg)](./LICENSE) diff --git a/Taskfile.yaml b/Taskfile.yaml new file mode 100644 index 0000000..47c2d68 --- /dev/null +++ b/Taskfile.yaml @@ -0,0 +1,41 @@ +version: '3' + +env: + GOOS: "{{OS}}" + +tasks: + build: + desc: Build Go Binaries + cmds: + - go build -v ./... + + test: + desc: Run tests + cmds: + - go test -v ./... + + lint: + desc: Run linter + cmds: + - golangci-lint run ./... -v --timeout 5m + + scan: + desc: Run security scan + cmds: + - gosec -conf .gosec.config.json "./..." + + tools: + desc: Install tools + cmds: + - task: tools:install:golangci-lint + - task: tools:install:gosec + + tools:install:golangci-lint: + desc: Install golangci-lint + cmds: + - go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest + + tools:install:gosec: + desc: Install gosec + cmds: + - go install github.com/securego/gosec/v2/cmd/gosec@latest diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..714751f --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module charmlogfiber + +go 1.21 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..76254df --- /dev/null +++ b/go.sum @@ -0,0 +1,51 @@ +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/lipgloss v0.8.0 h1:IS00fk4XAHcf8uZKc3eHeMUTCxUH6NkaTrdyCQk84RU= +github.com/charmbracelet/lipgloss v0.8.0/go.mod h1:p4eYUZZJ/0oXTuCQKFF8mqyKCz0ja6y+7DniDDw5KKU= +github.com/charmbracelet/log v0.2.5 h1:1yVvyKCKVV639RR4LIq1iy1Cs1AKxuNO+Hx2LJtk7Wc= +github.com/charmbracelet/log v0.2.5/go.mod h1:nQGK8tvc4pS9cvVEH/pWJiZ50eUq1aoXUOjGpXvdD0k= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= +github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/gofiber/fiber/v2 v2.49.2 h1:ONEN3/Vc+dUCxxDgZZwpqvhISgHqb+bu+isBiEyKEQs= +github.com/gofiber/fiber/v2 v2.49.2/go.mod h1:gNsKnyrmfEWFpJxQAV0qvW6l70K1dZGno12oLtukcts= +github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= +github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= +github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= +github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.49.0 h1:9FdvCpmxB74LH4dPb7IJ1cOSsluR07XG3I1txXWwJpE= +github.com/valyala/fasthttp v1.49.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/middleware.go b/middleware.go new file mode 100644 index 0000000..7ff326d --- /dev/null +++ b/middleware.go @@ -0,0 +1,89 @@ +package charmlogfiber + +import ( + "net/http" + "time" + + "github.com/charmbracelet/log" + "github.com/google/uuid" +) + +type Config struct { + DefaultLevel log.Level + ClientErrorLevel log.Level + ServerErrorLevel log.Level + + WithRequestID bool +} + +// New returns a fiber.Handler (middleware) that logs requests using slog. +// +// Requests with errors are logged using slog.Error(). +// Requests without errors are logged using slog.Info(). +func New(logger *log.Logger) fiber.Handler { + return NewWithConfig(logger, Config{ + DefaultLevel: log.InfoLevel, + ClientErrorLevel: log.WarnLevel, + ServerErrorLevel: log.ErrorLevel, + + WithRequestID: true, + }) +} + +// NewWithConfig returns a fiber.Handler (middleware) that logs requests using slog. +func NewWithConfig(logger *log.Logger, config Config) fiber.Handler { + return func(c *fiber.Ctx) error { + c.Path() + start := time.Now() + path := c.Path() + + requestID := uuid.New().String() + if config.WithRequestID { + c.Context().SetUserValue("request-id", requestID) + c.Set("X-Request-ID", requestID) + } + + err := c.Next() + + end := time.Now() + latency := end.Sub(start) + + attributes := []interface{}{ + "status", c.Response().StatusCode(), + "method", string(c.Context().Method()), + "path", path, + "ip", c.Context().RemoteIP().String(), + "latency", latency, + "user-agent", string(c.Context().UserAgent()), + "time", end, + } + + if config.WithRequestID { + attributes = append(attributes, "request-id", requestID) + } + + switch { + case c.Response().StatusCode() >= http.StatusBadRequest: + if err != nil { + attributes = append(attributes, "error", err) + } + logger.Error("Incoming request with error", + attributes..., + ) + default: + logger.Info("Incoming request", attributes...) + } + + return err + } +} + +// GetRequestID returns the request identifier +func GetRequestID(c *fiber.Ctx) string { + requestID, ok := c.Context().UserValue("request-id").(string) + if !ok { + return "" + } + + return requestID +}