Skip to content

This samples provided how we create connection and manipulate Golang with PosgreSQL

Notifications You must be signed in to change notification settings

go-tutorials/go-posgresql-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-posgresql-tutorial

How to run

To run the application

go run main.go

API Design

Common HTTP methods

  • GET: retrieve a representation of the resource
  • POST: create a new resource
  • PUT: update the resource
  • PATCH: perform a partial update of a resource
  • DELETE: delete a resource

API design for health check

To check if the service is available.

Request: GET /health

Response:

{
    "status": "UP",
    "details": {
        "sql": {
            "status": "UP"
        }
    }
}

API design for users

Resource: users

Get all users

Request: GET /users

Response:

[
    {
        "id": "spiderman",
        "username": "peter.parker",
        "email": "[email protected]",
        "phone": "0987654321",
        "dateOfBirth": "1962-08-25T16:59:59.999Z"
    },
    {
        "id": "wolverine",
        "username": "james.howlett",
        "email": "[email protected]",
        "phone": "0987654321",
        "dateOfBirth": "1974-11-16T16:59:59.999Z"
    }
]

Get one user by id

Request: GET /users/:id

GET /users/wolverine

Response:

{
  "id": "001",
  "username": "Radhika",
  "password": "12345678",
  "email": "[email protected]",
  "phone": "0900012345",
  "dateOfBirth": "1974-11-16T16:59:59.999Z",
  "interests": [
    "Play game",
    "Foot ball",
    "Basket ball"
  ],
   "skills": [
    {
      "skill": "Writing fast",
      "hirable": true
    }
  ],
  "settings": {
    "userId": "001",
    "language": "English",
    "dateformat": "dd/mm/yyyy",
    "datetimeformat" : "dd-mm-yyyy:hh:mm",
    "timeformat": "hh:mm:ss",
    "notification": true
  }
}

Create a new user

Request: POST /users

{
  "id": "001",
  "username": "Radhika",
  "password": "12345678",
  "email": "[email protected]",
  "phone": "0900012345",
  "dateOfBirth": "1974-11-16T16:59:59.999Z",
  "interests": [
    "Play game",
    "Foot ball",
    "Basket ball"
  ],
   "skills": [
    {
      "skill": "Writing fast",
      "hirable": true
    }
  ],
  "settings": {
    "userId": "001",
    "language": "English",
    "dateformat": "dd/mm/yyyy",
    "datetimeformat" : "dd-mm-yyyy:hh:mm",
    "timeformat": "hh:mm:ss",
    "notification": true
  }
}

{
  "id": "002",
  "username" : "Kisiman",
  "email" : "[email protected]",
  "phone" : "0900349845",
  "dateOfBirth" : "1982-11-14T16:59:59.999Z",
  "interests": [
	"Tennis",
    "Volley ball",
    "Basket ball"
  ],
  "skills": [
    {
      "Skill": "Writing fast",
      "Hirable": true
    }
  ],
  "settings": {
    "userId": "002",
    "language": "France",
    "dateFormat": "dd-mm-yyyy",
    "dateTimeFormat" : "dd-mm-yyyy:hh:mm",
    "timeFormat": "hh:mm",
    "notification": true
  }
}

Response: 1: success, 0: duplicate key, -1: error

1

Update one user by id

Request: PUT /users/:id

PUT /users/wolverine
{
  "id": "001",
  "username": "Radhika",
  "password": "12345678",
  "email": "[email protected]",
  "phone": "0900012345",
  "dateOfBirth": "1974-11-16T16:59:59.999Z",
  "interests": [
    "Play game",
    "Foot ball",
    "Basket ball"
  ],
   "skills": [
    {
      "skill": "Writing fast",
      "hirable": true
    }
  ],
  "settings": {
    "userId": "001",
    "language": "English",
    "dateformat": "dd/mm/yyyy",
    "datetimeformat" : "dd-mm-yyyy:hh:mm",
    "timeformat": "hh:mm:ss",
    "notification": true
  }
}

Response: 1: success, 0: not found, -1: error

1

Delete a new user by id

Request: DELETE /users/:id

DELETE /users/wolverine

Response: 1: success, 0: not found, -1: error

1

Common libraries

  • core-go/health: include HealthHandler, HealthChecker, SqlHealthChecker
  • core-go/config: to load the config file, and merge with other environments (SIT, UAT, ENV)
  • core-go/log: log and log middleware

core-go/health

To check if the service is available, refer to core-go/health

Request: GET /health

Response:

{
    "status": "UP",
    "details": {
        "sql": {
            "status": "UP"
        }
    }
}

To create health checker, and health handler

    db, err := sql.Open(conf.Driver, conf.DataSourceName)
    if err != nil {
        return nil, err
    }

    sqlChecker := s.NewSqlHealthChecker(db)
    healthHandler := health.NewHealthHandler(sqlChecker)

To handler routing

    r := mux.NewRouter()
    r.HandleFunc("/health", healthHandler.Check).Methods("GET")

core-go/config

To load the config from "config.yml", in "configs" folder

package main

import "github.com/core-go/config"

type Root struct {
    DB DatabaseConfig `mapstructure:"db"`
}

type DatabaseConfig struct {
    Driver         string `mapstructure:"driver"`
    DataSourceName string `mapstructure:"data_source_name"`
}

func main() {
    var conf Root
    err := config.Load(&conf, "configs/config")
    if err != nil {
        panic(err)
    }
}

core-go/log & core-go/middleware

import (
    "github.com/core-go/config"
    "github.com/core-go/log"
    m "github.com/core-go/middleware"
    "github.com/gorilla/mux"
)

func main() {
    var conf app.Root
    config.Load(&conf, "configs/config")

    r := mux.NewRouter()

    log.Initialize(conf.Log)
    r.Use(m.BuildContext)
    logger := m.NewLogger()
    r.Use(m.Logger(conf.MiddleWare, log.InfoFields, logger))
    r.Use(m.Recover(log.ErrorMsg))
}

To configure to ignore the health check, use "skips":

middleware:
  skips: /health

About

This samples provided how we create connection and manipulate Golang with PosgreSQL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages