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 ¶
- Variables
- func Debug()
- func GetFuncName(i interface{}) string
- func JoinRoutes() multiroute
- type BaseView
- type BeforeFunc
- type BoundMethod
- type Classy
- type DetailView
- type GenericView
- type ListView
- type Mapping
- type Registrar
- type ResponseHandlerFunc
- type SlugDetailView
- type SlugViewSet
- type ViewSet
- type Viewer
Constants ¶
This section is empty.
Variables ¶
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 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
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
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 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 }
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 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