add an app.View example for parsing and writing templates outside of the HTTP scope(context.View)

Former-commit-id: e65d8ece521c778dedf45cf2f522383c26b9901b
This commit is contained in:
Gerasimos (Makis) Maropoulos 2018-01-22 15:42:19 +02:00
parent 68acbf3f31
commit 4b4dbb04af
7 changed files with 79 additions and 8 deletions

View File

@ -17,7 +17,7 @@ Developers are not forced to upgrade if they don't really need it. Upgrade whene
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris` or let the automatic updater do that for you. **How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris` or let the automatic updater do that for you.
# Tu, 16 Jenuary 2018 | v10.0.2 # Tu, 16 January 2018 | v10.0.2
## Security | `iris.AutoTLS` ## Security | `iris.AutoTLS`
@ -28,7 +28,7 @@ Read more at:
- https://letsencrypt.status.io/pages/incident/55957a99e800baa4470002da/5a55777ed9a9c1024c00b241 - https://letsencrypt.status.io/pages/incident/55957a99e800baa4470002da/5a55777ed9a9c1024c00b241
- https://github.com/golang/crypto/commit/13931e22f9e72ea58bb73048bc752b48c6d4d4ac - https://github.com/golang/crypto/commit/13931e22f9e72ea58bb73048bc752b48c6d4d4ac
# Mo, 15 Jenuary 2018 | v10.0.1 # Mo, 15 January 2018 | v10.0.1
Not any serious problems were found to be resolved here but one, the first one which is important for devs that used the [cache](cache) package. Not any serious problems were found to be resolved here but one, the first one which is important for devs that used the [cache](cache) package.
@ -57,7 +57,7 @@ Not any serious problems were found to be resolved here but one, the first one w
1. [A Todo MVC Application using Iris and Vue.js](https://hackernoon.com/a-todo-mvc-application-using-iris-and-vue-js-5019ff870064) 1. [A Todo MVC Application using Iris and Vue.js](https://hackernoon.com/a-todo-mvc-application-using-iris-and-vue-js-5019ff870064)
2. [A Hasura starter project with a ready to deploy Golang hello-world web app with IRIS](bit.ly/2lmKaAZ) 2. [A Hasura starter project with a ready to deploy Golang hello-world web app with IRIS](bit.ly/2lmKaAZ)
# Mo, 01 Jenuary 2018 | v10.0.0 # Mo, 01 January 2018 | v10.0.0
We must thanks [Mrs. Diana](https://www.instagram.com/merry.dii/) for our awesome new [logo](https://iris-go.com/images/icon.svg)! We must thanks [Mrs. Diana](https://www.instagram.com/merry.dii/) for our awesome new [logo](https://iris-go.com/images/icon.svg)!

View File

@ -17,7 +17,7 @@
**Πώς να αναβαθμίσετε**: Ανοίξτε την γραμμή εντολών σας και εκτελέστε αυτήν την εντολή: `go get -u github.com/kataras/iris` ή αφήστε το αυτόματο updater να το κάνει αυτό για σας. **Πώς να αναβαθμίσετε**: Ανοίξτε την γραμμή εντολών σας και εκτελέστε αυτήν την εντολή: `go get -u github.com/kataras/iris` ή αφήστε το αυτόματο updater να το κάνει αυτό για σας.
# Tu, 16 Jenuary 2018 | v10.0.2 # Tu, 16 January 2018 | v10.0.2
## Ασφάλεια | `iris.AutoTLS` ## Ασφάλεια | `iris.AutoTLS`
@ -28,7 +28,7 @@
- https://letsencrypt.status.io/pages/incident/55957a99e800baa4470002da/5a55777ed9a9c1024c00b241 - https://letsencrypt.status.io/pages/incident/55957a99e800baa4470002da/5a55777ed9a9c1024c00b241
- https://github.com/golang/crypto/commit/13931e22f9e72ea58bb73048bc752b48c6d4d4ac - https://github.com/golang/crypto/commit/13931e22f9e72ea58bb73048bc752b48c6d4d4ac
# Mo, 15 Jenuary 2018 | v10.0.1 # Mo, 15 January 2018 | v10.0.1
- διόρθωση του cache handler που δεν δούλευε όπως έπρεπε όταν γινόταν εγγραφή σε πάνω από ένα handler, παλιότερα ήταν ένα cache handler προς ένα route handler, τώρα το ίδιο handler μπορεί να καταχωρηθεί σε όσα route handlers θέλετε https://github.com/kataras/iris/pull/852, όπως είχε αναφερθεί στο https://github.com/kataras/iris/issues/850 - διόρθωση του cache handler που δεν δούλευε όπως έπρεπε όταν γινόταν εγγραφή σε πάνω από ένα handler, παλιότερα ήταν ένα cache handler προς ένα route handler, τώρα το ίδιο handler μπορεί να καταχωρηθεί σε όσα route handlers θέλετε https://github.com/kataras/iris/pull/852, όπως είχε αναφερθεί στο https://github.com/kataras/iris/issues/850
- συγχώνευση PR https://github.com/kataras/iris/pull/862 - συγχώνευση PR https://github.com/kataras/iris/pull/862
@ -54,7 +54,7 @@
1. [A Todo MVC Application using Iris and Vue.js](https://hackernoon.com/a-todo-mvc-application-using-iris-and-vue-js-5019ff870064) 1. [A Todo MVC Application using Iris and Vue.js](https://hackernoon.com/a-todo-mvc-application-using-iris-and-vue-js-5019ff870064)
2. [A Hasura starter project with a ready to deploy Golang hello-world web app with IRIS](bit.ly/2lmKaAZ) 2. [A Hasura starter project with a ready to deploy Golang hello-world web app with IRIS](bit.ly/2lmKaAZ)
# Mo, 01 Jenuary 2018 | v10.0.0 # Mo, 01 January 2018 | v10.0.0
Πρέπει να ευχαριστήσουμε την [Κυρία Diana](https://www.instagram.com/merry.dii/) για το νέο μας [λογότυπο](https://iris-go.com/images/icon.svg)! Πρέπει να ευχαριστήσουμε την [Κυρία Diana](https://www.instagram.com/merry.dii/) για το νέο μας [λογότυπο](https://iris-go.com/images/icon.svg)!

View File

@ -294,7 +294,7 @@ Follow the examples below,
- [The `url` tmpl func](view/template_html_4/main.go) - [The `url` tmpl func](view/template_html_4/main.go)
- [Inject Data Between Handlers](view/context-view-data/main.go) - [Inject Data Between Handlers](view/context-view-data/main.go)
- [Embedding Templates Into App Executable File](view/embedding-templates-into-app/main.go) - [Embedding Templates Into App Executable File](view/embedding-templates-into-app/main.go)
- [Write to a custom `io.Writer`](view/write-to)
You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [hero templates](https://github.com/shiyanhui/hero/hero) files too, simply by using the `context#ResponseWriter`, take a look at the [http_responsewriter/quicktemplate](http_responsewriter/quicktemplate) and [http_responsewriter/herotemplate](http_responsewriter/herotemplate) examples. You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [hero templates](https://github.com/shiyanhui/hero/hero) files too, simply by using the `context#ResponseWriter`, take a look at the [http_responsewriter/quicktemplate](http_responsewriter/quicktemplate) and [http_responsewriter/herotemplate](http_responsewriter/herotemplate) examples.

View File

@ -0,0 +1,43 @@
package main
import (
"os"
"github.com/kataras/iris"
)
type mailData struct {
Title string
Body string
RefTitle string
RefLink string
}
func main() {
app := iris.New()
app.Logger().SetLevel("debug")
app.RegisterView(iris.HTML("./views", ".html"))
// you need to call `app.Build` manually before using the `app.View` func,
// so templates are built in that state.
app.Build()
// Or a string-buffered writer to use its body to send an e-mail
// for sending e-mails you can use the https://github.com/kataras/go-mailer
// or any other third-party package you like.
//
// The template's parsed result will be written to that writer.
writer := os.Stdout
err := app.View(writer, "email/simple.html", "shared/email.html", mailData{
Title: "This is my e-mail title",
Body: "This is my e-mail body",
RefTitle: "Iris web framework",
RefLink: "https://iris-go.com",
})
if err != nil {
app.Logger().Errorf("error from app.View: %v", err)
}
app.Run(iris.Addr(":8080"))
}

View File

@ -0,0 +1 @@
{{.Body}}

View File

@ -0,0 +1,6 @@
<h1>{{.Title}}</h1>
<p class="body">
{{yield}}
</p>
<a href="{{.RefLink}}" target="_new">{{.RefTitle}}</a>

View File

@ -66,6 +66,8 @@ var emptyHandler = func(ctx context.Context) {
ctx.StopExecution() ctx.StopExecution()
} }
///TODO: debug this and re-run the parallel tests on larger scale,
// because I think we have a bug here when `core/router#StaticWeb` is used after this middleware.
func (h *Handler) ServeHTTP(ctx context.Context) { func (h *Handler) ServeHTTP(ctx context.Context) {
// check for pre-cache validators, if at least one of them return false // check for pre-cache validators, if at least one of them return false
// for this specific request, then skip the whole cache // for this specific request, then skip the whole cache
@ -83,10 +85,16 @@ func (h *Handler) ServeHTTP(ctx context.Context) {
return return
} }
scheme := "http"
if ctx.Request().TLS != nil {
scheme = "https"
}
var ( var (
response *entry.Response response *entry.Response
valid = false valid = false
key = ctx.Path() // unique per subdomains and paths with different url query.
key = scheme + ctx.Host() + ctx.Request().URL.RequestURI()
) )
h.mu.RLock() h.mu.RLock()
@ -99,6 +107,9 @@ func (h *Handler) ServeHTTP(ctx context.Context) {
response, valid = e.Response() response, valid = e.Response()
} else { } else {
// create the entry now. // create the entry now.
// fmt.Printf("create new cache entry\n")
// fmt.Printf("key: %s\n", key)
e = entry.NewEntry(h.expiration) e = entry.NewEntry(h.expiration)
h.mu.Lock() h.mu.Lock()
h.entries[key] = e h.entries[key] = e
@ -132,6 +143,11 @@ func (h *Handler) ServeHTTP(ctx context.Context) {
// given expiration was not valid then check for GetMaxAge & // given expiration was not valid then check for GetMaxAge &
// update the response & release the recorder // update the response & release the recorder
e.Reset(recorder.StatusCode(), recorder.Header().Get(cfg.ContentTypeHeader), body, GetMaxAge(ctx.Request())) e.Reset(recorder.StatusCode(), recorder.Header().Get(cfg.ContentTypeHeader), body, GetMaxAge(ctx.Request()))
// fmt.Printf("reset cache entry\n")
// fmt.Printf("key: %s\n", key)
// fmt.Printf("content type: %s\n", recorder.Header().Get(cfg.ContentTypeHeader))
// fmt.Printf("body len: %d\n", len(body))
return return
} }
@ -139,4 +155,9 @@ func (h *Handler) ServeHTTP(ctx context.Context) {
ctx.ContentType(response.ContentType()) ctx.ContentType(response.ContentType())
ctx.StatusCode(response.StatusCode()) ctx.StatusCode(response.StatusCode())
ctx.Write(response.Body()) ctx.Write(response.Body())
// fmt.Printf("key: %s\n", key)
// fmt.Printf("write content type: %s\n", response.ContentType())
// fmt.Printf("write body len: %d\n", len(response.Body()))
} }