Skip to content

alexisvisco/kcd

Repository files navigation

KCD ?

KCD is a grandiose REST helper that wrap your shiny handler into a http handler that manages un-marshall, validating, errors, marshaling ...

Opinionated by default but fully customizable.

Features

  • Unmarshalling request structure with: body as JSON, query parameters, path parameters, header
  • Support default value for field in the request structure
  • Validating request structure with go-ozzo/ozzo-validation library
  • REST Error handling by using expectedsh/errors as the error library
  • Marshal response into a JSON

Example

examples/simple/main.go:

package main

import (
	"net/http"

	"github.com/go-chi/chi"
	"github.com/go-chi/chi/middleware"
	validation "github.com/go-ozzo/ozzo-validation/v4"
	"github.com/go-ozzo/ozzo-validation/v4/is"

	"github.com/expectedsh/kcd"
)

func main() {
	r := chi.NewRouter()
	r.Use(middleware.Logger)

	// kcd.Config.BindHook = ...

	r.Post("/{path}", kcd.Handler(CreateCustomer, http.StatusOK))

	_ = http.ListenAndServe(":3000", r)
}

type CreateCustomerInput struct {
	Name   string   `json:"name"`
	Emails []string `json:"emails"`
}

func (c CreateCustomerInput) Validate() error {
	return validation.ValidateStruct(&c,
		validation.Field(&c.Name, validation.Required, validation.Length(5, 20)),
		validation.Field(&c.Emails, validation.Each(is.Email)),
	)
}

func CreateCustomer(in *CreateCustomerInput) (CreateCustomerInput, error) {
	return *in, nil
}

Opinionated

KCD use:

Customizable

KCD works with a hooks & extractor system that are exposed and editable in the main package via the variable Config.

In this Config variable you have theses variables:

Variable Name Description
QueryExtractor Used to retrieve query parameter, works with http stdlib
PathExtractor Used to retrieve path variable, chi is used
HeaderExtractor Used to retrieve header value, works with http stdlib
ErrorHook This is the way the REST app will manage error, by default it return JSON error, errors (from expected) is used
RenderHook Use json as response
BindHook Use json tag of request struct retrieved with the body of the request
ValidateHook Use ozzo-validation to validate request struct