app

package
v0.0.0-...-71d5ba4 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2018 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Next is the function that executed when `ctx.Next()` is called. It can be changed to a customized one if needed (very advanced usage).

See `DefaultNext` for more information about this and why it's exported like this.

Functions

func DefaultNext

func DefaultNext(ctx Context)

DefaultNext is the default function that executed on each middleware if `ctx.Next()` is called.

DefaultNext calls the next handler from the handlers chain by registration order, it should be used inside a middleware.

It can be changed to a customized one if needed (very advanced usage).

Developers are free to customize the whole or part of the Context's implementation by implementing a new `context.Context` (see https://github.com/kataras/iris/tree/master/_examples/routing/custom-context) or by just override the `context.Next` package-level field, `context.DefaultNext` is exported in order to be able for developers to merge your customized version one with the default behavior as well.

func Do

func Do(ctx Context, handlers Handlers)

Do calls the SetHandlers(handlers) and executes the first handler, handlers should not be empty.

It's used by the router, developers may use that to replace and execute handlers immediately.

func HandlerName

func HandlerName(h Handler) string

HandlerName returns the name, the handler function informations. Same as `context.HandlerName`.

func IsController

func IsController(i interface{}) bool

func IsOrmModel

func IsOrmModel(i interface{}) bool

func Mix

func Mix(method string, path interface{}) string

Types

type Application

type Application interface {
	Name() string
	Run()
	ShutDown()

	// mvc objects
	RegisterController(...interface{})

	// modules
	AddModule(module.Module)
	GetModule(module.Module) module.Module
	RPC() module.RPCModule
	Service() module.ServiceModule
	Log() module.LogModule
	MySql() module.MySqlModule
	Mongo() module.MongoModule
	Config() utils.ValueMap

	// 使用中间件,中间件将在其他Handler之前执行
	Use(middleware ...Handler)
	GlobalMiddleWares() Handlers

	RpcCtxPool() *Pool
	ServiceCtxPool() *Pool
	WebApiCtxPool() *Pool

	// 系统配置
	BingoConfig() *config.BingoConfig
	// 获取运行环境
	Env() string
}

func New

func New() Application

type BaseMongoModel

type BaseMongoModel struct {
	Id bson.ObjectId `bson:"_id"`
}

type BaseMysqlOrmModel

type BaseMysqlOrmModel struct {
	Id uint32 `gorm:"primary_key"`
	// contains filtered or unexported fields
}

func (*BaseMysqlOrmModel) DB

func (m *BaseMysqlOrmModel) DB() *gorm.DB

func (*BaseMysqlOrmModel) Del

func (m *BaseMysqlOrmModel) Del()

从数据库移除,ID必须存在

func (*BaseMysqlOrmModel) DelInTx

func (m *BaseMysqlOrmModel) DelInTx(tx *gorm.DB)

从数据库移除,ID必须存在

func (*BaseMysqlOrmModel) FieldFromString

func (m *BaseMysqlOrmModel) FieldFromString(s string, f interface{})

func (*BaseMysqlOrmModel) FieldToString

func (m *BaseMysqlOrmModel) FieldToString(f interface{}) string

func (*BaseMysqlOrmModel) Init

func (m *BaseMysqlOrmModel) Init(db *gorm.DB, self interface{})

func (*BaseMysqlOrmModel) Sync

func (m *BaseMysqlOrmModel) Sync(cols ...interface{})

更新到数据库

func (*BaseMysqlOrmModel) SyncInTx

func (m *BaseMysqlOrmModel) SyncInTx(tx *gorm.DB, cols ...interface{})

更新到数据库

type Context

