handler

package
v0.0.0-...-abdde1b Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2020 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const CtxKeyToken contextKey = "token"

Variables

This section is empty.

Functions

func EncodeEntities

func EncodeEntities(entities, response interface{}, responseField string) error

EncodeEntities generic function to map a slice of database entities to a response field of a response struct All entity fields that will be mapped need to be public, response target field should be same type with entities slice, response MUST be a pointer to a response struct

func EncodeEntity

func EncodeEntity(entity, response interface{}) error

EncodeEntity generic function to map a database entity to a response object All fields in entity and response that needed to be mapped should be public Response MUST be a pointer to a response struct

func Routes

func Routes(h *Handler) *chi.Mux

Routes initializes api routes and shared middleware

Types

type APIError

type APIError struct {
	Message    string
	StatusCode int
}

APIError

func NewAPIError

func NewAPIError(message string, status int) APIError

func (APIError) Error

func (a APIError) Error() string

Error implements error interface

type ErrorResponse

type ErrorResponse struct {
	Message       string `json:"error"`
	StatusCode    int    `json:"statusCode"`
	StatusMessage string `json:"statusMessage"`
}

ErrorResponse object to map error response

type Handler

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

func NewHandler

func NewHandler(db *database.Database, c *config.Config, l *logger.Logger) *Handler

func (Handler) AuthorizationMiddleware

func (h Handler) AuthorizationMiddleware(next http.Handler) http.Handler

Authorization middleware assign to all routes that require users to be signed in

func (Handler) ContentTypeMiddleware

func (h Handler) ContentTypeMiddleware(next http.Handler) http.Handler

HeadersMiddleware sets pre defined headers

func (Handler) CorsMiddleware

func (h Handler) CorsMiddleware(next http.Handler) http.Handler

HeadersMiddleware sets pre defined headers

func (Handler) Create

func (h Handler) Create(w http.ResponseWriter, r *http.Request)

Create a new recipe

func (*Handler) Recipe

func (h *Handler) Recipe(w http.ResponseWriter, r *http.Request)

Recipe godoc @Summary Get a recipe @Description Get a recipe by ID @ID get-recipe-by-int @Accept application/x-www-form-urlencoded @Produce json @Param id path int true "Recipe ID" @Success 200 {object} handler.RecipeResponseItem @Failure 400 {object} handler.ErrorResponse @Failure 404 {object} handler.ErrorResponse @Failure 500 {object} handler.ErrorResponse @Security ApiKeyAuth @Router /recipes/{id} [get]

func (Handler) Recipes

func (h Handler) Recipes(w http.ResponseWriter, r *http.Request)

Recipes godoc @Summary Get recipes @Description Get a list of recipes @ID get-recipes @Accept application/x-www-form-urlencoded @Produce json @Success 200 {object} handler.RecipesResponse @Failure 400 {object} handler.ErrorResponse @Failure 404 {object} handler.ErrorResponse @Failure 500 {object} handler.ErrorResponse @Security ApiKeyAuth @Router /recipes [get]

func (Handler) SignIn

func (h Handler) SignIn(w http.ResponseWriter, r *http.Request)

SignIn godoc @Summary user sign in @Description user sign in @ID user-sign-in @Accept json @Produce json @Param credentials body handler.SignInRequest false "credentials payload" @Success 200 {object} handler.TokenResponse @Failure 400 {object} handler.ErrorResponse @Failure 404 {object} handler.ErrorResponse @Failure 500 {object} handler.ErrorResponse @Router /user/signin [post]

func (Handler) SignUp

func (h Handler) SignUp(w http.ResponseWriter, r *http.Request)

SignUp godoc @Summary user sign up @Description user sign up @ID user-sign-up @Accept json @Produce json @Param body body handler.SignUpRequest true "sign up payload" @Success 200 {object} handler.TokenResponse @Failure 400 {object} handler.ErrorResponse @Failure 404 {object} handler.ErrorResponse @Failure 500 {object} handler.ErrorResponse @Router /user/signup [post]

