mirror of
https://github.com/kataras/iris.git
synced 2025-12-23 12:57:05 +00:00
add two new examples and share the app's specific logger instance with sessions databases and APIBuilder
This commit is contained in:
@@ -15,6 +15,8 @@ import (
|
||||
"github.com/kataras/iris/v12/hero"
|
||||
"github.com/kataras/iris/v12/macro"
|
||||
macroHandler "github.com/kataras/iris/v12/macro/handler"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
)
|
||||
|
||||
// MethodNone is a Virtual method
|
||||
@@ -153,6 +155,8 @@ func overlapRoute(r *Route, next *Route) {
|
||||
// APIBuilder the visible API for constructing the router
|
||||
// and child routers.
|
||||
type APIBuilder struct {
|
||||
// the application logger.
|
||||
logger *golog.Logger
|
||||
// parent is the creator of this Party.
|
||||
// It is nil on Root.
|
||||
parent *APIBuilder // currently it's used only on UseRouter feature.
|
||||
@@ -227,8 +231,9 @@ var (
|
||||
|
||||
// NewAPIBuilder creates & returns a new builder
|
||||
// which is responsible to build the API and the router handler.
|
||||
func NewAPIBuilder() *APIBuilder {
|
||||
func NewAPIBuilder(logger *golog.Logger) *APIBuilder {
|
||||
return &APIBuilder{
|
||||
logger: logger,
|
||||
parent: nil,
|
||||
macros: macro.Defaults,
|
||||
errors: errgroup.New("API Builder"),
|
||||
@@ -240,6 +245,11 @@ func NewAPIBuilder() *APIBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
// Logger returns the Application Logger.
|
||||
func (api *APIBuilder) Logger() *golog.Logger {
|
||||
return api.logger
|
||||
}
|
||||
|
||||
// IsRoot reports whether this Party is the root Application's one.
|
||||
// It will return false on all children Parties, no exception.
|
||||
func (api *APIBuilder) IsRoot() bool {
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
)
|
||||
|
||||
//
|
||||
@@ -83,7 +85,7 @@ func BenchmarkAPIBuilder(b *testing.B) {
|
||||
// i.e /gzhyweumidvelqewrvoyqmzopvuxli/{name:string}/bibrkratnrrhvsjwsxygfwmqwhcstc/{age:int}/end
|
||||
paths := genPaths(routesLength, 15, 42)
|
||||
|
||||
api := NewAPIBuilder()
|
||||
api := NewAPIBuilder(golog.Default)
|
||||
requestHandler := NewDefaultHandler(nil, nil)
|
||||
|
||||
b.ReportAllocs()
|
||||
|
||||
@@ -246,7 +246,7 @@ func (h *routerHandler) Build(provider RoutesProvider) error {
|
||||
// logger.Debugf("API: %d registered %s (", len(registeredRoutes), tr)
|
||||
// with:
|
||||
pio.WriteRich(logger.Printer, debugLevel.Title, debugLevel.ColorCode, debugLevel.Style...)
|
||||
fmt.Fprintf(logger.Printer, " %s API: %d registered %s (", time.Now().Format(logger.TimeFormat), len(registeredRoutes), tr)
|
||||
fmt.Fprintf(logger.Printer, " %s %sAPI: %d registered %s (", time.Now().Format(logger.TimeFormat), logger.Prefix, len(registeredRoutes), tr)
|
||||
//
|
||||
logger.NewLine = bckpNewLine
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/v12/core/errgroup"
|
||||
"github.com/kataras/iris/v12/macro"
|
||||
|
||||
"github.com/kataras/golog"
|
||||
)
|
||||
|
||||
// Party is just a group joiner of routes which have the same prefix and share same middleware(s) also.
|
||||
@@ -13,6 +15,9 @@ import (
|
||||
//
|
||||
// Look the `APIBuilder` structure for its implementation.
|
||||
type Party interface {
|
||||
// Logger returns the Application Logger.
|
||||
Logger() *golog.Logger
|
||||
|
||||
// IsRoot reports whether this Party is the root Application's one.
|
||||
// It will return false on all children Parties, no exception.
|
||||
IsRoot() bool
|
||||
|
||||
Reference in New Issue
Block a user