resource

package
v0.0.0-...-2c2c105 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = &ErrResponse{HTTPStatusCode: 404, StatusText: "Resource not found."}

Functions

func ArticleCtx

func ArticleCtx(next http.Handler) http.Handler

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 ErrInvalidRequest(err error) render.Renderer

func ErrRender

func ErrRender(err error) render.Renderer

func GenTons

func GenTons(w http.ResponseWriter, r *http.Request)

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 IsValidMatchType(k string) bool

func ListArticles

func ListArticles(w http.ResponseWriter, r *http.Request)

func NewArticleListResponse

func NewArticleListResponse(articles []*Article) []render.Renderer

func ParamArray

func ParamArray(r *http.Request, key string) []string

func ParamInt

func ParamInt(r *http.Request, key string) int

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/

func (*ArticleRequest) Bind

func (a *ArticleRequest) Bind(r *http.Request) error

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

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 Filter

type Filter struct {
	// contains filtered or unexported fields
}

type Filters

type Filters struct {
	// contains filtered or unexported fields
}

func FiltersFromRequest

func FiltersFromRequest(r *http.Request) (*Filters, error)

type RestArticle

type RestArticle struct {
	Db *mongo.Database
}

type User

type User struct {
	ID   int64  `json:"id"`
	Name string `json:"name"`
}

User data model

type UserPayload

type UserPayload struct {
	*User
	Role string `json:"role"`
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL