Documentation
¶
Index ¶
- Variables
- func BadRequest(w http.ResponseWriter, reason interface{})
- func Forbidden(w http.ResponseWriter, reason interface{})
- func InProgress(w http.ResponseWriter, result interface{})
- func RenderListWithPages(w http.ResponseWriter, pageQuery db.PageQuery, total int64, list interface{})
- func ServerError(w http.ResponseWriter)
- func Success(w http.ResponseWriter, result interface{})
- func Unauthorized(w http.ResponseWriter, reason interface{})
- func WriteJSON(w http.ResponseWriter, status int, data interface{})
- type BaseRow
- type Page
- type R
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ResultServerError predefined response for `http.StatusInternalServerError`. ResultServerError = &R{ Code: http.StatusInternalServerError, Message: "Request Failed", } // ResultBadRequest predefined response for `http.StatusBadRequest`. ResultBadRequest = &R{ Code: http.StatusBadRequest, Message: "Bad Request", } // ResultSuccess predefined response for `http.StatusOK`. ResultSuccess = &R{ Code: http.StatusOK, Message: "Ok", } // ResultNotFound predefined response for `http.StatusNotFound`. ResultNotFound = &R{ Code: http.StatusNotFound, Message: "Not Found", } ResultUnauthorized = &R{ Code: http.StatusUnauthorized, Message: "Action Unauthorized", } // ResultForbidden predefined response for `http.StatusForbidden`. ResultForbidden = &R{ Code: http.StatusForbidden, Message: "Forbidden", } // ResultConflict predefined response for `http.StatusConflict`. ResultConflict = &R{ Code: http.StatusConflict, Message: "Already created", } )
View Source
var PrettyMarshal bool
PrettyMarshal is a flag that enable marshalling with indent
Functions ¶
func BadRequest ¶
func BadRequest(w http.ResponseWriter, reason interface{})
BadRequest renders `ResultBadRequest` with `reason` as an error.
func Forbidden ¶
func Forbidden(w http.ResponseWriter, reason interface{})
Forbidden renders `ResultForbidden` with `reason` as an error.
func InProgress ¶
func InProgress(w http.ResponseWriter, result interface{})
InProgress renders `ResultAccepted` with `reason` as an message.
func RenderListWithPages ¶
func RenderListWithPages(w http.ResponseWriter, pageQuery db.PageQuery, total int64, list interface{})
func ServerError ¶
func ServerError(w http.ResponseWriter)
ServerError renders default http.StatusInternalServerError.
func Success ¶
func Success(w http.ResponseWriter, result interface{})
Success renders `result` as JSON with `http.StatusOK`.
func Unauthorized ¶
func Unauthorized(w http.ResponseWriter, reason interface{})
Unauthorized renders `ResultUnauthorized` with `reason` as an error.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, status int, data interface{})
WriteJSON writes some response as WriteJSON to the `http.ResponseWriter`.
Types ¶
type Page ¶
type Page struct {
Page uint64 `json:"page"`
PageSize uint64 `json:"pageSize"`
Order string `json:"order"`
Total int64 `json:"total"`
Records interface{} `json:"records"`
}
func (*Page) Render ¶
func (page *Page) Render(w http.ResponseWriter)
type R ¶
type R struct {
Code int `json:"errcode"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
Error interface{} `json:"errmsg,omitempty"`
}
Example:
``` go
func MyHandler(w http.ResponseWriter, r *http.Request) {
// some code ...
// ...
res := render.R{
Code: http.StatusOk,
Message: "User created",
}
res.Render(w)
return
}
``` Usage of predefined response: ``` go
func MyHandler(w http.ResponseWriter, r *http.Request) {
// some code ...
// ...
render.ResultBadRequest.SetError("Invalid email").Render(w)
return
}
```
func (*R) Render ¶
func (r *R) Render(w http.ResponseWriter)
Render writes current response as WriteJSON to the `http.ResponseWriter`.
Click to show internal directories.
Click to hide internal directories.