It's an API Skeleton project based on Gin 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!
- Registration
- Authentication with JWT
- CRUD API for posts
- Migrations
- Request validation
- Swagger docs
- Environment configuration
- Docker development environment
-
Copy .env.dist to .env and set the environment variables.
-
Run your application using the command in the terminal:
docker-compose up
-
Browse to
{HOST}:{EXPOSE_PORT}/swagger/index.html
. You will see Swagger 2.0 API documents. -
Using the API documentation, make requests to register a user (if necessary) and login.
-
After the successful login, copy a token from the response, then click "Authorize" and in a popup that opened, enter the value for "apiKey" in a form: "Bearer {token}". For example:
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODk0NDA5NjYsIm9yaWdfaWF0IjoxNTg5NDM5OTY2LCJ1c2VyX2lkIjo1fQ.f8dSG3NxFLHwyA5-XIYALT5GtXm4eiH-motqtqAUBOI
Then, click "Authorize" and close the popup. Now, you are able to make requests which require authentication.
- Docker
- Minikube
Start minikube
minikube start
Connect to minikube Docker
eval $(minikube docker-env)
Update env file of project (update db values)
DB_HOST=gin-demo # host inside kubernetes
DB_PORT=3306 # port inside kubernetes
Build docker container locally
docker build -t gin_demo:dev .
You can use one command to do this
minikube kubectl -- create -f kubernetes/
or do it separately:
-
Run database
minikube kubectl -- create -f kubernetes/mysql-secret.yaml minikube kubectl -- apply -f kubernetes/mysql-db-pv.yaml minikube kubectl -- apply -f kubernetes/mysql-db-pvc.yaml minikube kubectl -- apply -f kubernetes/mysql-db-deployment.yaml minikube kubectl -- apply -f kubernetes/mysql-db-service.yaml minikube kubectl -- get pods # check the status of the pod
-
Run application
minikube kubectl -- apply -f kubernetes/app-gin-demo-deployment.yaml minikube kubectl -- apply -f kubernetes/app-gin-demo-service.yaml minikube kubectl -- get pods # check the status of the pod
-
After that, the application should work (look at the pods to check) To redirect an application from Kubernetes to the local machine, run the command (you will probably have to enable the ingress addon in the minikube):
minikube kubectl -- apply -f kubernetes/ingress.yaml
minikube service app-gin-demo
You can now make requests to the application using Postman
Entry point of whole project is main.go file. Here you should init DB connection, init Server and start listen for new connections.
server
package will contain implementation of your server(requests, responses, handlers, routes, models).
db
package is responsible for connection with a database. Place for DB connection initiation.
handler
package is responsible for connection of business logic layer and transport layer.
model
package is a place where to store app models.
repository
package is a place where to store repositories.
request
package is a place where to store requests.
response
package is a place where to store requests.
service
package is a place where to store business logic of the app.
Check README.md
in server
package to get more details.
For control code quality we are use golangci-lint. Golangci-lint is a linters aggregator.
Why we use linters? Linters help us:
- Finding critical bugs
- Finding bugs before they go live
- Finding performance errors
- To speed up the code review, because reviewers do not spend time searching for syntax errors and searching for violations of generally accepted code style
- The quality of the code is guaranteed at a fairly high level.
Linter tool wrapped to docker-compose and first of all need to build container with linters
make lint-build
Next you need to run linter to check bugs ant errors
make lint-check
- it will log to console what bugs and errors linters found
Finally, you need to fix all problems manually or using autofixing (if it's supported by the linter)
make lint-fix
The project is developed by NIX and distributed under MIT LICENSE