README ¶
Test
go test ./... -p=1 -count=1 -v
Integration in another project
go get bitbucket.org/amotus/rest-server
Usage
package handler
import (
"actions-service/app/model"
"log"
"net/http"
"bitbucket.org/amotus/rest-server"
"github.com/go-chi/render"
"gorm.io/gorm"
)
type ActionHandler struct {
db *gorm.DB
}
func NewActionHandler(db *gorm.DB) *ActionHandler {
return &ActionHandler{db: db}
}
func (a *ActionHandler) List(w http.ResponseWriter, r *http.Request) {
// gorm models
var actions []model.Action
result, pager := server.NewQuery(r.URL.Query()). // Parse query parameters to get a filters object
Apply(a.db). // Apply filters to the database query
Paginate(&actions) //Paginate the result, this call is required to execute the query
// result is a *gorm.DB object
if result.Error != nil {
server.RenderNotFound(w, r)
return
}
// format the response with the Fundamentum API standard
if errRender := render.Render(w, r, &server.SuccessResponse{StatusText: "success", Data: actions, Pager: pager}); errRender != nil {
log.Println("Error render Success response:", errRender)
}
}
Fundamentum API Standard
https://dimonoff.atlassian.net/wiki/spaces/F2/pages/412155960/API+Standard
TODO
- Add Batch Query support
- Cleanup Response helpers
License
Licensed under Apache License, Version 2.0 LICENSE.
Documentation ¶
Index ¶
- func RenderBadRequest(w http.ResponseWriter, r *http.Request, err error)
- func RenderModelNotFound(w http.ResponseWriter, r *http.Request, err error)
- func RenderNotFound(w http.ResponseWriter, r *http.Request)
- func RenderServerError(w http.ResponseWriter, r *http.Request, err error, code int)
- func RenderSuccess(w http.ResponseWriter, r *http.Request, response *SuccessResponse)
- func RenderUnprocessableEntity(w http.ResponseWriter, r *http.Request, err error)
- type ErrResponse
- type Query
- type SuccessResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RenderBadRequest ¶
func RenderBadRequest(w http.ResponseWriter, r *http.Request, err error)
func RenderModelNotFound ¶ added in v0.0.4
func RenderModelNotFound(w http.ResponseWriter, r *http.Request, err error)
func RenderNotFound ¶
func RenderNotFound(w http.ResponseWriter, r *http.Request)
func RenderServerError ¶ added in v0.0.5
func RenderSuccess ¶
func RenderSuccess(w http.ResponseWriter, r *http.Request, response *SuccessResponse)
func RenderUnprocessableEntity ¶
func RenderUnprocessableEntity(w http.ResponseWriter, r *http.Request, err error)
Types ¶
type ErrResponse ¶
type ErrResponse struct { Err error `json:"-"` // low-level runtime error HTTPStatusCode int `json:"-"` // http response status code StatusText string `json:"status"` // user-level status message AppCode int64 `json:"code,omitempty"` // application-specific error code ErrorText string `json:"error,omitempty"` // application-level error message, for debugging }
func (*ErrResponse) Render ¶
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error
type SuccessResponse ¶
type SuccessResponse struct { StatusText string `json:"status"` Data interface{} `json:"data,omitempty"` Pager interface{} `json:"pager,omitempty"` }
func (*SuccessResponse) Render ¶
func (rd *SuccessResponse) Render(http.ResponseWriter, *http.Request) error
Click to show internal directories.
Click to hide internal directories.