Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Develop

See merge request golang-general/echo-demo-app!26
  • Loading branch information
taras-seryogin committed Dec 17, 2023
2 parents 5b309ca + 0887737 commit ec659b2
Show file tree
Hide file tree
Showing 40 changed files with 537 additions and 432 deletions.
32 changes: 24 additions & 8 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
HOST=
PORT=
DB_USER=
DB_PASSWORD=
DB_DRIVER=
DB_NAME=
DB_HOST=
DB_PORT=
#The application host name
HOST=localhost
#The application port to get access from the docker container
PORT=7788

#Parameters for getting the access to the database
DB_USER=local_user
DB_PASSWORD=password
DB_DRIVER=mysql
DB_NAME=echo_example_docker
DB_HOST=echo_mysql
DB_PORT=33060

#IDs of user group and user which will be used inside docker container by the linter to make changes to files
COMPOSE_USER_ID=999
COMPOSE_GROUP_ID=999

#The application and the database will be accessible from the host machine on these ports
EXPOSE_PORT=7788
EXPOSE_DB_PORT=3306

#Secret keys for the access token and refresh token signing
ACCESS_SECRET=access_secret
REFRESH_SECRET=refresh_secret
40 changes: 17 additions & 23 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ stages:
- deploy

lint:
image: golangci/golangci-lint:v1.36.0
image: golangci/golangci-lint:v1.51.0
stage: test
before_script:
- go get -v github.com/swaggo/swag/cmd/swag
- $GOPATH/bin/swag init -g cmd/main.go
- cp .env.testing.gitlab-ci .env.testing
- go install -v github.com/swaggo/swag/cmd/[email protected]
- swag init -g cmd/main.go
script:
- golangci-lint run -v
- golangci-lint run
allow_failure: true

migrations:
image: golang:1.13.3-alpine3.10
image: golang:1.21.4-alpine3.18
services:
- mysql:5.7
tags:
Expand All @@ -37,15 +38,15 @@ migrations:
before_script:
- apk add --no-cache --update git build-base openssh-client
- cp .env.testing.gitlab-ci .env
- go get -v github.com/swaggo/swag/cmd/swag
- go install -v github.com/swaggo/swag/cmd/swag@v1.8.10
- $GOPATH/bin/swag init -g cmd/main.go
script:
- go get -v ./...
- go run migrations/entry.go --env-path=./ --verbose
- go run migrations/entry.go --rollback --env-path=./ --verbose

test:
image: golang:1.13.3-alpine3.10
image: golang:1.20.1-alpine3.17
services:
- mysql:5.7
tags:
Expand All @@ -55,8 +56,8 @@ test:
before_script:
- apk add --no-cache --update git build-base openssh-client
- cp .env.testing.gitlab-ci .env.testing
- go get -v github.com/swaggo/swag/cmd/swag
- $GOPATH/bin/swag init -g cmd/main.go
- go install -v github.com/swaggo/swag/cmd/swag@v1.8.10
- swag init -g cmd/main.go
script:
- go get -v ./...
- go test -v ./...
Expand All @@ -77,13 +78,10 @@ build:
- docker push $CI_REGISTRY_IMAGE:staging

