mirror of
https://github.com/kataras/iris.git
synced 2026-03-05 16:05:58 +00:00
new apps/switch (beta)
This commit is contained in:
@@ -84,9 +84,30 @@ type Application interface {
|
||||
//
|
||||
// Order may change.
|
||||
FindClosestPaths(subdomain, searchPath string, n int) []string
|
||||
|
||||
// String returns the Application's Name.
|
||||
String() string
|
||||
}
|
||||
|
||||
// Notes(@kataras):
|
||||
// Alternative places...
|
||||
// 1. in apps/store, but it would require an empty `import _ "....apps/store"
|
||||
// from end-developers, to avoid the import cycle and *iris.Application access.
|
||||
// 2. in root package level, that could be the best option, it has access to the *iris.Application
|
||||
// instead of the context.Application interface, but we try to keep the root package
|
||||
// as minimum as possible, however: if in the future, those Application instances
|
||||
// can be registered through network instead of same-process then we must think of that choice.
|
||||
// 3. this is the best possible place, as the root package and all subpackages
|
||||
// have access to this context package without import cycles and they already using it,
|
||||
// the only downside is that we don't have access to the *iris.Application instance
|
||||
// but this context.Application is designed that way that can execute all important methods
|
||||
// as the whole Iris code base is so well written.
|
||||
|
||||
var (
|
||||
// registerApps holds all the created iris Applications by this process.
|
||||
// It's slice instead of map because if IRIS_APP_NAME env var exists,
|
||||
// by-default all applications running on the same machine
|
||||
// will have the same name unless `Application.SetName` is called.
|
||||
registeredApps []Application
|
||||
mu sync.RWMutex
|
||||
)
|
||||
@@ -107,8 +128,8 @@ func RegisterApplication(app Application) {
|
||||
// use `Context.Application()` instead.
|
||||
func LastApplication() Application {
|
||||
mu.RLock()
|
||||
if n := len(registeredApps); n > 0 {
|
||||
if app := registeredApps[n-1]; app != nil {
|
||||
for i := len(registeredApps) - 1; i >= 0; i-- {
|
||||
if app := registeredApps[i]; app != nil {
|
||||
mu.RUnlock()
|
||||
return app
|
||||
}
|
||||
@@ -117,6 +138,34 @@ func LastApplication() Application {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetApplication returns a registered Application
|
||||
// based on its name. If the "appName" is not unique
|
||||
// across Applications, then it will return the newest one.
|
||||
func GetApplication(appName string) (Application, bool) {
|
||||
mu.RLock()
|
||||
|
||||
for i := len(registeredApps) - 1; i >= 0; i-- {
|
||||
if app := registeredApps[i]; app != nil && app.String() == appName {
|
||||
mu.RUnlock()
|
||||
return app, true
|
||||
}
|
||||
}
|
||||
|
||||
mu.RUnlock()
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// MustGetApplication same as `GetApplication` but it
|
||||
// panics if "appName" is not a registered Application's name.
|
||||
func MustGetApplication(appName string) Application {
|
||||
app, ok := GetApplication(appName)
|
||||
if !ok || app == nil {
|
||||
panic(appName + " is not a registered Application")
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
// DefaultLogger returns a Logger instance for an Iris module.
|
||||
// If the program contains at least one registered Iris Application
|
||||
// before this call then it will return a child of that Application's Logger
|
||||
|
||||
@@ -135,6 +135,14 @@ func NewContext(app Application) *Context {
|
||||
return &Context{app: app}
|
||||
}
|
||||
|
||||
/* Not required, unless requested.
|
||||
// SetApplication sets an Iris Application on-fly.
|
||||
// Do NOT use it after ServeHTTPC is fired.
|
||||
func (ctx *Context) SetApplication(app Application) {
|
||||
ctx.app = app
|
||||
}
|
||||
*/
|
||||
|
||||
// Clone returns a copy of the context that
|
||||
// can be safely used outside the request's scope.
|
||||
// Note that if the request-response lifecycle terminated
|
||||
@@ -898,8 +906,22 @@ func (ctx *Context) Domain() string {
|
||||
return GetDomain(ctx.Host())
|
||||
}
|
||||
|
||||
// SubdomainFull returnst he full subdomain level, e.g.
|
||||
// GetSubdomainFull returns the full subdomain level, e.g.
|
||||
// [test.user.]mydomain.com.
|
||||
func GetSubdomainFull(r *http.Request) string {
|
||||
host := GetHost(r) // host:port
|
||||
rootDomain := GetDomain(host) // mydomain.com
|
||||
rootDomainIdx := strings.Index(host, rootDomain)
|
||||
if rootDomainIdx == -1 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return host[0:rootDomainIdx]
|
||||
}
|
||||
|
||||
// SubdomainFull returns the full subdomain level, e.g.
|
||||
// [test.user.]mydomain.com.
|
||||
// Note that HostProxyHeaders are being respected here.
|
||||
func (ctx *Context) SubdomainFull() string {
|
||||
host := ctx.Host() // host:port
|
||||
rootDomain := GetDomain(host) // mydomain.com
|
||||
|
||||
@@ -19,7 +19,7 @@ var (
|
||||
)
|
||||
|
||||
var (
|
||||
handlerNames = make(map[*nameExpr]string)
|
||||
handlerNames = make(map[*NameExpr]string)
|
||||
handlerNamesMu sync.RWMutex
|
||||
)
|
||||
|
||||
@@ -44,19 +44,22 @@ func SetHandlerName(original string, replacement string) {
|
||||
// when a handler name is declared as it's and cause regex parsing expression error,
|
||||
// e.g. `iris/cache/client.(*Handler).ServeHTTP-fm`
|
||||
regex, _ := regexp.Compile(original)
|
||||
handlerNames[&nameExpr{
|
||||
handlerNames[&NameExpr{
|
||||
literal: original,
|
||||
regex: regex,
|
||||
}] = replacement
|
||||
handlerNamesMu.Unlock()
|
||||
}
|
||||
|
||||
type nameExpr struct {
|
||||
// NameExpr regex or literal comparison through `MatchString`.
|
||||
type NameExpr struct {
|
||||
regex *regexp.Regexp
|
||||
literal string
|
||||
}
|
||||
|
||||
func (expr *nameExpr) MatchString(s string) bool {
|
||||
// MatchString reports whether "s" is literal of "literal"
|
||||
// or it matches the regex expression at "regex".
|
||||
func (expr *NameExpr) MatchString(s string) bool {
|
||||
if expr.literal == s { // if matches as string, as it's.
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user