Documentation ¶
Index ¶
- Variables
- func ArticleCtx(next http.Handler) http.Handler
- func CreateArticle(w http.ResponseWriter, r *http.Request)
- func DeleteArticle(w http.ResponseWriter, r *http.Request)
- func ErrInvalidRequest(err error) render.Renderer
- func ErrRender(err error) render.Renderer
- func GenTons(w http.ResponseWriter, r *http.Request)
- func GetArticle(w http.ResponseWriter, r *http.Request)
- func IsValidMatchType(k string) bool
- func ListArticles(w http.ResponseWriter, r *http.Request)
- func NewArticleListResponse(articles []*Article) []render.Renderer
- func ParamArray(r *http.Request, key string) []string
- func ParamInt(r *http.Request, key string) int
- func SearchArticles(w http.ResponseWriter, r *http.Request)
- func UpdateArticle(w http.ResponseWriter, r *http.Request)
- type Article
- type ArticleRequest
- type ArticleResponse
- type ErrResponse
- type Filter
- type Filters
- type RestArticle
- type User
- type UserPayload
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = &ErrResponse{HTTPStatusCode: 404, StatusText: "Resource not found."}
Functions ¶
func ArticleCtx ¶
ArticleCtx middleware is used to load an Article object from the URL parameters passed through as the request. In case the Article could not be found, we stop here and return a 404.
func CreateArticle ¶
func CreateArticle(w http.ResponseWriter, r *http.Request)
CreateArticle persists the posted Article and returns it back to the client as an acknowledgement.
func DeleteArticle ¶
func DeleteArticle(w http.ResponseWriter, r *http.Request)
DeleteArticle removes an existing Article from our persistent store.
func ErrInvalidRequest ¶
func GetArticle ¶
func GetArticle(w http.ResponseWriter, r *http.Request)
GetArticle returns the specific Article. You'll notice it just fetches the Article right off the context, as its understood that if we made it this far, the Article must be on the context. In case its not due to a bug, then it will panic, and our Recoverer will save us.
func IsValidMatchType ¶
func ListArticles ¶
func ListArticles(w http.ResponseWriter, r *http.Request)
func NewArticleListResponse ¶
func SearchArticles ¶
func SearchArticles(w http.ResponseWriter, r *http.Request)
SearchArticles searches the Articles data for a matching article. It's just a stub, but you get the idea.
func UpdateArticle ¶
func UpdateArticle(w http.ResponseWriter, r *http.Request)
UpdateArticle updates an existing Article in our persistent store.
Types ¶
type Article ¶
type Article struct { ID string `json:"id"` UserID int64 `json:"user_id"` // the author Title string `json:"title"` Slug string `json:"slug"` }
Article data model. I suggest looking at https://upper.io for an easy and powerful data persistence adapter.
type ArticleRequest ¶
type ArticleRequest struct { *Article User *UserPayload `json:"user,omitempty"` ProtectedID string `json:"id"` // override 'id' json to have more control }
ArticleRequest is the request payload for Article data model.
NOTE: It's good practice to have well defined request and response payloads so you can manage the specific inputs and outputs for clients, and also gives you the opportunity to transform data on input or output, for example on request, we'd like to protect certain fields and on output perhaps we'd like to include a computed field based on other values that aren't in the data model. Also, check out this awesome blog post on struct composition: http://attilaolah.eu/2014/09/10/json-and-struct-composition-in-go/
type ArticleResponse ¶
type ArticleResponse struct { *Article User *UserPayload `json:"user,omitempty"` // We add an additional field to the response here.. such as this // elapsed computed property Elapsed int64 `json:"elapsed"` }
ArticleResponse is the response payload for the Article data model. See NOTE above in ArticleRequest as well.
In the ArticleResponse object, first a Render() is called on itself, then the next field, and so on, all the way down the tree. Render is called in top-down order, like a http handler middleware chain.
func NewArticleResponse ¶
func NewArticleResponse(article *Article) *ArticleResponse
func (*ArticleResponse) Render ¶
func (rd *ArticleResponse) Render(w http.ResponseWriter, r *http.Request) error
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 }
ErrResponse renderer type for handling all sorts of errors.
In the best case scenario, the excellent github.com/pkg/errors package helps reveal information on the error, setting it on Err, and in the Render() method, using it to set the application-specific error code in AppCode.
func (*ErrResponse) Render ¶
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error
type RestArticle ¶
type UserPayload ¶
func NewUserPayloadResponse ¶
func NewUserPayloadResponse(user *User) *UserPayload
func (*UserPayload) Bind ¶
func (u *UserPayload) Bind(r *http.Request) error
Bind on UserPayload will run after the unmarshalling is complete, its a good time to focus some post-processing after a decoding.
func (*UserPayload) Render ¶
func (u *UserPayload) Render(w http.ResponseWriter, r *http.Request) error