From d5a179cc4598917608b2336fae4ae8afacdd2761 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Fri, 18 Sep 2020 02:08:27 +0300 Subject: [PATCH] fix UseRouter not respected by iris-cli live reload --- cli.go | 18 +++++------------- hero/handler.go | 13 +++++++++++++ iris.go | 2 +- middleware/accesslog/accesslog.go | 22 ++++++++++++---------- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/cli.go b/cli.go index db4eb62a..a47c9fd8 100644 --- a/cli.go +++ b/cli.go @@ -8,13 +8,11 @@ package iris import ( "bytes" "fmt" - "net/http" "os" "path/filepath" "strings" "github.com/kataras/iris/v12/context" - "github.com/kataras/iris/v12/core/router" "gopkg.in/yaml.v3" ) @@ -29,7 +27,7 @@ import ( // at runtime it will fire 404 instead of redirecting to the correct port (that's a TODO). // // tryInjectLiveReload runs right before Build -> BuildRouter. -func injectLiveReload(contextPool *context.Pool, router *router.Router) (bool, error) { +func injectLiveReload(r Party) (bool, error) { conf := struct { Running bool `yaml:"Running,omitempty"` LiveReload struct { @@ -88,11 +86,9 @@ func injectLiveReload(contextPool *context.Pool, router *router.Router) (bool, e bodyCloseTag := []byte("") - wrapper := func(w http.ResponseWriter, r *http.Request, _ http.HandlerFunc) { - ctx := contextPool.Acquire(w, r) - rec := ctx.Recorder() // Record everything and write all in once at the Context release. - router.ServeHTTPC(ctx) // We directly call request handler with Context. - + r.UseRouter(func(ctx Context) { + rec := ctx.Recorder() // Record everything and write all in once at the Context release. + ctx.Next() // call the next, so this is a 'done' handler. if strings.HasPrefix(ctx.GetContentType(), "text/html") { // delete(rec.Header(), context.ContentLengthHeaderKey) @@ -111,10 +107,6 @@ func injectLiveReload(contextPool *context.Pool, router *router.Router) (bool, e rec.Header().Set(context.ContentLengthHeaderKey, fmt.Sprintf("%d", len(rec.Body()))) } } - - contextPool.Release(ctx) - } - - router.AddRouterWrapper(wrapper) + }) return true, nil } diff --git a/hero/handler.go b/hero/handler.go index 9180ef03..92444fb6 100644 --- a/hero/handler.go +++ b/hero/handler.go @@ -41,6 +41,19 @@ func (fn ErrorHandlerFunc) HandleError(ctx *context.Context, err error) { fn(ctx, err) } +// String implements the fmt.Stringer interface. +// Returns the text corresponding to this status code, e.g. "Not Found". +// Same as iris.StatusText(int(code)). +func (code Code) String() string { + return context.StatusText(int(code)) +} + +// Value returns the underline int value. +// Same as int(code). +func (code Code) Value() int { + return int(code) +} + var ( // ErrSeeOther may be returned from a dependency handler to skip a specific dependency // based on custom logic. diff --git a/iris.go b/iris.go index 5c8a0419..74a8b15a 100644 --- a/iris.go +++ b/iris.go @@ -623,7 +623,7 @@ func (app *Application) Build() error { if !app.Router.Downgraded() { // router - if _, err := injectLiveReload(app.ContextPool, app.Router); err != nil { + if _, err := injectLiveReload(app); err != nil { app.logger.Errorf("LiveReload: init: failed: %v", err) return err } diff --git a/middleware/accesslog/accesslog.go b/middleware/accesslog/accesslog.go index 4ddde808..4bd5f15e 100644 --- a/middleware/accesslog/accesslog.go +++ b/middleware/accesslog/accesslog.go @@ -308,6 +308,7 @@ func New(w io.Writer) *AccessLog { host.RegisterOnInterrupt(func() { ac.Close() }) + return ac } @@ -474,18 +475,19 @@ func (ac *AccessLog) setOutput(reset bool, writers ...io.Writer) { } } - // And finally, wait before exit this method - // until previous writer's closers and flush finish. - for _, flusher := range flushers { - if flusher != nil { - flusher.Flush() + if reset { + // And finally, wait before exit this method + // until previous writer's closers and flush finish. + for _, flusher := range flushers { + if flusher != nil { + flusher.Flush() + } } - } - for _, closer := range closers { - if closer != nil { - closer.Close() + for _, closer := range closers { + if closer != nil { + closer.Close() + } } - } }