Skip to content

Commit

Permalink
Merge pull request #22 from evrone/develop
Browse files Browse the repository at this point in the history
Release v1.0.0
  • Loading branch information
golang-school authored Apr 2, 2021
2 parents 63df786 + ea12aba commit df6e25c
Show file tree
Hide file tree
Showing 55 changed files with 3,731 additions and 4 deletions.
22 changes: 22 additions & 0 deletions .circleci/bin/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

set -ex

REVIEWDOG_VERSION=v0.11.0
GOLANGCILINT_VERSION=v1.37.1
HADOLINT_VERSION=v1.22.1
DOTENV_LINTER_VERSION=v3.0.0

# Install reviewdog
wget -O - -q https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh \
| sh -s -- -b ./bin $REVIEWDOG_VERSION

# Install golangci-lint
wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s $GOLANGCILINT_VERSION

# Install hadolint
wget -q https://github.com/hadolint/hadolint/releases/download/$HADOLINT_VERSION/hadolint-Linux-x86_64 \
-O ./bin/hadolint && chmod +x ./bin/hadolint

# Install dotenv-linter
wget -q -O - https://git.io/JLbXn | sh -s -- -b bin $DOTENV_LINTER_VERSION
7 changes: 7 additions & 0 deletions .circleci/bin/reviewdog_dotenv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -ex

# Exit code always 0
dotenv-linter \
| reviewdog -f=dotenv-linter -diff="git diff ${DEFAULT_BRANCH}..HEAD" -reporter=github-pr-review
7 changes: 7 additions & 0 deletions .circleci/bin/reviewdog_golangci-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -ex

# Exit code always 0
golangci-lint run --out-format=line-number \
| reviewdog -f=golangci-lint -diff="git diff ${DEFAULT_BRANCH}..HEAD" -reporter=github-pr-review
7 changes: 7 additions & 0 deletions .circleci/bin/reviewdog_hadolint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -ex

# Exit code always 0
git ls-files --exclude='Dockerfile*' --ignored | xargs hadolint \
| reviewdog -efm="%f:%l %m" -diff="git diff ${DEFAULT_BRANCH}..HEAD" -reporter=github-pr-review
88 changes: 88 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
version: 2.1

parameters:
service_name:
type: string
default: "go-service-template"
image:
type: string
default: "evrone/go-service-template"

orbs:
docker: circleci/docker@0.5.20

executors:
default:
docker:
- image: circleci/golang:1.16

aliases:
- &restore-mod-cache
keys:
- go-modules-v1-{{ checksum "go.sum" }}
- go-modules-v1-

- &save-mod-cache
key: go-modules-v1-{{ checksum "go.sum" }}
paths:
- "/go/pkg/mod"

jobs:
linters:
executor:
name: default
steps:
- checkout
- run:
name: Install tools
command: .circleci/bin/install.sh
- run:
name: PATH for working without a relative path
command: echo "export PATH=$PATH:/home/circleci/project/bin" >> $BASH_ENV
- run:
name: Dotenv > Reviewdog
command: .circleci/bin/reviewdog_dotenv.sh
- run:
name: Hadolint > Reviewdog
command: .circleci/bin/reviewdog_hadolint.sh
- run:
name: Golangci-lint > Reviewdog
command: .circleci/bin/reviewdog_golangci-lint.sh
- run:
name: Dotenv
command: dotenv-linter
- run:
name: Hadolint
command: git ls-files --exclude='Dockerfile*' --ignored | xargs hadolint
- run:
name: Golangci-lint
command: golangci-lint run

tests:
executor:
name: default
steps:
- checkout
- restore_cache: *restore-mod-cache
- run:
name: Tests
command: go test -v -race ./internal/...
- save_cache: *save-mod-cache

integration-tests:
executor:
name: default
steps:
- checkout
- setup_remote_docker:
version: 19.03.13
- run:
name: Integration tests
command: docker-compose up --build --abort-on-container-exit --exit-code-from integration

