mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
.DI() to ConfigureContainer(...builders)
Former-commit-id: 169671a8b5b706dc8f136e68c1a060f27a2c421b
This commit is contained in:
@@ -113,7 +113,7 @@ func (repo *repository) register(route *Route, rule RouteRegisterRule) (*Route,
|
||||
// and child routers.
|
||||
type APIBuilder struct {
|
||||
// the per-party APIBuilder with DI.
|
||||
apiBuilderDI *APIBuilderDI
|
||||
apiBuilderDI *APIContainer
|
||||
|
||||
// the api builder global macros registry
|
||||
macros *macro.Macros
|
||||
@@ -171,7 +171,7 @@ func NewAPIBuilder() *APIBuilder {
|
||||
routes: new(repository),
|
||||
}
|
||||
|
||||
api.apiBuilderDI = &APIBuilderDI{
|
||||
api.apiBuilderDI = &APIContainer{
|
||||
Self: api,
|
||||
Container: hero.New(),
|
||||
}
|
||||
@@ -179,8 +179,22 @@ func NewAPIBuilder() *APIBuilder {
|
||||
return api
|
||||
}
|
||||
|
||||
// DI returns the APIBuilder featured with Dependency Injection.
|
||||
func (api *APIBuilder) DI() *APIBuilderDI {
|
||||
// ConfigureContainer accepts one or more functions that can be used
|
||||
// to configure dependency injection features of this Party
|
||||
// such as register dependency and register handlers that will automatically inject any valid dependency.
|
||||
// However, if the "builder" parameter is nil or not provided then it just returns the *APIContainer,
|
||||
// which automatically initialized on Party allocation.
|
||||
//
|
||||
// It returns the same `APIBuilder` featured with Dependency Injection.
|
||||
func (api *APIBuilder) ConfigureContainer(builder ...func(*APIContainer)) *APIContainer {
|
||||
for _, b := range builder {
|
||||
if b == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
b(api.apiBuilderDI)
|
||||
}
|
||||
|
||||
return api.apiBuilderDI
|
||||
}
|
||||
|
||||
@@ -529,7 +543,7 @@ func (api *APIBuilder) Party(relativePath string, handlers ...context.Handler) P
|
||||
// based on the fullpath.
|
||||
childContainer := api.apiBuilderDI.Container.Clone()
|
||||
|
||||
childAPI.apiBuilderDI = &APIBuilderDI{
|
||||
childAPI.apiBuilderDI = &APIContainer{
|
||||
Self: childAPI,
|
||||
Container: childContainer,
|
||||
}
|
||||
|
||||
@@ -8,8 +8,9 @@ import (
|
||||
"github.com/kataras/iris/v12/macro"
|
||||
)
|
||||
|
||||
// APIBuilderDI is a wrapper of a common `Party` features Dependency Injection.
|
||||
type APIBuilderDI struct {
|
||||
// APIContainer is a wrapper of a common `Party` featured by Dependency Injection.
|
||||
// See `Party.ConfigureContainer` for more.
|
||||
type APIContainer struct {
|
||||
// Self returns the original `Party` without DI features.
|
||||
Self Party
|
||||
|
||||
@@ -17,12 +18,12 @@ type APIBuilderDI struct {
|
||||
Container *hero.Container
|
||||
}
|
||||
|
||||
// Party returns a child of this `APIBuilderDI` featured with Dependency Injection.
|
||||
// Party returns a child of this `APIContainer` featured with Dependency Injection.
|
||||
// Like the `Self.Party` method does for the common Router Groups.
|
||||
func (api *APIBuilderDI) Party(relativePath string, handlersFn ...interface{}) *APIBuilderDI {
|
||||
func (api *APIContainer) Party(relativePath string, handlersFn ...interface{}) *APIContainer {
|
||||
handlers := api.convertHandlerFuncs(relativePath, handlersFn...)
|
||||
p := api.Self.Party(relativePath, handlers...)
|
||||
return p.DI()
|
||||
return p.ConfigureContainer()
|
||||
}
|
||||
|
||||
// OnError adds an error handler for this Party's DI Hero Container and its handlers (or controllers).
|
||||
@@ -33,7 +34,7 @@ func (api *APIBuilderDI) Party(relativePath string, handlersFn ...interface{}) *
|
||||
// Container.GetErrorHandler = func(ctx iris.Context) hero.ErrorHandler { return errorHandler }
|
||||
//
|
||||
// See `RegisterDependency`, `Use`, `Done` and `Handle` too.
|
||||
func (api *APIBuilderDI) OnError(errorHandler func(context.Context, error)) {
|
||||
func (api *APIContainer) OnError(errorHandler func(context.Context, error)) {
|
||||
errHandler := hero.ErrorHandlerFunc(errorHandler)
|
||||
api.Container.GetErrorHandler = func(ctx context.Context) hero.ErrorHandler {
|
||||
return errHandler
|
||||
@@ -59,12 +60,12 @@ func (api *APIBuilderDI) OnError(errorHandler func(context.Context, error)) {
|
||||
// - RegisterDependency(func(User) OtherResponse {...})
|
||||
//
|
||||
// See `OnError`, `Use`, `Done` and `Handle` too.
|
||||
func (api *APIBuilderDI) RegisterDependency(dependency interface{}) *hero.Dependency {
|
||||
func (api *APIContainer) RegisterDependency(dependency interface{}) *hero.Dependency {
|
||||
return api.Container.Register(dependency)
|
||||
}
|
||||
|
||||
// convertHandlerFuncs accepts Iris hero handlers and returns a slice of native Iris handlers.
|
||||
func (api *APIBuilderDI) convertHandlerFuncs(relativePath string, handlersFn ...interface{}) context.Handlers {
|
||||
func (api *APIContainer) convertHandlerFuncs(relativePath string, handlersFn ...interface{}) context.Handlers {
|
||||
fullpath := api.Self.GetRelPath() + relativePath
|
||||
paramsCount := macro.CountParams(fullpath, *api.Self.Macros())
|
||||
|
||||
@@ -84,14 +85,14 @@ func (api *APIBuilderDI) convertHandlerFuncs(relativePath string, handlersFn ...
|
||||
// Use same as `Self.Use` but it accepts dynamic functions as its "handlersFn" input.
|
||||
//
|
||||
// See `OnError`, `RegisterDependency`, `Done` and `Handle` for more.
|
||||
func (api *APIBuilderDI) Use(handlersFn ...interface{}) {
|
||||
func (api *APIContainer) Use(handlersFn ...interface{}) {
|
||||
handlers := api.convertHandlerFuncs("/", handlersFn...)
|
||||
api.Self.Use(handlers...)
|
||||
}
|
||||
|
||||
// Done same as `Self.Done` but it accepts dynamic functions as its "handlersFn" input.
|
||||
// See `OnError`, `RegisterDependency`, `Use` and `Handle` for more.
|
||||
func (api *APIBuilderDI) Done(handlersFn ...interface{}) {
|
||||
func (api *APIContainer) Done(handlersFn ...interface{}) {
|
||||
handlers := api.convertHandlerFuncs("/", handlersFn...)
|
||||
api.Self.Done(handlers...)
|
||||
}
|
||||
@@ -107,7 +108,7 @@ func (api *APIBuilderDI) Done(handlersFn ...interface{}) {
|
||||
// the end-developer should output an error and return `iris.ErrStopExecution`.
|
||||
//
|
||||
// See `OnError`, `RegisterDependency`, `Use`, `Done`, `Get`, `Post`, `Put`, `Patch` and `Delete` too.
|
||||
func (api *APIBuilderDI) Handle(method, relativePath string, handlersFn ...interface{}) *Route {
|
||||
func (api *APIContainer) Handle(method, relativePath string, handlersFn ...interface{}) *Route {
|
||||
handlers := api.convertHandlerFuncs(relativePath, handlersFn...)
|
||||
return api.Self.Handle(method, relativePath, handlers...)
|
||||
}
|
||||
@@ -115,63 +116,63 @@ func (api *APIBuilderDI) Handle(method, relativePath string, handlersFn ...inter
|
||||
// Get registers a route for the Get HTTP Method.
|
||||
//
|
||||
// Returns a *Route and an error which will be filled if route wasn't registered successfully.
|
||||
func (api *APIBuilderDI) Get(relativePath string, handlersFn ...interface{}) *Route {
|
||||
func (api *APIContainer) Get(relativePath string, handlersFn ...interface{}) *Route {
|
||||
return api.Handle(http.MethodGet, relativePath, handlersFn...)
|
||||
}
|
||||
|
||||
// Post registers a route for the Post HTTP Method.
|
||||
//
|
||||
// Returns a *Route and an error which will be filled if route wasn't registered successfully.
|
||||
func (api *APIBuilderDI) Post(relativePath string, handlersFn ...interface{}) *Route {
|
||||
func (api *APIContainer) Post(relativePath string, handlersFn ...interface{}) *Route {
|
||||
return api.Handle(http.MethodPost, relativePath, handlersFn...)
|
||||
}
|
||||
|
||||
// Put registers a route for the Put HTTP Method.
|
||||
//
|
||||
// Returns a *Route and an error which will be filled if route wasn't registered successfully.
|
||||
func (api *APIBuilderDI) Put(relativePath string, handlersFn ...interface{}) *Route {
|
||||
func (api *APIContainer) Put(relativePath string, handlersFn ...interface{}) *Route {
|
||||
return api.Handle(http.MethodPut, relativePath, handlersFn...)
|
||||
}
|
||||
|
||||
// Delete registers a route for the Delete HTTP Method.
|
||||
//
|
||||
// Returns a *Route and an error which will be filled if route wasn't registered successfully.
|
||||
func (api *APIBuilderDI) Delete(relativePath string, handlersFn ...interface{}) *Route {
|
||||
func (api *APIContainer) Delete(relativePath string, handlersFn ...interface{}) *Route {
|
||||
return api.Handle(http.MethodDelete, relativePath, handlersFn...)
|
||||
}
|
||||
|
||||
// Connect registers a route for the Connect HTTP Method.
|
||||
//
|
||||
// Returns a *Route and an error which will be filled if route wasn't registered successfully.
|
||||
func (api *APIBuilderDI) Connect(relativePath string, handlersFn ...interface{}) *Route {
|
||||
func (api *APIContainer) Connect(relativePath string, handlersFn ...interface{}) *Route {
|
||||
return api.Handle(http.MethodConnect, relativePath, handlersFn...)
|
||||
}
|
||||
|
||||
// Head registers a route for the Head HTTP Method.
|
||||
//
|
||||
// Returns a *Route and an error which will be filled if route wasn't registered successfully.
|
||||
func (api *APIBuilderDI) Head(relativePath string, handlersFn ...interface{}) *Route {
|
||||
func (api *APIContainer) Head(relativePath string, handlersFn ...interface{}) *Route {
|
||||
return api.Handle(http.MethodHead, relativePath, handlersFn...)
|
||||
}
|
||||
|
||||
// Options registers a route for the Options HTTP Method.
|
||||
//
|
||||
// Returns a *Route and an error which will be filled if route wasn't registered successfully.
|
||||
func (api *APIBuilderDI) Options(relativePath string, handlersFn ...interface{}) *Route {
|
||||
func (api *APIContainer) Options(relativePath string, handlersFn ...interface{}) *Route {
|
||||
return api.Handle(http.MethodOptions, relativePath, handlersFn...)
|
||||
}
|
||||
|
||||
// Patch registers a route for the Patch HTTP Method.
|
||||
//
|
||||
// Returns a *Route and an error which will be filled if route wasn't registered successfully.
|
||||
func (api *APIBuilderDI) Patch(relativePath string, handlersFn ...interface{}) *Route {
|
||||
func (api *APIContainer) Patch(relativePath string, handlersFn ...interface{}) *Route {
|
||||
return api.Handle(http.MethodPatch, relativePath, handlersFn...)
|
||||
}
|
||||
|
||||
// Trace registers a route for the Trace HTTP Method.
|
||||
//
|
||||
// Returns a *Route and an error which will be filled if route wasn't registered successfully.
|
||||
func (api *APIBuilderDI) Trace(relativePath string, handlersFn ...interface{}) *Route {
|
||||
func (api *APIContainer) Trace(relativePath string, handlersFn ...interface{}) *Route {
|
||||
return api.Handle(http.MethodTrace, relativePath, handlersFn...)
|
||||
}
|
||||
|
||||
@@ -185,7 +186,7 @@ func (api *APIBuilderDI) Trace(relativePath string, handlersFn ...interface{}) *
|
||||
// Options
|
||||
// Connect
|
||||
// Trace
|
||||
func (api *APIBuilderDI) Any(relativePath string, handlersFn ...interface{}) (routes []*Route) {
|
||||
func (api *APIContainer) Any(relativePath string, handlersFn ...interface{}) (routes []*Route) {
|
||||
handlers := api.convertHandlerFuncs(relativePath, handlersFn...)
|
||||
|
||||
for _, m := range AllMethods {
|
||||
@@ -9,10 +9,16 @@ import (
|
||||
// Party is just a group joiner of routes which have the same prefix and share same middleware(s) also.
|
||||
// Party could also be named as 'Join' or 'Node' or 'Group' , Party chosen because it is fun.
|
||||
//
|
||||
// Look the "APIBuilder" for its implementation.
|
||||
// Look the `APIBuilder` structure for its implementation.
|
||||
type Party interface {
|
||||
// DI returns the APIBuilder featured with Dependency Injection.
|
||||
DI() *APIBuilderDI
|
||||
// ConfigureContainer accepts one or more functions that can be used
|
||||
// to configure dependency injection features of this Party
|
||||
// such as register dependency and register handlers that will automatically inject any valid dependency.
|
||||
// However, if the "builder" parameter is nil or not provided then it just returns the *APIContainer,
|
||||
// which automatically initialized on Party allocation.
|
||||
//
|
||||
// It returns the same `APIBuilder` featured with Dependency Injection.
|
||||
ConfigureContainer(builder ...func(*APIContainer)) *APIContainer
|
||||
|
||||
// GetRelPath returns the current party's relative path.
|
||||
// i.e:
|
||||
|
||||
Reference in New Issue
Block a user