1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 10:27:06 +00:00

add two new examples and share the app's specific logger instance with sessions databases and APIBuilder

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-16 07:07:36 +03:00
parent ef7d365e81
commit 889b7942d3
23 changed files with 346 additions and 89 deletions

View File

@@ -4,6 +4,7 @@ import (
stdContext "context"
"io"
"net/http"
"sync"
"github.com/kataras/golog"
"github.com/tdewolff/minify/v2"
@@ -84,3 +85,51 @@ type Application interface {
// Order may change.
FindClosestPaths(subdomain, searchPath string, n int) []string
}
var (
registeredApps []Application
mu sync.RWMutex
)
// RegisterApplication registers an application to the global shared storage.
func RegisterApplication(app Application) {
if app == nil {
return
}
mu.Lock()
registeredApps = append(registeredApps, app)
mu.Unlock()
}
// LastApplication returns the last registered Application.
// Handlers has access to the current Application,
// use `Context.Application()` instead.
func LastApplication() Application {
mu.RLock()
if n := len(registeredApps); n > 0 {
if app := registeredApps[n-1]; app != nil {
mu.RUnlock()
return app
}
}
mu.RUnlock()
return nil
}
// 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
// otherwise a fresh child of the `golog.Default` will be returned instead.
//
// It should be used when a module has no access to the Application or its Logger.
func DefaultLogger(prefix string) (logger *golog.Logger) {
if app := LastApplication(); app != nil {
logger = app.Logger()
} else {
logger = golog.Default
}
logger = logger.Child(prefix)
return
}