deploy staging:
image: golang:1.13.3-alpine3.10
image: golang:1.20.1-alpine3.17
environment:
name: staging
when: manual
only:
refs:
- develop
tags:
- dind
- docker
Expand All @@ -95,15 +93,11 @@ deploy staging:
- chmod 600 ~/.ssh/id_rsa
- eval "$(ssh-agent -s)"
- ssh-add ~/.ssh/id_rsa
- ssh-keyscan -H $STAGING_IP >> ~/.ssh/known_hosts
- GOOS=linux GOARCH=386 go build -o entry migrations/entry.go
- ssh-keyscan -p 221 -H $STAGING_IP >> ~/.ssh/known_hosts
script:
- ssh dev@${STAGING_IP} "mkdir -p ${PROJECT_PATH}"
- scp -r ${STAGING_ENV} dev@${STAGING_IP}:${PROJECT_PATH}/.env
- scp -r ${MIGRATION_ENV} dev@${STAGING_IP}:${PROJECT_PATH}/.env.migration
- scp -r ./deploy/docker-compose.yml dev@${STAGING_IP}:${PROJECT_PATH}/docker-compose.yml
- ssh dev@$STAGING_IP "docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY};
- ssh dev@${STAGING_IP} -p 221 "mkdir -p ${PROJECT_PATH}"
- scp -P 221 -r ${STAGING_ENV} dev@${STAGING_IP}:${PROJECT_PATH}/.env
- scp -P 221 -r ./deploy/compose.yml dev@${STAGING_IP}:${PROJECT_PATH}/compose.yml
- ssh dev@$STAGING_IP -p 221 "docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY};
docker pull ${CI_REGISTRY}/${CI_PROJECT_PATH}:staging;
docker-compose -f ${PROJECT_PATH}/docker-compose.yml up -d"
- scp -r ./entry dev@${STAGING_IP}:${PROJECT_PATH}/entry
- ssh dev@${STAGING_IP} "${PROJECT_PATH}/entry --env-path=${PROJECT_PATH} --env-file=.env.migration --verbose"
docker compose -f ${PROJECT_PATH}/compose.yml up -d"
14 changes: 5 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
Expand All @@ -63,27 +62,23 @@ linters:
- gocyclo
- gofmt
- goimports
- golint
- revive
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- lll
- misspell
- nakedret
- rowserrcheck
- scopelint
- exportloopref
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace

# don't enable:
Expand All @@ -95,7 +90,8 @@ linters:

run:
skip-dirs:
- test
- tests
- migrations/list

service:
golangci-lint-version: 1.36.0
golangci-lint-version: 1.51.0
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Start from golang base image
FROM golang:1.20-alpine as builder
FROM golang:1.21.4-alpine3.18 as builder

# Install git.
# Git is required for fetching the dependencies.
Expand All @@ -9,7 +9,7 @@ RUN apk update && apk add --no-cache git
WORKDIR /app

RUN go install github.com/githubnemo/CompileDaemon@latest
RUN go install github.com/swaggo/swag/cmd/swag@latest
RUN go install github.com/swaggo/swag/cmd/swag@v1.8.10

ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
RUN chmod +x /wait
Expand Down
12 changes: 7 additions & 5 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Copyright (C) 2013 by NIX Solutions Ltd.
MIT License

Copyright (c) 2023 NIX Group

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -7,13 +9,13 @@ 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 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.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# Overview
<h1 align="center">Welcome to <span style="color:mediumseagreen">Echo boilerplate</span> 👋</h1>

<p align="center">
<img src="https://img.shields.io/badge/golang-v1.21-lightblue" height="25"/>
<img src="https://img.shields.io/badge/echo-v4.11-blue" height="25"/>
<img src="https://img.shields.io/badge/gorm-v1.25-green" height="25"/>
<img src="https://img.shields.io/badge/swagger-v1.16-orange" height="25"/>
<img src="https://img.shields.io/badge/gin--jwt-v2.9-yellow" height="25"/>
<img src="https://img.shields.io/badge/docker-support-darkgreeen" height="25"/>
</p>

