diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..2b40712 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,17 @@ +--- +# To get started with Dependabot version updates, you'll need to specify which +# package ecosystems to update and where the package manifests are located. +# Please see the documentation for all configuration options: +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "daily" + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/linters/.ecrc b/.github/linters/.ecrc new file mode 100644 index 0000000..d660631 --- /dev/null +++ b/.github/linters/.ecrc @@ -0,0 +1,18 @@ +{ + "Verbose": false, + "Debug": true, + "IgnoreDefaults": false, + "SpacesAftertabs": false, + "NoColor": false, + "Exclude": ["testfiles", "README.md", "doc.go", "docs.go", "\\.tpl"], + "AllowedContentTypes": [], + "PassedFiles": [], + "Disable": { + "EndOfLine": false, + "Indentation": false, + "InsertFinalNewline": false, + "TrimTrailingWhitespace": false, + "IndentSize": false, + "MaxLineLength": false + } +} diff --git a/.github/linters/.golangci.yml b/.github/linters/.golangci.yml new file mode 100644 index 0000000..e137b79 --- /dev/null +++ b/.github/linters/.golangci.yml @@ -0,0 +1,131 @@ +--- +# configure golangci-lint +# based on https://github.com/github/super-linter/blob/main/TEMPLATES/.golangci.yml +linters: + # see https://golangci-lint.run/usage/linters/ + enable: + - gosec + - unconvert + - goconst + - goimports + - gocritic + - govet + - revive + - gofmt + - gci + - gosimple + - ineffassign + - gocheckcompilerdirectives + - sloglint + - unconvert + - whitespace +linters-settings: + errcheck: + # report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`; + # default is false: such cases aren't reported by default. + check-blank: true + # List of functions to exclude from checking, where each entry is a single function to exclude. + # See https://github.com/kisielk/errcheck#excluding-functions for details. + exclude-functions: + - io.Copy + - fmt.Printf + - bytes.Buffer.Write + maligned: + # print struct with more effective memory layout or not, false by default + suggest-new: true + revive: + # disable all rules by default in case new linters will be added + # enable this flag from time to time to see if we should fix/add more here + enable-all-rules: false + # see https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md + rules: + - name: exported + disabled: false + arguments: + - disableStutteringCheck + - name: import-alias-naming + disabled: false + arguments: ['^[a-z][a-zA-Z0-9]{0,}$'] + - name: context-as-argument + disabled: false + - name: import-shadowing + disabled: false + - name: var-declaration + disabled: false + - name: use-any + disabled: false + - name: cognitive-complexity + disabled: false + arguments: [30] + - name: cyclomatic + disabled: false + arguments: [30] + - name: line-length-limit + disabled: false + arguments: [180] + - name: comment-spacings + disabled: false + - name: increment-decrement + disabled: false + - name: unhandled-error + disabled: false + arguments: + - 'fmt.Printf' + - 'io.Copy' + - 'bytes.Buffer.Write' + - name: var-naming + disabled: false + + - name: add-constant + disabled: true + - name: bare-return + disabled: true + - name: flag-parameter + disabled: true + - name: function-length + disabled: true + - name: unused-receiver + disabled: true + - name: max-public-structs + disabled: true + - name: unchecked-type-assertion + disabled: true + - name: function-result-limit + disabled: true + - name: confusing-naming + disabled: true + - name: get-return + disabled: true + - name: nested-structs + disabled: true + - name: argument-limit + disabled: true + gci: + custom-order: true + sections: + - standard + - default + - prefix(github.com/WildEgor/e-shop-fiber-microservice-boilerplate) + staticcheck: + checks: + - all + +issues: + exclude-dirs: + - api + - vendor + - generated + exclude-files: + - .goreleaser.yml + exclude-rules: + - path: _test\.go + linters: + - dupl + - gosec + - goconst + - staticcheck + include: + # based on the + # https://www.pixelstech.net/article/1650069870-golangci-lint-to-enable-comment-check-for-exported-functions + - EXC0012 # EXC0012 revive: Annoying issue about not having a comment. The rare codebase has such comments + - EXC0014 # EXC0014 revive: Annoying issue about not having a comment. The rare codebase has such comments diff --git a/.github/linters/.hadolint.yaml b/.github/linters/.hadolint.yaml new file mode 100644 index 0000000..8edb921 --- /dev/null +++ b/.github/linters/.hadolint.yaml @@ -0,0 +1,10 @@ +--- +########################## +## Hadolint config file ## +########################## + +failure-threshold: warning + +ignored: + # Pin versions in apt get install. Instead of `apt-get install ` use `apt-get install =` + - DL3008 diff --git a/.github/linters/.jscpd.json b/.github/linters/.jscpd.json new file mode 100644 index 0000000..202d927 --- /dev/null +++ b/.github/linters/.jscpd.json @@ -0,0 +1,11 @@ +{ + "threshold": 5, + "reporters": [ + "consoleFull" + ], + "ignore": [ + "**/*_test.go", + "**/testing/mock.go" + ], + "absolute": true +} diff --git a/.github/linters/.markdown-lint.yml b/.github/linters/.markdown-lint.yml new file mode 100644 index 0000000..ad2ef77 --- /dev/null +++ b/.github/linters/.markdown-lint.yml @@ -0,0 +1,39 @@ +--- +########################### +########################### +## Markdown Linter rules ## +########################### +########################### + +# Linter rules doc: +# - https://github.com/DavidAnson/markdownlint +# +# Note: +# To comment out a single error: +# +# any violations you want +# +# + +############### +# Rules by id # +############### +MD004: false # Unordered list style +MD007: + indent: 2 # Unordered list indentation +MD010: + code_blocks: false # Ignore no-hard-tabs in cde blocks because that's how formatting for golang works +MD013: + line_length: 120 # Line length 80 is far too short +MD024: + siblings_only: true # Allow the same headers for different sections +MD026: + punctuation: ".,;:!。,;:" # List of not allowed +MD029: false # Ordered list item prefix +MD033: false # Allow inline HTML +MD036: false # Emphasis used instead of a heading + +################# +# Rules by tags # +################# +blank_lines: false # Error on blank lines diff --git a/.github/linters/.yaml-lint.yml b/.github/linters/.yaml-lint.yml new file mode 100644 index 0000000..5097912 --- /dev/null +++ b/.github/linters/.yaml-lint.yml @@ -0,0 +1,59 @@ +--- +########################################### +# These are the rules used for # +# linting all the yaml files in the stack # +# NOTE: # +# You can disable line with: # +# # yamllint disable-line # +########################################### +rules: + braces: + level: warning + min-spaces-inside: 0 + max-spaces-inside: 1 + min-spaces-inside-empty: 1 + max-spaces-inside-empty: 5 + brackets: + level: warning + min-spaces-inside: 0 + max-spaces-inside: 1 + min-spaces-inside-empty: 1 + max-spaces-inside-empty: 5 + colons: + level: warning + max-spaces-before: 0 + max-spaces-after: 1 + commas: + level: warning + max-spaces-before: 0 + min-spaces-after: 1 + max-spaces-after: 1 + comments: disable + comments-indentation: disable + document-end: disable + document-start: + level: warning + present: true + empty-lines: + level: warning + max: 2 + max-start: 0 + max-end: 0 + hyphens: + level: warning + max-spaces-after: 1 + indentation: + level: warning + spaces: consistent + indent-sequences: true + check-multi-line-strings: false + key-duplicates: enable + line-length: + level: warning + max: 120 + allow-non-breakable-words: true + allow-non-breakable-inline-mappings: true + new-line-at-end-of-file: disable + new-lines: + type: unix + trailing-spaces: disable diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..c568fd2 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,63 @@ +--- +name: "Lint" + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +on: + push: + branches: [ main, develop ] + pull_request: + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + - name: Check out code + uses: actions/checkout@v4 + with: + # Full git history is needed to get a proper list of changed files within `super-linter` + fetch-depth: 0 + + - name: Lint Code Base + uses: github/super-linter@v6 + env: + VALIDATE_ALL_CODEBASE: ${{ github.event_name != 'pull_request' }} + DEFAULT_BRANCH: develop + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + FILTER_REGEX_EXCLUDE: '.*(assets/.*|\.goreleaser\.yml)' + # go validator works pretty bad in super-linter, we'll use the original one + VALIDATE_GO: false + # do not validate SQL - linters are pretty useless in case of this library, + # and the functionality is tested using integration tests + VALIDATE_SQL: false + VALIDATE_SQLFLUFF: false + VALIDATE_CHECKOV: false + VALIDATE_YAML: false + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: ./go.mod + + - name: Lint Golang + uses: golangci/golangci-lint-action@v6 + with: + only-new-issues: ${{ github.event_name == 'pull_request' }} + args: > + --config=./.github/linters/.golangci.yml + + codespell: + name: Check spelling + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + - uses: actions/checkout@v4 + - uses: codespell-project/actions-codespell@v2 + with: + exclude_file: go.mod,go.sum,assets/dist/script.js diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..b1461cd --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,61 @@ +--- +name: Release + +on: + release: + types: + - created + pull_request: + +jobs: + goreleaser: + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + - name: Check out code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Fetch git tags + run: git fetch --force --tags + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: ./go.mod + + - name: Docker Login + if: false # disabled + env: + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + run: | + echo "${DOCKER_PASSWORD}" | docker login --username "${DOCKER_USERNAME}" --password-stdin + + - name: Setup QEMU + uses: docker/setup-qemu-action@v3 + with: + platforms: arm64 + + - name: Set up Docker BuildX + uses: docker/setup-buildx-action@v3 + with: + platforms: linux/amd64,linux/arm64v8 + + - name: Run GoReleaser (Release) + uses: goreleaser/goreleaser-action@v6 + if: github.event_name == 'release' + with: + args: 'release --clean' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Run GoReleaser (PR) + uses: goreleaser/goreleaser-action@v6 + if: github.event_name == 'pull_request' + with: + args: '--skip=publish --snapshot --clean' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml new file mode 100644 index 0000000..4e3d32d --- /dev/null +++ b/.github/workflows/testing.yml @@ -0,0 +1,38 @@ +--- +name: "Test" + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +on: + push: + branches: [ main, develop ] + pull_request: + +jobs: + test: + name: Test + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: ./go.mod + + - name: Run tests + run: make test + + - name: Upload coverage to Codecov + if: success() + uses: codecov/codecov-action@v4 + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + with: + file: ./coverage.txt + fail_ci_if_error: false diff --git a/.gitignore b/.gitignore index d47126e..31787f3 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ tmp app +dist/* # Test binary, built with `go test -c` *.test diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..19594ad --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,63 @@ +--- +version: 2 + +project_name: e-shop-fiber-microservice-boilerplate + +builds: + - id: binary-build # ID of the build + main: ./cmd/main.go # Path to main.go file or main package + binary: app # Binary name + # Custom ldflags + ldflags: + - -s -w -X main.version={{.Version}} + # Custom environment variables to be set during the builds + env: + - CGO_ENABLED=0 + # GOOS list to build for + goos: + - linux + - darwin + - windows + # GOARCH to build for + goarch: + - amd64 + - arm64 + +dockers: + - goos: linux + goarch: amd64 + use: buildx + ids: + - binary-build + image_templates: + - "WildEgor/e-shop-fiber-microservice-boilerplate:latest-amd64" + - "WildEgor/e-shop-fiber-microservice-boilerplate:{{.Tag}}-amd64" + build_flag_templates: + - "--platform=linux/amd64" + dockerfile: Dockerfile.goreleaser + # You can add extra pre-existing files to the upload + extra_files: + - assets + - goos: linux + goarch: arm64 + use: buildx + ids: + - binary-build + image_templates: + - "WildEgor/e-shop-fiber-microservice-boilerplate:latest-arm64v8" + - "WildEgor/e-shop-fiber-microservice-boilerplate:{{.Tag}}-arm64v8" + build_flag_templates: + - "--platform=linux/arm64/v8" + dockerfile: Dockerfile.goreleaser + extra_files: + - assets + +docker_manifests: + - name_template: WildEgor/e-shop-fiber-microservice-boilerplate:latest + image_templates: + - "WildEgor/e-shop-fiber-microservice-boilerplate:latest-amd64" + - "WildEgor/e-shop-fiber-microservice-boilerplate:latest-arm64v8" + - name_template: WildEgor/e-shop-fiber-microservice-boilerplate:{{.Tag}} + image_templates: + - "WildEgor/e-shop-fiber-microservice-boilerplate:{{.Tag}}-amd64" + - "WildEgor/e-shop-fiber-microservice-boilerplate:{{.Tag}}-arm64v8" diff --git a/Dockerfile b/Dockerfile index 727b7bc..41e026c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ LABEL maintainer="YOUR_NAME " #RUN apk update && apk add ca-certificates git openssh #RUN git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/" WORKDIR /app -COPY go.mod go.sum ./ +COPY ./go.mod ./go.sum ./ RUN mkdir -p dist RUN go mod download diff --git a/Dockerfile.goreleaser b/Dockerfile.goreleaser new file mode 100644 index 0000000..f54f63c --- /dev/null +++ b/Dockerfile.goreleaser @@ -0,0 +1,7 @@ +FROM cgr.dev/chainguard/busybox:latest-glibc +WORKDIR /app/ +RUN mkdir -p /app/assets +COPY ./assets/ /assets +COPY app . +RUN chmod a+x /app +CMD ["/app"] \ No newline at end of file diff --git a/Taskfile.yml b/Taskfile.yml index 801b45b..fcf4749 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -1,3 +1,4 @@ +--- version: '3' tasks: @@ -18,4 +19,7 @@ tasks: # Update swagger files swag: - cmd: swag init -g cmd/main.go --output api/swagger \ No newline at end of file + cmd: swag init -g cmd/main.go --output api/swagger + + debug-build: + goreleaser release --snapshot --clean \ No newline at end of file