1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-10 05:25:58 +00:00

add iris.Minify middleware and Context.OnCloseErr/OnConnectionCloseErr

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-15 17:21:57 +03:00
parent ab226d925a
commit ef7d365e81
10 changed files with 178 additions and 4 deletions

View File

@@ -6,6 +6,7 @@ import (
"net/http"
"github.com/kataras/golog"
"github.com/tdewolff/minify/v2"
)
// Application is the context's owner.
@@ -25,6 +26,14 @@ type Application interface {
// the failure reason if not.
Validate(interface{}) error
// Minifier returns the minifier instance.
// By default it can minifies:
// - text/html
// - text/css
// - image/svg+xml
// - application/text(javascript, ecmascript, json, xml).
// Use that instance to add custom Minifiers before server ran.
Minifier() *minify.M
// View executes and write the result of a template file to the writer.
//
// Use context.View to render templates to the client instead.

View File

@@ -259,6 +259,35 @@ func (ctx *Context) OnConnectionClose(cb Handler) bool {
return true
}
// OnConnectionCloseErr same as `OnConnectionClose` but instead it
// receives a function which returns an error.
// If error is not nil, it will be logged as a debug message.
func (ctx *Context) OnConnectionCloseErr(cb func() error) bool {
if cb == nil {
return false
}
reqCtx := ctx.Request().Context()
if reqCtx == nil {
return false
}
notifyClose := reqCtx.Done()
if notifyClose == nil {
return false
}
go func() {
<-notifyClose
if err := cb(); err != nil {
// Can be ignored.
ctx.app.Logger().Debugf("OnConnectionCloseErr: received error: %v", err)
}
}()
return true
}
// OnClose registers a callback which
// will be fired when the underlying connection has gone away(request canceled)
// on its own goroutine or in the end of the request-response lifecylce
@@ -297,6 +326,36 @@ func (ctx *Context) OnClose(cb Handler) {
ctx.writer.SetBeforeFlush(onFlush)
}
// OnCloseErr same as `OnClose` but instead it
// receives a function which returns an error.
// If error is not nil, it will be logged as a debug message.
func (ctx *Context) OnCloseErr(cb func() error) {
if cb == nil {
return
}
var executed uint32
callback := func() error {
if atomic.CompareAndSwapUint32(&executed, 0, 1) {
return cb()
}
return nil
}
ctx.OnConnectionCloseErr(callback)
onFlush := func() {
if err := callback(); err != nil {
// Can be ignored.
ctx.app.Logger().Debugf("OnClose: SetBeforeFlush: received error: %v", err)
}
}
ctx.writer.SetBeforeFlush(onFlush)
}
/* Note(@kataras): just leave end-developer decide.
const goroutinesContextKey = "iris.goroutines"
@@ -795,7 +854,7 @@ func GetHost(r *http.Request) string {
// Subdomain returns the subdomain of this request, if any.
// Note that this is a fast method which does not cover all cases.
func (ctx *Context) Subdomain() (subdomain string) {
host := ctx.request.URL.Host // ctx.Host()
host := ctx.Host()
if index := strings.IndexByte(host, '.'); index > 0 {
subdomain = host[0:index]
}