classy

package module
v0.0.0-...-dbf6b07 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2016 License: MIT Imports: 10 Imported by: 2

README

Classy

Class based views, inspired by django class based views. The functionality is simple yet powerful. Have in mind that only one instance of view exists so you need to be be sure that your view is goroutine safe.

API

    // enable debug for whole classy
    classy.Debug()

    // or enable for given registering
    classy.Use(middleware1, middleware2).Debug().Name("api:{name}").Register(
        router,
        classy.New(&ProductDetailView{}).Use(middleware3),
        classy.New(&ProductApproveView{}).Path("/approve").Name("approve"),
    )

    // lets register some views
    classy.Register(
        router,
        classy.New(&ProductDetailView{}).Path("/product/"),
        classy.New(&ProductApproveView{}).Path("/product/approve").Debug(),
    )

    // or provide path for all registered classy views `api`
    classy.Path("/api").Register(
        router,
        classy.New(&ProductDetailView{}).Path("/product/"),
        classy.New(&ProductApproveView{}).Path("/product/approve").Debug(),
    )

    // set response as not allowed (TODO)
    na := response.New(http.StatusMethodNotAllowed)

    classy.Path("/api").MethodNotAllowed(na).Register(
        router,
        classy.New(&ProductDetailView{}).Path("/product/"),
        classy.New(&ProductApproveView{}).Path("/product/approve").Debug(),
    )

    // support for Groups
    classy.Path("/api").Register(
        router,
        classy.Group(
            "/product",
            classy.New(&ProductDetailView{}),
            classy.New(&ProductApproveView{}).Path("/approve").Debug(),
        ),
    )
    

    // method not allowed (TODO)
    classy.Path("/api").Register(
        router,
        classy.New(&ProductDetailView{}).Path("/product/").MethodNotAllowed(na),
        classy.New(&ProductApproveView{}).Path("/product/approve").Debug().MethodNotAllowed(na),
    )

Every view needs to have Routes method that returns mapping:

    func (l View) Routes() map[string]Mapping {
        return map[string]Mapping {
            "/": NewMapping(
                []string("GET": "List"},
                []string("POST": "Post"},
            )
        }
    }

If you embed multiple views you can use shorthand to merge routes:

    return JoinRoutes().
        Add(Detail.Routes(), "{name}_detail").
        Add(List.Routes(), "{name}_list").
        Get()

Documentation

Overview

Api provides simple functions that return registrar.

The api is fairly simple and can be used in many different ways.

Example:

	classy.Name("api:{{view}}).Register(
        router,
        classy.New(&ProductDetailView{}),
	)

    classy.Debug().Use(middleware1, middleware2).Name("api:{{view}}").Register(
        router,
        classy.New(&ProductDetailView{}).Use(middleware3),
        classy.New(&ProductApproveView{}).Path("/approve").Name("approve"),
    )

    classy.Register(
        router,
        classy.New(&ProductDetailView{}).Path("/product/").Name(),
        classy.New(&ProductApproveView{}).Path("/product/approve").Debug(),
    )

Classy package

Support for class based views. Inspired by django class based views. Every structure can be view when it provides needed methods. Classy has som pre built configured base views, so you don't need to create one. They are built with rest principles in mind. Design of classy is that you can provide mapping from http methods to view methods. Classy then registers views to gorilla mux router. Classy uses reflection quite a lot, but the advantage is that it's used only during registration to router. During the run of server it doesn't affects speed and performance

Classy has also support for "ViewSet" as we know them from django rest framework, so you can combine list/detail view in single struct.

Example:

Let's create users list class based view. We will use predefined ListView.

type UserListView struct {
	ListView
}

registrar provides object that handles all the registration. It is also used as shorthands in top level api.

All interfaces used in classy module

Index

Constants

This section is empty.

Variables

View Source
var (
	// List of available (supported) http methods. You can extend this with new methods
	AVAILABLE_METHODS = []string{"GET", "POST", "PUT", "PATCH", "OPTIONS", "TRACE", "HEAD", "DELETE"}
)

Functions

func Debug

func Debug()

Debug function is basically Regsitrar with debug enabled

func GetFuncName

func GetFuncName(i interface{}) string

GetFuncName returns function name (primarily for logging reasons)

func JoinRoutes

func JoinRoutes() multiroute

Types

type BaseView

type BaseView struct{}

BaseView is blank implementation of basic view

func (BaseView) Before

func (v BaseView) Before(w http.ResponseWriter, r *http.Request) response.Response

Before blank implementation

func (*BaseView) Routes

func (v *BaseView) Routes() map[string]Mapping

GetRoutes blank implementation

type BeforeFunc

type BeforeFunc func(w http.ResponseWriter, r *http.Request) response.Response

Before func is called before any view is called. If Response is returned it's written and stopped execution