func (Handler) User

func (h Handler) User(w http.ResponseWriter, r *http.Request)

User godoc @Summary user profile @Description Get user profile info @ID user-profile @Accept application/x-www-form-urlencoded @Produce json @Success 200 {object} handler.UserProfileResponse @Failure 400 {object} handler.ErrorResponse @Failure 404 {object} handler.ErrorResponse @Failure 500 {object} handler.ErrorResponse @Security ApiKeyAuth @Router /user [get]

type IngredientResponse

type IngredientResponse []IngredientResponseItem

IngredientResponseItem object to map slice of ingredients

type IngredientResponseItem

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

IngredientResponseItem object to map single ingredient

type Metadata

type Metadata struct {
	Total int64
}

Metadata

type RecipeCreateRequest

type RecipeCreateRequest struct {
	Title       string   `json:"title" validate:"required,min=2"`
	URL         string   `json:"url" validate:"required,min=10"`
	Thumbnail   string   `json:"thumbnail"`
	Ingredients []string `json:"ingredients" validate:"required,max=30,min=1"`
}

CreateRecipeRequest object to map incoming request for Create handler

type RecipeResponseItem

type RecipeResponseItem struct {
	ID          int64              `json:"id"`
	Title       string             `json:"title"`
	Href        string             `json:"href"`
	Ingredients IngredientResponse `json:"ingredients"`
	Thumbnail   string             `json:"thumbnail"`
	CreatedAt   string             `json:"createdAt"`
	UpdatedAt   string             `json:"updatedAt"`
}

RecipeResponseItem object to map a recipe item

type RecipeResponseItems

type RecipeResponseItems []RecipeResponseItem

RecipeResponseItem object to map recipe items

type RecipesRequest

type RecipesRequest struct {
	Page        uint64   `schema:"page" validate:"omitempty,min=1"`
	Term        string   `schema:"term" validate:"omitempty,min=3"`
	Ingredients []string `schema:"ingredient" validate:"omitempty,max=5"`
}

RecipesRequest object to map incoming request for Recipes handler

type RecipesResponse

type RecipesResponse struct {
	Data     *RecipeResponseItems `json:"data"`
	Metadata Metadata             `json:"metadata"`
}

RecipeResponse recipe response object

type SignInRequest

type SignInRequest struct {
	Username string `json:"username" validate:"required,min=1,max=20"`
	Password string `json:"password" validate:"required,min=1,max=32"`
}

SignUpRequest object to map sign up incoming request

type SignUpRequest

type SignUpRequest struct {
	Email          string `json:"email" validate:"required,email"`
	FullName       string `json:"fullName"`
	Username       string `json:"username" validate:"required,min=5,max=20"`
	Password       string `json:"password" validate:"required,min=8,max=32"`
	RepeatPassword string `json:"repeatPassword" validate:"eqfield=Password"`
}

SignUpRequest object to map sign up incoming request

type Token

type Token struct {
	UserID   int64  `json:"uid"`
	Username string `json:"uname"`
	jwt.StandardClaims
}

Token object to map incoming authorization bearer token

type TokenResponse

type TokenResponse struct {
	Token string `json:"token"`
}

TokenResponse map token response

type UserProfileResponse

type UserProfileResponse struct {
	ID        int64
	Username  string `json:"username"`
	FullName  string `json:"fullName"`
	Email     string `json:"email"`
	Active    bool   `json:"active"`
	CreatedAt string `json:"createdAt"`
	UpdatedAt string `json:"updatedAt"`
}

UserProfileResponse object to map user profile response

func NewUserProfileResponse

func NewUserProfileResponse(u database.User) UserProfileResponse

NewUserProfileResponse creates a new UserProfileResponse object

Jump to

Keyboard shortcuts

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