Skip to content
This repository has been archived by the owner on Jan 27, 2024. It is now read-only.

GoldenOwlAsia/golang-api-template

Repository files navigation

Golden Owl Golang Gin API Template

accessibility text

Why Using Gin For Golang Backend?

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.

Prerequisites

  1. Install go. You can download the Golang in this page. You should install version 1.20
  2. Install Postgres database. You can download the Postgres in this page. You should install version 14.1
  3. Make an .env file from .env.example
  4. Go to pgadmin create a database. Note the name of it and add to .env
  5. Install Air - live reload fo Go apps. You can visit this page.

💿 Installation

Via go

  1. You run this command to install packages
    go mod download && go mod tidy
  2. Create .env file from .env.example file.
  3. run this command to start (hot reload):
    make watch
    run without hot reload
    make run
  4. 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

📌 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

  1. change configuration in file wire.go
  2. 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

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": "admin@example.com",
         "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'

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": "admin@example.com",
            "role": "Admin",
            "status": ""
         },
         "title": "test",
         "content": "test content"
      }
   ]
}