Gin allows you to build web applications and microservices in Go. It contains a set of commonly used functionalities (e.g., routing, middleware support, rendering, etc.) that reduce boilerplate code and make it simpler to build web applications.
- Install go. You can download the Golang in this page. You should install version 1.20
- Install Postgres database. You can download the Postgres in this page. You should install version 14.1
- Make an
.env
file from.env.example
- Go to
pgadmin
create a database. Note the name of it and add to.env
- Install Air - live reload fo Go apps. You can visit this page.
- You run this command to install packages
go mod download && go mod tidy
- Create
.env
file from.env.example
file. - run this command to start (hot reload):
run without hot reload
make watch
make run
- Visit: http://localhost:8080/swagger/index.html to access the API interface.
Run by docker
docker-compose up
build api executable file
make build
Run swag init
in the project's root folder which contains the main.go
file. This will parse your comments and generate the required files (docs
folder and docs/docs.go
).
swag init
- change configuration in file
wire.go
- run the following command to automatically generate the code
make di
make test
# or
go test ./... -cover
# with cover
go test ./... -cover
# with verbose
go test -v ./... -cover
# specific folder
go test -v ./utils -cover
# specific test file
go test ./utils/array_test.go ./utils/array.go
# one unit test
# - api/utils is a package name
# - TestChunkSlice is a testcase
go test api/utils -run TestChunkSlice
make lint
curl -L 'localhost:8080/api/v1/user/login' \
-H 'Content-Type: application/json' \
-d '{
"username": "admin",
"password": "1234"
}'
response:
{
"status": "success",
"message": "welcome back",
"data": {
"user": {
"id": 1,
"created_at": "2023-04-19T14:32:21.978531Z",
"updated_at": "2023-04-19T14:32:21.978531Z",
"username": "admin",
"email": "[email protected]",
"role": "Admin",
"status": ""
},
"access_token": "xxx.xxx.xxx",
"refresh_token": "yyy.yyy.yyy"
}
}
curl -L 'localhost:8080/api/v1/articles' -H 'Authorization: Bearer xxx.xxx.xxx-xxx'
response:
{
"_metadata": {
"limit": 10,
"total": 54,
"total_pages": 6,
"per_page": 10,
"page": 1,
"sort": "created_at DESC"
},
"records": [
{
"id": 59,
"created_at": "2023-04-23T10:38:29.80017Z",
"updated_at": "2023-04-23T10:38:29.80017Z",
"user": {
"id": 1,
"created_at": "2023-04-19T14:32:21.978531Z",
"updated_at": "2023-04-19T14:32:21.978531Z",
"username": "admin",
"email": "[email protected]",
"role": "Admin",
"status": ""
},
"title": "test",
"content": "test content"
}
]
}