Skip to content

hylim-tech-lover/restful-services-go

Repository files navigation

Dockerized RESTful services in Go

Environment Setup / Pre-requisite

Please install the following for running OS environment before continue:

  1. Go
  2. Docker
  3. Postman

Quick Start

  1. Git clone current repository to your local environment and open with preferred IDE [eg: VS code].
  2. Rename .env.example to .env file .
  3. Make sure no other application running at port 3000 as it will be used by the application.
  4. Make sure docker is already running at local environment.
  5. Open terminal in project root path and run following command to verify docker are running:
# View installed docker version
docker version
  1. If docker is not running, please run docker and repeat Step 5.
  2. At the terminal, run following command to start containerized RESTful services in Go with Postgres database via Docker.
# Create docker instance based on docker-compose.yml
docker compose up
  1. Make sure terminal have the following prompt below continue:

    Fiber cli prompt

  2. Open Postman and start to send RESTful API with correct parameter as stated in API sesison below.


API Endpoints Reference

GET http://localhost:3000

This API will return the raw JSON format of stored object(s) in Postgres. Hence, it will be empty by default and objects will be displayed after running POST request for at least once and success.

Sample Response

[
  {
    "ID": 1,
    "CreatedAt": "2022-12-14T14:42:59.162640885Z",
    "UpdatedAt": "2022-12-14T14:42:59.162640885Z",
    "DeletedAt": null,
    "question": "Is this a sample?",
    "answer": "Yes"
  },
  {
    "ID": 2,
    "CreatedAt": "2022-12-14T14:45:11.451989Z",
    "UpdatedAt": "2022-12-14T14:45:11.451989Z",
    "DeletedAt": null,
    "question": "Is this a sample too?",
    "answer": "Yea!"
  }
]

POST http://localhost:3000/fact

Use Postman and create a new POST request.

Then, choose Body , then raw and make sure JSON is selected. Paste in the following skeleton and fill in accordingly.

Body

name required
question yes
answer yes

Sample Body

{
  "question": "Who learn Golang today?",
  "answer": "Me!",
}

Response

{
  "ID": 1,
  "CreatedAt": "2022-12-14T14:42:59.162640885Z",
  "UpdatedAt": "2022-12-14T14:42:59.162640885Z",
  "DeletedAt": null,
  "question": "Who learn Golang today?",
  "answer": "Me!"
}

Third-party Golang package/modules installed

  • Go-fiber
  • Gorm with Postgres driver
  • Air