It's an API Skeleton project based on Echo framework.
Our aim is reducing development time on default features that you can meet very often when your work on API.
There is a useful set of tools that described below. Feel free to contribute!
Expand Down Expand Up @@ -102,13 +112,17 @@ Finally, you need to fix all problems manually or using autofixing (if it's supp
## Libraries
Migrations - https://github.com/ShkrutDenis/go-migrations

Jwt - https://github.com/dgrijalva/jwt-go
Jwt - https://github.com/golang-jwt/jwt/v5

Swagger - https://github.com/swaggo/echo-swagger

Mocking db - https://github.com/selvatico/go-mocket

Orm - https://github.com/jinzhu/gorm
Orm - https://gorm.io/gorm

## License
The project is developed by [NIX Solutions](http://nixsolutions.com) Go team and distributed under [MIT LICENSE](https://github.com/nixsolutions/golang-echo-boilerplate/blob/master/LICENSE)

The project is developed by [NIX][1] and distributed under [MIT LICENSE][2]

[1]: https://nixs.com/
[2]: https://github.com/nixsolutions/golang-echo-boilerplate/blob/master/LICENSE
22 changes: 11 additions & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import (
"fmt"
)

// @title Echo Demo App
// @version 1.0
// @description This is a demo version of Echo app.
// @title Echo Demo App
// @version 1.0
// @description This is a demo version of Echo app.

// @contact.name NIX Solutions
// @contact.url https://www.nixsolutions.com/
// @contact.email [email protected]
// @contact.name NIX Solutions
// @contact.url https://www.nixsolutions.com/
// @contact.email [email protected]

// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization

// @BasePath /
// @BasePath /
func main() {
cfg := config.NewConfig()

docs.SwaggerInfo.Host = fmt.Sprintf("%s:%s", cfg.HTTP.Host, cfg.HTTP.ExposePort)
docs.SwaggerInfo.Host = fmt.Sprintf("%s:%s", cfg.HTTP.Host, cfg.HTTP.Port)

application.Start(cfg)
}
3 changes: 1 addition & 2 deletions docker-compose.yml → compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.7'
services:
echo_app:
container_name: echo_demo
Expand All @@ -21,7 +20,7 @@ services:
- echo-demo-stack

echo_mysql:
image: mysql:5.7
image: mysql:8.0
container_name: echo_db
ports:
- ${EXPOSE_DB_PORT}:${DB_PORT}
Expand Down
6 changes: 3 additions & 3 deletions db/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"echo-demo-project/db/seeders"
"fmt"

_ "github.com/go-sql-driver/mysql" // nolint
"github.com/jinzhu/gorm"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

func Init(cfg *config.Config) *gorm.DB {
Expand All @@ -19,7 +19,7 @@ func Init(cfg *config.Config) *gorm.DB {

fmt.Println(dataSourceName)

db, err := gorm.Open(cfg.DB.Driver, dataSourceName)
db, err := gorm.Open(mysql.Open(dataSourceName), &gorm.Config{})
if err != nil {
panic(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion db/seeders/user_seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package seeders
import (
"echo-demo-project/models"

"github.com/jinzhu/gorm"
"gorm.io/gorm"
)

type UserSeeder struct {
Expand Down
18 changes: 14 additions & 4 deletions deploy/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# base image
FROM golang:1.13.3-alpine3.10 as build
FROM golang:1.21.4-alpine3.18 as build

RUN apk add --no-cache --update git build-base openssh-client

Expand All @@ -16,14 +16,24 @@ RUN mkdir ~/.ssh && \
COPY . .

RUN git config --global http.sslVerify true &&\
go get -v github.com/swaggo/swag/cmd/swag &&\
go install github.com/swaggo/swag/cmd/swag@v1.8.10 &&\
$GOPATH/bin/swag init -g cmd/main.go &&\
go get -v ./... &&\
go build -o demo-echo .
go build -o demo-echo ./cmd &&\
go build -o entry ./migrations

FROM alpine

ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
RUN chmod +x /wait

WORKDIR /app
COPY --from=build /go/src/api/demo-echo /app/demo-echo
COPY --from=build /go/src/api/entry /app/entry

RUN ["chmod", "+x", "./demo-echo"]
RUN ["chmod", "+x", "./entry"]

CMD ["./demo-echo"]
CMD /wait &&\
./entry --verbose &&\
./demo-echo
12 changes: 10 additions & 2 deletions deploy/docker-compose.yml → deploy/compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
version: "3.7"
services:
echo-demo:
container_name: echo-demo
image: hub.gitlab.nixdev.co/golang-general/echo-demo-app:staging
ports:
- 9379:80
- ${EXPOSE_PORT}:${PORT}
depends_on:
- mysql
environment:
WAIT_HOSTS: mysql:${DB_PORT}
WAIT_BEFORE_HOSTS: 5
WAIT_SLEEP_INTERVAL: 3
DB_USERNAME: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD}
DB_NAME: ${DB_NAME}
DB_HOST: mysql
DB_PORT: ${DB_PORT}
volumes:
- ./.env:/app/.env
restart: on-failure
Expand Down
1 change: 0 additions & 1 deletion development/golangci_lint/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.7'
services:
echo-golinter:
build:
Expand Down
Loading

0 comments on commit ec659b2

Please sign in to comment.