type BoundMethod

type BoundMethod struct {
	Handlerfunc  http.HandlerFunc
	Path         string
	Name         string
	Method       string
	StructMethod string
}

BoundMethod is representation of struct method

func (BoundMethod) String

func (b BoundMethod) String() string

type Classy

type Classy interface {

	// Debug enables debugging of classy view
	Debug() Classy

	// Name sets name of given classy view (route will be registered under this name)
	Name(name string) Classy

	// GetName returns name
	GetName() string

	// Path sets path (optional)
	Path(path string) Classy

	// Use adds middlewares for given classy view
	Use(middlewares ...alice.Constructor) Classy
	// contains filtered or unexported methods
}

Classy is struct that wraps classy view and can register views to gorilla mux

func Group

func Group(path string, views ...Classy) (result Classy)

Group creates new group of multiple classy views

func New

func New(view Viewer) Classy

New instantiates new classy view, that is able to introspect view for http methods.

type DetailView

type DetailView struct {
	BaseView
}

DetailView is predefined struct for detail

func (DetailView) Routes

func (d DetailView) Routes() (result map[string]Mapping)

Routes returns list of routes with predefined method maps

type GenericView

type GenericView struct {
	BaseView
}

View is predefined struct for list views

func (GenericView) Routes

func (l GenericView) Routes() map[string]Mapping

Routes returns list of routes with predefined method maps

type ListView

type ListView struct {
	BaseView
}

ListView is predefined struct for list views

func (ListView) Routes

func (l ListView) Routes() map[string]Mapping

Routes returns list of routes with predefined method maps

type Mapping

type Mapping interface {

	// Add adds mapping from http mehod to struct method names
	Add(httpmethod string, names ...string) Mapping

	// Debug enables debugging on mapping
	Debug() Mapping

	// GetMap returns map of struct method to http method
	Get() map[string]string

	// set name
	Name(string) Mapping

	// returns name
	GetName() string

	// Renames field
	Rename(from, to string) Mapping
	// contains filtered or unexported methods
}

func NewMapping

func NewMapping(items ...[]string) (result Mapping)

Do not forget on this

AVAILABLE_METHODS

type Registrar

type Registrar interface {

	// Debug enables debug
	Debug() Registrar

	// MethodNotAllowed sets response that will be returned
	MethodNotAllowed(response.Response) Registrar

	// Name sets name, this is a text/template that injects variable `view` with view name in snakecase
	Name(string) Registrar

	// Path sets path to registrar, this path will be prepended to all classy views paths
	Path(string) Registrar

	// Register registers all classy views to router
	// In the future we will probably set router parameter as interface{} and we will do type switch with concrete
	// impementations
	Register(router *mux.Router, views ...Classy) Registrar

	// Use sets middlewares to registrar (all classy views will use this middlewares)
	Use(middleware ...alice.Constructor) Registrar
	// contains filtered or unexported methods
}

Registrar interface

This interface is responsible for registering views.

func Name

func Name(name string) Registrar

Name function returns Regsitrar without given name

func Path

func Path(path string) Registrar

Path function creates Regsitrar with set Path

func Register

func Register(router *mux.Router, views ...Classy) Registrar

Register function is basically Regsitrar without any options

func Use

func Use(middleware ...alice.Constructor) Registrar

Use function is basically Regsitrar with given middlewares

type ResponseHandlerFunc

type ResponseHandlerFunc func(http.ResponseWriter, *http.Request) response.Response

support for custom http handlers that return response.Response

type SlugDetailView

type SlugDetailView struct {
	BaseView
}

SlugDetailView is predefined struct for detail that handles id as slug

func (SlugDetailView) Routes

func (d SlugDetailView) Routes() map[string]Mapping

Routes returns list of routes with predefined method maps

type SlugViewSet

type SlugViewSet struct {
	ListView
	SlugDetailView
}

func (SlugViewSet) Before

func (v SlugViewSet) Before(w http.ResponseWriter, r *http.Request) response.Response

Before is blank implementation for ViewSet

func (SlugViewSet) Routes

func (v SlugViewSet) Routes() map[string]Mapping

Routes returns combination of list and detail routes

type ViewSet

type ViewSet struct {
	ListView
	DetailView
}

func (ViewSet) Before

func (v ViewSet) Before(w http.ResponseWriter, r *http.Request) response.Response

Before is blank implementation for ViewSet

func (ViewSet) Routes

func (v ViewSet) Routes() (result map[string]Mapping)

Routes returns combination of list and detail routes

type Viewer

type Viewer interface {
	Before(w http.ResponseWriter, r *http.Request) response.Response

	// Return used route map.
	Routes() map[string]Mapping
}

View

Interface for ClassyView, structs will be so-called class based view.

Jump to

Keyboard shortcuts

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