router

package
v0.0.0-...-699b668 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2018 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrRouteStringFormat is returned when the route string is of the wrong format
	ErrRouteStringFormat = errors.New("wrong route string, example is\" get,post;/hello/world;Hello\"")
)

Functions

This section is empty.

Types

type Middleware

type Middleware struct {
	Type MiddlewareType
	// contains filtered or unexported fields
}

Middleware is the utron middleware

func (*Middleware) ToHandler

func (m *Middleware) ToHandler(ctx *base.Context) func(http.Handler) http.Handler

ToHandler returns a func(http.Handler) http.Handler from the Middleware. Utron uses alice to chain middleware.

Use this method to get alice compatible middleware.

type MiddlewareType

type MiddlewareType int

MiddlewareType is the kind of middleware. Utron support middleware with variary of signatures.

const (
	//PlainMiddleware is the middleware with signature
	// func(http.Handler)http.Handler
	PlainMiddleware MiddlewareType = iota

	//CtxMiddleware is the middlewate with signature
	// func(*base.Context)error
	CtxMiddleware
)

type Options

type Options struct {
	Model        *models.Model
	View         view.View
	Config       *config.Config
	Log          logger.Logger
	SessionStore sessions.Store
}

Options additional settings for the router.

type Router

type Router struct {
	*mux.Router

	Options *Options
	// contains filtered or unexported fields
}

Router registers routes and handlers. It embeds gorilla mux Router

func NewRouter

func NewRouter(app ...*Options) *Router

NewRouter returns a new Router, if app is passed then it is used

func (*Router) Add

func (r *Router) Add(ctrlfn func() controller.Controller, middlewares ...interface{}) error

Add registers ctrl. It takes additional comma separated list of middleware. middlewares are of type

func(http.Handler)http.Handler
or
func(*base.Context)error

utron uses the alice package to chain middlewares, this means all alice compatible middleware works out of the box

func (*Router) LoadRoutes

func (r *Router) LoadRoutes(cfgPath string)

LoadRoutes searches for the route file i the cfgPath. The order of file lookup is as follows.

  • routes.json
  • routes.toml
  • routes.yml
  • routes.hcl

func (*Router) LoadRoutesFile

func (r *Router) LoadRoutesFile(file string) error

LoadRoutesFile loads routes from a json file. Example of the routes file.

{
	"routes": [
		"get,post;/hello;Sample.Hello",
		"get,post;/about;Hello.About"
	]
}

supported formats are json, toml, yaml and hcl with extension .json, .toml, .yml and .hcl respectively.

TODO refactor the decoding part to a separate function? This part shares the same logic as the one found in NewConfig()

func (*Router) Static

func (r *Router) Static(prefix string, h http.FileSystem)

Static registers static handler for path perfix

Jump to

Keyboard shortcuts

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