git clone https://github.com/go-tutorials/go-cassandra-sample.git
cd go-cassandra-sample
go run main.go
- 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": {
"cassandra": {
"status": "UP"
}
}
}
[
{
"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 /users/wolverine
{
"id": "wolverine",
"username": "james.howlett",
"email": "[email protected]",
"phone": "0987654321",
"dateOfBirth": "1974-11-16T16:59:59.999Z"
}
{
"id": "wolverine",
"username": "james.howlett",
"email": "[email protected]",
"phone": "0987654321",
"dateOfBirth": "1974-11-16T16:59:59.999Z"
}
1
PUT /users/wolverine
{
"username": "james.howlett",
"email": "[email protected]",
"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
cqlChecker := s.NewSqlHealthChecker(db)
healthHandler := health.NewHealthHandler(cqlChecker)
To handler routing
r := mux.NewRouter()
r.HandleFunc("/health", healthHandler.Check).Methods("GET")
To load the config from "config.yml", in "configs" folder
package main
import "github.com/core-go/config"
type Root struct {
Server sv.ServerConfig `mapstructure:"server"`
Cql Cassandra `mapstructure:"cassandra"`
Log log.Config `mapstructure:"log"`
MiddleWare mid.LogConfig `mapstructure:"middleware"`
}
type Cassandra struct {
PublicIp string `mapstructure:"public_ip"`
UserName string `mapstructure:"user_name"`
Password string `mapstructure:"password"`
}
func main() {
var conf Root
err := config.Load(&conf, "configs/config")
if err != nil {
panic(err)
}
}
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.NewStructuredLogger()
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