# README
Golden Owl Golang Gin API
Getting Started
Prerequisites
-
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
-
Migrations & Seeding data
create all database tables
go run cmd/run_migration.go
migrate and seed your database with dummy data
go run cmd/run_migration.go --seed
-
Run command to create user for login
curl --location 'pathtohost' \ --header 'Content-Type: application/json' \ --data-raw '{ "confirm_password": "123", "email": "[email protected]", "password": "123", "username": "admin" }'
-
Install Air - live reload fo Go apps. You can visit this page.
๐ฟ Installation
Via go
- 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 reloadmake watch
make run
- Visit: http://localhost:8080/swagger/index.html to access the API interface.
Via docker
- Run by docker
docker-compose up
โถ๏ธ Build go executables
build api executable file
make build
run it
api
๐ Database Diagram
๐ Converts Go annotations to Swagger Documentation 2.0
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
๐ Dependency injection with Wire
- change configuration in file
wire.go
- run the following command to automatically generate the code
make di
๐งช testing
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
๐งช Improve code with lint checks
make lint
#or
golangci-lint run -v
Demo
Login
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"
}
}
Articles
curl -L 'localhost:8080/api/v1/articles' \
-H 'Authorization: Bearer xxx.xxx.xxx-xxx' \
-d ''
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"
}
]
}