From 29010bfd7c8630114d92391eba5d2d1dd8557a2a Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Tue, 27 Sep 2022 20:43:01 +0300 Subject: [PATCH] minor: compatibility: context render --- HISTORY.md | 2 ++ _proposals/generic_handler.md | 2 +- context/context.go | 20 ++++++++++++++++++++ context/{context_fs.go => fs.go} | 0 4 files changed, 23 insertions(+), 1 deletion(-) rename context/{context_fs.go => fs.go} (100%) diff --git a/HISTORY.md b/HISTORY.md index 76c66fe6..daa813f7 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -28,6 +28,8 @@ The codebase for Dependency Injection, Internationalization and localization and ## Fixes and Improvements +- Add `Context.Render` method for compatibility. + - Support of embedded [locale files](https://github.com/kataras/iris/blob/master/_examples/i18n/template-embedded/main.go) using standard `embed.FS` with the new `LoadFS` function. - Support of direct embedded view engines (`HTML, Blocks, Django, Handlebars, Pug, Amber, Jet` and `Ace`) with `embed.FS` or `fs.FS` (in addition to `string` and `http.FileSystem` types). - Add support for `embed.FS` and `fs.FS` on `app.HandleDir`. diff --git a/_proposals/generic_handler.md b/_proposals/generic_handler.md index 950b3643..41e3fb42 100644 --- a/_proposals/generic_handler.md +++ b/_proposals/generic_handler.md @@ -62,7 +62,7 @@ type ( func main() { app := iris.New() - app.Post("/", your_package.Handle(handler)) + app.Post("/", x.Handle(handler)) app.Listen(":8080") } diff --git a/context/context.go b/context/context.go index 85196cf6..55d75824 100644 --- a/context/context.go +++ b/context/context.go @@ -4204,6 +4204,26 @@ func (ctx *Context) handleContextError(err error) { // keep the error non nil so the caller has control over further actions. } +// Render writes the response headers and calls the given renderer "r" to render data. +// This method can be used while migrating from other frameworks. +func (ctx *Context) Render(statusCode int, r interface { + // Render should push data with custom content type to the client. + Render(http.ResponseWriter) error + // WriteContentType writes custom content type to the response. + WriteContentType(w http.ResponseWriter) +}) { + ctx.StatusCode(statusCode) + + if statusCode >= 100 && statusCode <= 199 || statusCode == http.StatusNoContent || statusCode == http.StatusNotModified { + r.WriteContentType(ctx.writer) + return + } + + if err := r.Render(ctx.writer); err != nil { + ctx.StopWithError(http.StatusInternalServerError, err) + } +} + // JSON marshals the given "v" value to JSON and writes the response to the client. // Look the Configuration.EnableProtoJSON and EnableEasyJSON too. // diff --git a/context/context_fs.go b/context/fs.go similarity index 100% rename from context/context_fs.go rename to context/fs.go