Controller-based REST API server container for Golang with Postgres support
- Uses GORM v1.21.13 for DB interaction with Postgres
- Uses Gorilla Mux v1.8.0 for routing
- Uses logmatic v0.4.0 for nice server logs
- Default port is 8080
- Default base path is /api
Documentation here: https://godoc.org/github.com/mborders/requiem
s := requiem.NewServer(... controllers)
s.Start()
s := requiem.NewServer(... controllers)
s.EnableDB = true
s.Start()
s := requiem.NewServer(... controllers)
s.Port = 9090
s.BasePath = "/rest"
s.Start()
type MyController struct {
DB *gorm.DB
}
type Response struct {
Message string
}
type CreateRequest struct {
SomeValue string
}
func (c MyController) getStuff(ctx requiem.HTTPContext) {
m := Response{Message: "Hello, world!"}
ctx.SendJSON(res)
}
func (c MyController) createStuff(ctx requiem.HTTPContext) {
m := ctx.Body.(*CreateRequest)
fmt.Println("Value: %s", m.SomeValue)
ctx.SendStatus(Http.StatusNoContent)
}
func AuthInterceptor(ctx HTTPContext) bool {
// Example:
// 1) Check if user is authenticated
// 2) Return false if not
// 3) Use ctx.SetAttribute() to pass user claim
return true
}
func (c MyController) Load(router *requiem.Router) {
c.DB = router.DB
r := router.NewRestRouter("/stuff")
r.Get("/", c.getStuff)
r.Post("/", c.createStuff)
// Use AuthInterceptor
r.Get("/interceptor", func(ctx HTTPContext) {
ctx.SendStatus(http.StatusOK)
}, AuthInterceptor)
}
DB_HOST
DB_PORT
DB_NAME
DB_USERNAME
DB_PASSWORD