app

package
v0.0.0-...-521f081 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2017 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package app ties together application resources and handlers.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBadRequest returns status 400 Bad Request for malformed request body.
	ErrBadRequest = &ErrResponse{HTTPStatusCode: http.StatusBadRequest, StatusText: http.StatusText(http.StatusBadRequest)}

	// ErrUnauthorized returns 401 Unauthorized.
	ErrUnauthorized = &ErrResponse{HTTPStatusCode: http.StatusUnauthorized, StatusText: http.StatusText(http.StatusUnauthorized)}

	// ErrNotFound returns status 404 Not Found for invalid resource request.
	ErrNotFound = &ErrResponse{HTTPStatusCode: http.StatusNotFound, StatusText: http.StatusText(http.StatusNotFound)}

	// ErrInternalServerError returns status 500 Internal Server Error.
	ErrInternalServerError = &ErrResponse{HTTPStatusCode: http.StatusInternalServerError, StatusText: http.StatusText(http.StatusInternalServerError)}
)

Functions

func ErrInvalidRequest

func ErrInvalidRequest(err error) render.Renderer

ErrInvalidRequest returns status 422 Unprocessable Entity including error message.

func ErrRender

func ErrRender(err error) render.Renderer

ErrRender returns status 422 Unprocessable Entity rendering response error.

func ErrValidation

func ErrValidation(valErrors error) render.Renderer

ErrValidation returns status 422 Unprocessable Entity stating validation errors.

Types

type API

type API struct {
	Account *AccountResource
	Profile *ProfileResource
}

API provides application resources and handlers.

func NewAPI

func NewAPI(db *pg.DB) (*API, error)

NewAPI configures and returns application API.

func (*API) Router

func (a *API) Router() *chi.Mux

Router provides application routes.

type AccountResource

type AccountResource struct {
	Store AccountStore
}

AccountResource implements account management handler.

func NewAccountResource

func NewAccountResource(store AccountStore) *AccountResource

NewAccountResource creates and returns an account resource.

type AccountStore

type AccountStore interface {
	Get(id int) (*auth.Account, error)
	Update(*auth.Account) error
	Delete(*auth.Account) error
	UpdateToken(*auth.Token) error
	DeleteToken(*auth.Token) error
}

AccountStore defines database operations for account.

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.

func (*ErrResponse) Render

func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error

Render sets the application-specific error code in AppCode.

type ErrValidationResponse

type ErrValidationResponse struct {
	*ErrResponse
	Errors string `json:"errors,omitempty"`
}

ErrValidationResponse renderer for handling validation errors.

func (*ErrValidationResponse) Render

Render sets the application-specific error code in AppCode.

type ProfileResource

type ProfileResource struct {
	Store ProfileStore
}

ProfileResource implements profile management handler.

func NewProfileResource

func NewProfileResource(store ProfileStore) *ProfileResource

NewProfileResource creates and returns a profile resource.

type ProfileStore

type ProfileStore interface {
	Get(accountID int) (*models.Profile, error)
	Update(p *models.Profile) error
}

ProfileStore defines database operations for a profile.

Jump to

Keyboard shortcuts

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