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.
- Install migrate to migration table to database. You can visti 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):
make watch
run without hot reload
make run
-
Migration database:
migrate -database ${POSTGRESQL_URL} -path database/migrations up
Run by docker
docker-compose up
/cmd
The folder will contain the main applications for this project.
There might be more than 1 application within cmd folder, and each application should have its own folder so the path would be cmd -> applicationNameFolder.
/api
This folder can have OpenAPI/Swagger specs, JSON schema files, and protocol definition files.
/configs
It can have configuration file templates or default configs.
/internal
Private application and library code. This is the code you don't want others importing into their applications or libraries. Note that this layout pattern is enforced by the Go compiler itself. Note that you are not limited to the top-level internal directory. You can have more than one internal directory at any level of your project tree.
/pkg
Library codes which can be used by external applications (e.g. /pkg/mypubliclib). Other projects will import these libraries expecting them to work, so double-check before you put something here.
/web
Web application-specific components: static web assets, server-side templates and SPAs.
/http
You might want to expose your application through several means of communication such as rest-api and grpc.
This way you can have a separate separation between each type of communication layer by creating a separate directory such as /http/rest or /http/grpc
/tests
The folder will contain unit test, testcase of the project.
/utils
The directory contains supporting tools for this project.
make test
# or
go test ./tests -cover
# with cover
go test ./tests -cover
# with verbose
go test -v ./test -cover
# specific folder
go test -v ./utils -cover
# specific test file
go test ./utils/array_test.go ./utils/array.go
# one unit test
# - utils is a package name
# - TestChunkSlice is a testcase
go test utils -run TestChunkSlice
make lint
curl -L 'localhost:8088/api/v1/health'
response:
{
"status": "success",
"message": "live",
"data": {
"welcome": "Welcome to GoldenOwn Consulting"
}
}
curl -L 'localhost:8088/api/v1/user/login' \
-H 'Content-Type: application/json' \
-d '{
"username": "admin",
"password": "12345678"
}'
response:
{
"status": "success",
"message": "Login success",
"data": {
"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9eyJhdXRob3JpemVkIjp0cnVlLCJleHAiOjE3MDUzOTIzNDQsInVzZXJfaWQiOjEwfQtqD4X_5tl_xsNNkhsP-ub6jBPjFNEa0sRjcGoqdHW8"
}
}
curl --location 'localhost:8088/api/v1/user' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRob3JpemVkIjp0cnVlLCJleHAiOjE3MDUzOTIzNDQsInVzZXJfaWQiOjEwfQ.tqD4X_5tl_xsNNkhsP-ub6jBPjFNEa0sRjcGoqmdHW8'
response:
{
"status": "success",
"message": "Get user success",
"data": {
"created_at": "2024-01-15T16:34:14Z",
"updated_at": "2024-01-15T16:34:14Z",
"username": "edric1",
"password": "",
"email": "cao.bada96@gmail.com",
"status": 1,
"id": 1
}
}