go-classy: github.com/phonkee/go-classy Index | Files

package classy

import "github.com/phonkee/go-classy"

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

Package Files

api.go classy.go doc.go generic.go group.go logger.go mapping.go registrar.go routes.go utils.go view.go

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"}
)

func Debug Uses

func Debug()

Debug function is basically Regsitrar with debug enabled

func GetFuncName Uses

func GetFuncName(i interface{}) string

GetFuncName returns function name (primarily for logging reasons)

func JoinRoutes Uses

func JoinRoutes() multiroute

type BaseView Uses

type BaseView struct{}

BaseView is blank implementation of basic view

func (BaseView) Before Uses

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

Before blank implementation

func (*BaseView) Routes Uses

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

GetRoutes blank implementation

type BeforeFunc Uses

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 Uses

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

BoundMethod is representation of struct method

func (BoundMethod) String Uses

func (b BoundMethod) String() string

type Classy Uses

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 Uses

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

Group creates new group of multiple classy views

func New Uses

func New(view Viewer) Classy

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

type DetailView Uses

type DetailView struct {
    BaseView
}

DetailView is predefined struct for detail

func (DetailView) Routes Uses

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

Routes returns list of routes with predefined method maps

type GenericView Uses

type GenericView struct {
    BaseView
}

View is predefined struct for list views

func (GenericView) Routes Uses

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

Routes returns list of routes with predefined method maps

type ListView Uses

type ListView struct {
    BaseView
}

ListView is predefined struct for list views

func (ListView) Routes Uses

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

Routes returns list of routes with predefined method maps

type Mapping Uses

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 Uses

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

Do not forget on this

AVAILABLE_METHODS

type Registrar Uses

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 Uses

func Name(name string) Registrar

Name function returns Regsitrar without given name

func Path Uses

func Path(path string) Registrar

Path function creates Regsitrar with set Path

func Register Uses

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

Register function is basically Regsitrar without any options

func Use Uses

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

Use function is basically Regsitrar with given middlewares

type ResponseHandlerFunc Uses

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

support for custom http handlers that return response.Response

type SlugDetailView Uses

type SlugDetailView struct {
    BaseView
}

SlugDetailView is predefined struct for detail that handles id as slug

func (SlugDetailView) Routes Uses

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

Routes returns list of routes with predefined method maps

type SlugViewSet Uses

type SlugViewSet struct {
    ListView
    SlugDetailView
}

func (SlugViewSet) Before Uses

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

Before is blank implementation for ViewSet

func (SlugViewSet) Routes Uses

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

Routes returns combination of list and detail routes

type ViewSet Uses

type ViewSet struct {
    ListView
    DetailView
}

func (ViewSet) Before Uses

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

Before is blank implementation for ViewSet

func (ViewSet) Routes Uses

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

Routes returns combination of list and detail routes

type Viewer Uses

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.

Package classy imports 10 packages (graph) and is imported by 2 packages. Updated 2016-11-25. Refresh now. Tools for package owners.