Categorygithub.com/GoldenOwlAsia/golang-api-template
modulepackage
1.1.1
Repository: https://github.com/goldenowlasia/golang-api-template.git
Documentation: pkg.go.dev

# README

Golden Owl Golang Gin API

Getting Started

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. 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
    
  6. 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" }'

  7. 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

  1. 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

  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
#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"
      }
   ]
}

# Packages

No description provided by the author
Code generated by swaggo/swag.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author