a restful api service built with golang + echo framework + elephantSQL (postgresql cloud db)
git clone https://github.com/go-tutorials/go-echo-sql-tutorial.git
go run main.go
- create database document: https://www.elephantsql.com/docs/ (Note: use file data.sql (data -> data.sql) to get the sql query to create new database)
- get URI link for the app to connect: choose the instance database you want to use after created in ElephantSQL, click "Details" option in the left column. Then copy the URL in the "Details" section
- use URI in code: configs -> config.yml. Then paste the URI above to the sql.data_source_name
go get github.com/labstack/echo/v4
go get github.com/labstack/echo/v4/middleware
- 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
To check if the service is available.
{
"status": "UP",
"details": {
"sql": {
"status": "UP"
}
}
}
[
{
"id": "spiderman",
"username": "peter.parker",
"email": "peter.parker@gmail.com",
"phone": "0987654321",
"dateOfBirth": "1962-08-25T16:59:59.999Z"
},
{
"id": "wolverine",
"username": "james.howlett",
"email": "james.howlett@gmail.com",
"phone": "0987654321",
"dateOfBirth": "1974-11-16T16:59:59.999Z"
}
]
GET /users/wolverine
{
"id": "wolverine",
"username": "james.howlett",
"email": "james.howlett@gmail.com",
"phone": "0987654321",
"dateOfBirth": "1974-11-16T16:59:59.999Z"
}
{
"id": "wolverine",
"username": "james.howlett",
"email": "james.howlett@gmail.com",
"phone": "0987654321",
"dateOfBirth": "1974-11-16T16:59:59.999Z"
}
1
PUT /users/wolverine
{
"username": "james.howlett",
"email": "james.howlett@gmail.com",
"phone": "0987654321",
"dateOfBirth": "1974-11-16T16:59:59.999Z"
}
1
DELETE /users/wolverine
1
- 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
To check if the service is available, refer to core-go/health
{
"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
e := echo.New()
e.GET("/health", app.HealthHandler.Check)
To load the config from "config.yml", in "configs" folder
package main
import "github.com/core-go/core/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)
}
}
import (
"context"
"fmt"
"github.com/core-go/config"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"go-service/internal/app"
)
func main() {
var conf app.Root
e := echo.New()
e.Use(middleware.BodyDump(func(c echo.Context, reqBody, resBody []byte) {
fmt.Printf("Request Body: %v\n", string(reqBody))
fmt.Printf("Response Body: %v\n", string(resBody))
fmt.Printf("----------------------------------------\n")
}))
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "host=${host}, method=${method}, uri=${uri}, status=${status}, error=${error}, message=${message}\n",
}))
e.Use(middleware.Recover())
}
To configure to ignore the health check, use "skips":
middleware:
skips: /health