diff --git a/README.md b/README.md index f013a8b3..5a3a2a09 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Learn what [others saying about Iris](https://iris-go.com/testimonials/) and **[ ## 👑 Supporters

+ Damon Blais 陆 轶丰 Weihang Ding Li Fang diff --git a/apps/apps.go b/apps/apps.go index dd492c5f..5202eb7a 100644 --- a/apps/apps.go +++ b/apps/apps.go @@ -2,3 +2,53 @@ // This package directly imports the iris root package and cannot be used // inside Iris' codebase itself. Only external packages/programs can make use of it. package apps + +import ( + "github.com/kataras/iris/v12" + "github.com/kataras/iris/v12/context" +) + +// Get returns an existing Iris Application based on its "appName". +// Applications of the same program +// are registered automatically. +func Get(appName string) *iris.Application { + if app, ok := context.GetApplication(appName); ok { + return app.(*iris.Application) + } + + return nil +} + +// GetAll returns a slice of all registered Iris Applications. +func GetAll() []*iris.Application { + appsReadOnly := context.GetApplications() + apps := make([]*iris.Application, 0, len(appsReadOnly)) + + for _, app := range appsReadOnly { + apps = append(apps, app.(*iris.Application)) + } + + return apps +} + +// Configure applies one or more configurator to the +// applications with the given "appNames". +// +// See `ConfigureAll` too. +func Configure(appNames []string, configurators ...iris.Configurator) { + for _, appName := range appNames { + if app := Get(appName); app != nil { + app.Configure(configurators...) + } + } +} + +// ConfigureAll applies one or more configurator to all +// registered applications so far. +// +// See `Configure` too. +func ConfigureAll(configurators ...iris.Configurator) { + for _, app := range context.GetApplications() { + app.(*iris.Application).Configure(configurators...) + } +} diff --git a/context/application.go b/context/application.go index e2f52874..b1d5b4db 100644 --- a/context/application.go +++ b/context/application.go @@ -123,6 +123,18 @@ func RegisterApplication(app Application) { mu.Unlock() } +// GetApplications returns a slice of all the registered Applications. +func GetApplications() []Application { + mu.RLock() + // a copy slice but the instances are pointers so be careful what modifications are done + // the return value is read-only but it can be casted to *iris.Application. + apps := make([]Application, 0, len(registeredApps)) + copy(apps, registeredApps) + mu.RLock() + + return apps +} + // LastApplication returns the last registered Application. // Handlers has access to the current Application, // use `Context.Application()` instead.