Skip to content

charlie-goldenowl/golang-api-template

 
 

Repository files navigation

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

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 98.3%
  • Dockerfile 1.2%
  • Makefile 0.5%