type Context interface {
	// BeginRequest is executing once for each request
	// it should prepare the (new or acquired from pool) context's fields for the new request.
	//
	// To follow the iris' flow, developer should:
	// 1. reset handlers to nil
	// 2. reset values to empty
	// 3. reset sessions to nil
	// 4. reset response writer to the http.ResponseWriter
	// 5. reset request to the *http.Request
	// and any other optional steps, depends on dev's application type.
	BeginRequest()
	// EndRequest is executing once after a response to the request was sent and this context is useless or released.
	//
	// To follow the iris' flow, developer should:
	// 1. flush the response writer's result
	// 2. release the response writer
	// and any other optional steps, depends on dev's application type.
	EndRequest()
	// returns the unique id
	Id() uint32
	// Do calls the SetHandlers(handlers)
	// and executes the first handler,
	// handlers should not be empty.
	//
	// It's used by the router, developers may use that
	// to replace and execute handlers immediately.
	Do(Handlers)
	// AddHandler can add handler(s)
	// to the current request in serve-time,
	// these handlers are not persistenced to the router.
	//
	// Router is calling this function to add the route's handler.
	// If AddHandler called then the handlers will be inserted
	// to the end of the already-defined route's handler.
	//
	AddHandler(...Handler)
	// SetHandlers replaces all handlers with the new.
	SetHandlers(Handlers)
	// Handlers keeps tracking of the current handlers.
	Handlers() Handlers
	// HandlerIndex sets the current index of the
	// current context's handlers chain.
	// If -1 passed then it just returns the
	// current handler index without change the current index.rns that index, useless return value.
	//
	// Look Handlers(), Next() and StopExecution() too.
	HandlerIndex(n int) int
	// Proceed is an alternative way to check if a particular handler
	// has been executed and called the `ctx.Next` function inside it.
	// This is useful only when you run a handler inside
	// another handler. It justs checks for before index and the after index.
	//
	// A usecase example is when you want to execute a middleware
	// inside controller's `BeginRequest` that calls the `ctx.Next` inside it.
	// The Controller looks the whole flow (BeginRequest, method handler, EndRequest)
	// as one handler, so `ctx.Next` will not be reflected to the method handler
	// if called from the `BeginRequest`.
	//
	// Although `BeginRequest` should NOT be used to call other handlers,
	// the `BeginRequest` has been introduced to be able to set
	// common data to all method handlers before their execution.
	// Controllers can accept middleware(s) from the MVC's Application's Router as normally.
	//
	// That said let's see an example of `ctx.Proceed`:
	//
	// var authMiddleware = basicauth.New(basicauth.Config{
	// 	Users: map[string]string{
	// 		"admin": "password",
	// 	},
	// })
	//
	// func (c *UsersController) BeginRequest(ctx iris.Context) {
	// 	if !ctx.Proceed(authMiddleware) {
	// 		ctx.StopExecution()
	// 	}
	// }
	// This Get() will be executed in the same handler as `BeginRequest`,
	// internally controller checks for `ctx.StopExecution`.
	// So it will not be fired if BeginRequest called the `StopExecution`.
	// func(c *UsersController) Get() []models.User {
	//	  return c.Service.GetAll()
	//}
	// Alternative way is `!ctx.IsStopped()` if middleware make use of the `ctx.StopExecution()` on failure.
	Proceed(Handler) bool
	// HandlerName returns the current handler's name, helpful for debugging.
	HandlerName() string
	// Next calls all the next handler from the handlers chain,
	// it should be used inside a middleware.
	//
	// Note: Custom context should override this method in order to be able to pass its own context.Context implementation.
	Next()
	// NextOr checks if chain has a next handler, if so then it executes it
	// otherwise it sets a new chain assigned to this Context based on the given handler(s)
	// and executes its first handler.
	//
	// Returns true if next handler exists and executed, otherwise false.
	//
	// Note that if no next handler found and handlers are missing then
	// it sends a Status Not Found (404) to the client and it stops the execution.
	NextOr(handlers ...Handler) bool
	// NextOrNotFound checks if chain has a next handler, if so then it executes it
	// otherwise it sends a Status Not Found (404) to the client and stops the execution.
	//
	// Returns true if next handler exists and executed, otherwise false.
	NextOrNotFound() bool
	// NextHandler returns (it doesn't execute) the next handler from the handlers chain.
	//
	// Use .Skip() to skip this handler if needed to execute the next of this returning handler.
	NextHandler() Handler
	// Skip skips/ignores the next handler from the handlers chain,
	// it should be used inside a middleware.
	Skip()
	// StopExecution if called then the following .Next calls are ignored,
	// as a result the next handlers in the chain will not be fire.
	StopExecution()
	// IsStopped checks and returns true if the current position of the Context is 255,
	// means that the StopExecution() was called.
	IsStopped() bool

	App() Application

	LogE(format string, v ...interface{})
	LogD(format string, v ...interface{})
	LogW(format string, v ...interface{})
	LogI(format string, v ...interface{})

	Logger(name string) log.Logger
}

func NewContext

func NewContext(app Application) Context

NewContext returns the default, internal, context implementation. You may use this function to embed the default context implementation to a custom one.

This context is received by the context pool.

type Controller

type Controller interface {
	Route(builder RouterBuilder)
}

type Handler

type Handler func(Context)

type Handlers

type Handlers []Handler

Handlers is just a type of slice of []Handler.

See `Handler` for more.

type MessageBodyWrapper

type MessageBodyWrapper struct {
	RawContent net.MessageBody
	Codec      codec.Codec
}

func (*MessageBodyWrapper) Unmarshal

