fix UseRouter not respected by iris-cli live reload

This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-09-18 02:08:27 +03:00
parent 7fa2666f58
commit d5a179cc45
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
4 changed files with 31 additions and 24 deletions

18
cli.go
View File

@ -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("</body>")
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
}

View File

@ -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.

View File

@ -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
}

View File

@ -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()
}
}
}
}