workflows:
ci:
jobs:
- linters
- tests
- integration-tests
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DISABLE_SWAGGER_HTTP_HANDLER=true
GIN_MODE=release
LOG_ROLLBAR_TOKEN=<TOKEN>
PG_URL=postgres://user:pass@localhost:5432/postgres
RMQ_URL=amqp://guest:guest@localhost:5672/
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DISABLE_SWAGGER_HTTP_HANDLER=true
GIN_MODE=release
LOG_ROLLBAR_TOKEN=<TOKEN>
PG_URL=postgres://user:pass@localhost:5432/postgres
RMQ_URL=amqp://guest:guest@localhost:5672/
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/.idea/
/bin/
.env
143 changes: 143 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
linters-settings:
gci:
local-prefixes: github.com/evrone/go-service-template
dupl:
threshold: 100
errorlint:
errorf: true
errcheck:
check-type-assertions: true
check-blank: true
exhaustive:
check-generated: false
default-signifies-exhaustive: false
funlen:
lines: 65
statements: 40
gocognit:
min-complexity: 15
gocyclo:
min-complexity: 10
goconst:
min-len: 2
min-occurrences: 2
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- dupImport
- unnecessaryBlock
golint:
min-confidence: 0.7
gofumpt:
extra-rules: true
gomnd:
settings:
mnd:
checks:
- argument
- case
- condition
- operation
- return
govet:
check-shadowing: true
lll:
line-length: 120
maligned:
suggest-new: true
misspell:
locale: US
nestif:
min-complexity: 4
nolintlint:
require-explanation: true
require-specific: true

linters:
disable-all: true
enable:
- asciicheck
- bodyclose
- cyclop
- deadcode
- depguard
- dogsled
- dupl
- durationcheck
- errcheck
- errorlint
- exhaustive
- exportloopref
- forbidigo
- funlen
- gci
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gofmt
- gofumpt
- goimports
- golint
- gomnd
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- makezero
- misspell
- nakedret
- nestif
- nlreturn
- noctx
- nolintlint
- paralleltest
- predeclared
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- structcheck
- stylecheck
- tparallel
- thelper
- typecheck
- unconvert
- unparam
- unused
- varcheck
- wsl
- whitespace

# disable:
# - exhaustivestruct
# - ifshort
# - goheader
# - maligned
# - prealloc
# - testpackage
# - wrapcheck

issues:
exclude-rules:
- path: integration-test
linters:
- paralleltest
- godot

run:
skip-dirs:
- docs
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Step 1: Modules caching
FROM golang:1.16.2-alpine3.13 as modules
COPY go.mod go.sum /modules/
WORKDIR /modules
RUN go mod download

# Step 2: Builder
FROM golang:1.16.2-alpine3.13 as builder
COPY --from=modules /go/pkg /go/pkg
COPY . /app
WORKDIR /app
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -tags migrate -o /bin/app ./cmd/app

# Step 3: Final
FROM scratch
COPY --from=builder /app/config /config
COPY --from=builder /app/migrations /migrations
COPY --from=builder /bin/app /app
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
CMD ["/app"]
11 changes: 11 additions & 0 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Step 1: Builder
FROM golang:1.16.2-alpine3.13 as builder
COPY . /app
WORKDIR /app
RUN go mod download -x && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -x -o /bin/app ./cmd/app

# Step 2: Final
FROM alpine:3.13
COPY --from=builder /bin/app /app
CMD ["/app"]
59 changes: 59 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
include .env
export

compose-up:
docker-compose up --build -d postgres rabbitmq && docker-compose logs -f
.PHONY: compose-up

compose-up-integration-test:
docker-compose up --build --abort-on-container-exit --exit-code-from integration
.PHONY: compose-up-integration-test

compose-down:
docker-compose down --remove-orphans
.PHONY: compose-down

swag-v1:
swag init -g internal/delivery/http/v1/router.go
.PHONY: swag-v1

run: swag-v1
go mod tidy && go mod download && \
DISABLE_SWAGGER_HTTP_HANDLER='' GIN_MODE=debug CGO_ENABLED=0 go run -tags migrate ./cmd/app
.PHONY: run

docker-rm-volume:
docker volume rm go-service-template_pg-data
.PHONY: docker-rm-volume

linter-golangci:
golangci-lint run
.PHONY: linter-golangci

linter-hadolint:
git ls-files --exclude='Dockerfile*' --ignored | xargs hadolint
.PHONY: linter-hadolint

linter-dotenv:
dotenv-linter
.PHONY: linter-dotenv

test:
go test -v -cover -race ./internal/...
.PHONY: test

integration-test:
go clean -testcache && go test -v ./integration-test/...
.PHONY: integration-test

mock:
mockery --all -r --case snake
.PHONY: mock

migrate-create:
migrate create -ext sql -dir migrations 'migrate_name'
.PHONY: migrate-create

migrate-up:
migrate -path migrations -database '$(PG_URL)?sslmode=disable' up
.PHONY: migrate-up
Loading

0 comments on commit df6e25c

Please sign in to comment.