func (c *MessageBodyWrapper) Unmarshal(v interface{})

type MysqlOrmModel

type MysqlOrmModel interface {
	Init(db *gorm.DB, self interface{})
	DB() *gorm.DB
	Sync(cols ...interface{})
	SyncInTx(tx *gorm.DB, cols ...interface{})
	Del()
	DelInTx(tx *gorm.DB)
	FieldToString(f interface{}) string
	FieldFromString(s string, f interface{})
}

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool is the context pool, it's used inside router and the framework by itself.

It's the only one real implementation inside this package because it used widely.

func NewPool

func NewPool(newFunc func() Context) *Pool

New creates and returns a new context pool.

func (*Pool) Acquire

func (c *Pool) Acquire() Context

Acquire returns a Context from pool. See Release.

func (*Pool) Attach

func (c *Pool) Attach(newFunc func() Context)

Attach changes the pool's return value Context.

The new Context should explicitly define the `Next()` and `Do(context.Handlers)` functions.

Example: https://github.com/kataras/iris/blob/master/_examples/routing/custom-context/method-overriding/main.go

func (*Pool) Release

func (c *Pool) Release(ctx Context)

Release puts a Context back to its pull, this function releases its resources. See Acquire.

func (*Pool) ReleaseLight

func (c *Pool) ReleaseLight(ctx Context)

ReleaseLight will just release the object back to the pool, but the clean method is caller's responsibility now, currently this is only used on `SPABuilder`.

type Router

type Router interface {
	Handle(key string, handlers ...Handler)
	Handlers(kind string) map[string]Handlers
	OnHandleRequest(ctx Context)
}

func NewRouter

func NewRouter() Router

type RouterBuilder

type RouterBuilder interface {
	HandleWebApi(path interface{}, method string, middleware ...Handler)
	HandleRPCMethod(method string, middleware ...Handler)
	HandleServiceMessage(msgId interface{}, method string, middleware ...Handler)
	Build()
}

type RpcContext

type RpcContext struct {
	Context
	Method string
	// contains filtered or unexported fields
}

func (*RpcContext) Args

func (c *RpcContext) Args(i interface{})

func (*RpcContext) CheckError

func (c *RpcContext) CheckError(e error)

func (*RpcContext) Do

func (c *RpcContext) Do(handlers Handlers)

The only one important if you will override the Context with an embedded context.Context inside it. Required in order to run the handlers via this "*MyContext".

func (*RpcContext) Next

func (c *RpcContext) Next()

The second one important if you will override the Context with an embedded context.Context inside it. Required in order to run the chain of handlers via this "*MyContext".

func (*RpcContext) Return

func (c *RpcContext) Return(i interface{})

func (*RpcContext) ReturnNil

func (c *RpcContext) ReturnNil()

type ServiceContext

type ServiceContext struct {
	Context
	Conn         net.Conn
	MessageType  int32
	MessageGroup int32
	MessageExtra int32
	MessageId    int32 // unpacked id
	MessageBody  *MessageBodyWrapper
	Codec        codec.Codec
}

func (*ServiceContext) Ack

func (c *ServiceContext) Ack(body interface{})

func (*ServiceContext) Cmd

func (c *ServiceContext) Cmd(body interface{})

Note that: just send to the requester

func (*ServiceContext) Do

func (c *ServiceContext) Do(handlers Handlers)

The only one important if you will override the Context with an embedded context.Context inside it. Required in order to run the handlers via this "*MyContext".

func (*ServiceContext) Next

func (c *ServiceContext) Next()

The second one important if you will override the Context with an embedded context.Context inside it. Required in order to run the chain of handlers via this "*MyContext".

func (*ServiceContext) Ntf

func (c *ServiceContext) Ntf(body interface{})

Note that: just send to the requester

type WebApiContext

type WebApiContext struct {
	Context
	RequestCtx *fasthttp.RequestCtx
	Codec      codec.Codec
}

func (*WebApiContext) Do

func (c *WebApiContext) Do(handlers Handlers)

The only one important if you will override the Context with an embedded context.Context inside it. Required in order to run the handlers via this "*MyContext".

func (*WebApiContext) Next

func (c *WebApiContext) Next()

The second one important if you will override the Context with an embedded context.Context inside it. Required in order to run the chain of handlers via this "*MyContext".

func (*WebApiContext) RequestBody

func (c *WebApiContext) RequestBody(body interface{})

func (*WebApiContext) ResponseFailed

func (c *WebApiContext) ResponseFailed(reason string)

func (*WebApiContext) ResponseOK

func (c *WebApiContext) ResponseOK(body interface{})

Jump to

Keyboard shortcuts

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