1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-18 01:15:59 +00:00

reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker

Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-06-07 15:26:06 +03:00
parent 9fdcb4c7fb
commit ed45c77be5
328 changed files with 4262 additions and 41621 deletions

View File

@@ -0,0 +1,26 @@
# Hero Template Example
This folder contains the iris version of the original hero's example: https://github.com/shiyanhui/hero/tree/master/examples/app.
Iris is 100% compatible with `net/http` so you don't have to change anything else
except the handler input from the original example.
The only inline handler's changes were:
From:
```go
if _, err := w.Write(buffer.Bytes()); err != nil {
// and
template.UserListToWriter(userList, w)
```
To:
```go
if _, err := ctx.Write(buffer.Bytes()); err != nil {
// and
template.UserListToWriter(userList, ctx)
```
So easy.
Read more at: https://github.com/shiyanhui/hero

View File

@@ -0,0 +1,54 @@
package main
import (
"bytes"
"github.com/kataras/iris/v12/_examples/response-writer/herotemplate/template"
"github.com/kataras/iris/v12"
)
// $ go get -u github.com/shiyanhui/hero/hero
// $ go run app.go
//
// Read more at https://github.com/shiyanhui/hero/hero
func main() {
app := iris.New()
app.Get("/users", func(ctx iris.Context) {
ctx.Gzip(true)
ctx.ContentType("text/html")
userList := []string{
"Alice",
"Bob",
"Tom",
}
// Had better use buffer sync.Pool.
// Hero(github.com/shiyanhui/hero/hero) exports GetBuffer and PutBuffer for this.
//
// buffer := hero.GetBuffer()
// defer hero.PutBuffer(buffer)
// buffer := new(bytes.Buffer)
// template.UserList(userList, buffer)
// ctx.Write(buffer.Bytes())
// using an io.Writer for automatic buffer management (i.e. hero built-in buffer pool),
// iris context implements the io.Writer by its ResponseWriter
// which is an enhanced version of the standard http.ResponseWriter
// but still 100% compatible, GzipResponseWriter too:
// _, err := template.UserListToWriter(userList, ctx.GzipResponseWriter())
buffer := new(bytes.Buffer)
template.UserList(userList, buffer)
_, err := ctx.Write(buffer.Bytes())
if err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
})
app.Listen(":8080")
}

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<%@ body { %>
<% } %>
</body>
</html>

View File

@@ -0,0 +1,3 @@
// Code generated by hero.
// DO NOT EDIT!
package template

View File

@@ -0,0 +1,3 @@
<li>
<%= user %>
</li>

View File

@@ -0,0 +1,3 @@
// Code generated by hero.
// DO NOT EDIT!
package template

View File

@@ -0,0 +1,11 @@
<%: func UserList(userList []string, buffer *bytes.Buffer) %>
<%~ "index.html" %>
<%@ body { %>
<% for _, user := range userList { %>
<ul>
<%+ "user.html" %>
</ul>
<% } %>
<% } %>

View File

@@ -0,0 +1,40 @@
// Code generated by hero.
// DO NOT EDIT!
package template
import (
"bytes"
"github.com/shiyanhui/hero"
)
func UserList(userList []string, buffer *bytes.Buffer) {
buffer.WriteString(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
`)
for _, user := range userList {
buffer.WriteString(`
<ul>
`)
buffer.WriteString(`<li>
`)
hero.EscapeHTML(user, buffer)
buffer.WriteString(`
</li>
`)
buffer.WriteString(`
</ul>
`)
}
buffer.WriteString(`
</body>
</html>
`)
}

View File

@@ -0,0 +1,11 @@
<%: func UserListToWriter(userList []string, w io.Writer) (int, error)%>
<%~ "index.html" %>
<%@ body { %>
<% for _, user := range userList { %>
<ul>
<%+ "user.html" %>
</ul>
<% } %>
<% } %>

View File

@@ -0,0 +1,43 @@
// Code generated by hero.
// DO NOT EDIT!
package template
import (
"io"
"github.com/shiyanhui/hero"
)
func UserListToWriter(userList []string, w io.Writer) (int, error) {
_buffer := hero.GetBuffer()
defer hero.PutBuffer(_buffer)
_buffer.WriteString(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
`)
for _, user := range userList {
_buffer.WriteString(`
<ul>
`)
_buffer.WriteString(`<li>
`)
hero.EscapeHTML(user, _buffer)
_buffer.WriteString(`
</li>
`)
_buffer.WriteString(`
</ul>
`)
}
_buffer.WriteString(`
</body>
</html>
`)
return w.Write(_buffer.Bytes())
}

View File

@@ -0,0 +1,19 @@
First of all, install [quicktemplate](https://github.com/valyala/quicktemplate) package and [quicktemplate compiler](https://github.com/valyala/quicktemplate/tree/master/qtc)
```sh
go get -u github.com/valyala/quicktemplate
go get -u github.com/valyala/quicktemplate/qtc
```
The example has the Go code compiled already for you, therefore:
```sh
go run main.go # http://localhost:8080
```
However there is an instruction below, full documentation can be found at https://github.com/valyala/quicktemplate.
Save your template files into `templates` folder under the extension *.qtpl, open your terminal and run `qtc` inside this folder.
If all went ok, `*.qtpl.go` files must appear in the `templates` folder. These files contain the Go code for all `*.qtpl` files.
> Remember, each time you change a a `/templates/*.qtpl` file you have to run the `qtc` command and re-build your application.

View File

@@ -0,0 +1,14 @@
package controllers
import (
"github.com/kataras/iris/v12/_examples/response-writer/quicktemplate/templates"
"github.com/kataras/iris/v12"
)
// ExecuteTemplate renders a "tmpl" partial template to the `context#ResponseWriter`.
func ExecuteTemplate(ctx iris.Context, tmpl templates.Partial) {
ctx.Gzip(true)
ctx.ContentType("text/html")
templates.WriteTemplate(ctx.ResponseWriter(), tmpl)
}

View File

@@ -0,0 +1,30 @@
package controllers
import (
"github.com/kataras/iris/v12/_examples/response-writer/quicktemplate/templates"
"github.com/kataras/iris/v12"
)
// Hello renders our ../templates/hello.qtpl file using the compiled ../templates/hello.qtpl.go file.
func Hello(ctx iris.Context) {
// vars := make(map[string]interface{})
// vars["message"] = "Hello World!"
// vars["name"] = ctx.Params().Get("name")
// [...]
// &templates.Hello{ Vars: vars }
// [...]
// However, as an alternative, we recommend that you should the `ctx.ViewData(key, value)`
// in order to be able modify the `templates.Hello#Vars` from a middleware(other handlers) as well.
ctx.ViewData("message", "Hello World!")
ctx.ViewData("name", ctx.Params().Get("name"))
// set view data to the `Vars` template's field
tmpl := &templates.Hello{
Vars: ctx.GetViewData(),
}
// render the template
ExecuteTemplate(ctx, tmpl)
}

View File

@@ -0,0 +1,15 @@
package controllers
import (
"github.com/kataras/iris/v12/_examples/response-writer/quicktemplate/templates"
"github.com/kataras/iris/v12"
)
// Index renders our ../templates/index.qtpl file using the compiled ../templates/index.qtpl.go file.
func Index(ctx iris.Context) {
tmpl := &templates.Index{}
// render the template
ExecuteTemplate(ctx, tmpl)
}

View File

@@ -0,0 +1,22 @@
package main
import (
"github.com/kataras/iris/v12/_examples/response-writer/quicktemplate/controllers"
"github.com/kataras/iris/v12"
)
func newApp() *iris.Application {
app := iris.New()
app.Get("/", controllers.Index)
app.Get("/{name}", controllers.Hello)
return app
}
func main() {
app := newApp()
// http://localhost:8080
// http://localhost:8080/yourname
app.Listen(":8080")
}

View File

@@ -0,0 +1,47 @@
package main
import (
"fmt"
"testing"
"github.com/kataras/iris/v12/httptest"
)
func TestResponseWriterQuicktemplate(t *testing.T) {
baseRawBody := `
<html>
<head>
<title>Quicktemplate integration with Iris</title>
</head>
<body>
<div>
Header contents here...
</div>
<div style="margin:10px;">
<h1>%s</h1>
<div>
%s
</div>
</div>
</body>
<footer>
Footer contents here...
</footer>
</html>
`
expectedIndexRawBody := fmt.Sprintf(baseRawBody, "Index Page", "This is our index page's body.")
name := "yourname"
expectedHelloRawBody := fmt.Sprintf(baseRawBody, "Hello World!", "Hello <b>"+name+"!</b>")
app := newApp()
e := httptest.New(t, app)
e.GET("/").Expect().Status(httptest.StatusOK).Body().Equal(expectedIndexRawBody)
e.GET("/" + name).Expect().Status(httptest.StatusOK).Body().Equal(expectedHelloRawBody)
}

View File

@@ -0,0 +1,36 @@
This is our templates' base implementation.
{% interface
Partial {
Body()
}
%}
Template writes a template implementing the Partial interface.
{% func Template(p Partial) %}
<html>
<head>
<title>Quicktemplate integration with Iris</title>
</head>
<body>
<div>
Header contents here...
</div>
<div style="margin:10px;">
{%= p.Body() %}
</div>
</body>
<footer>
Footer contents here...
</footer>
</html>
{% endfunc %}
Base template implementation. Other pages may inherit from it if they need
overriding only certain Partial methods.
{% code type Base struct {} %}
{% func (b *Base) Body() %}This is the base body{% endfunc %}

View File

@@ -0,0 +1,147 @@
// This file is automatically generated by qtc from "base.qtpl".
// See https://github.com/valyala/quicktemplate for details.
// This is our templates' base implementation.
//
//line base.qtpl:3
package templates
//line base.qtpl:3
import (
qtio422016 "io"
qt422016 "github.com/valyala/quicktemplate"
)
//line base.qtpl:3
var (
_ = qtio422016.Copy
_ = qt422016.AcquireByteBuffer
)
//line base.qtpl:4
type Partial interface {
//line base.qtpl:4
Body() string
//line base.qtpl:4
StreamBody(qw422016 *qt422016.Writer)
//line base.qtpl:4
WriteBody(qq422016 qtio422016.Writer)
//line base.qtpl:4
}
// Template writes a template implementing the Partial interface.
//line base.qtpl:11
func StreamTemplate(qw422016 *qt422016.Writer, p Partial) {
//line base.qtpl:11
qw422016.N().S(`
<html>
<head>
<title>Quicktemplate integration with Iris</title>
</head>
<body>
<div>
Header contents here...
</div>
<div style="margin:10px;">
`)
//line base.qtpl:22
p.StreamBody(qw422016)
//line base.qtpl:22
qw422016.N().S(`
</div>
</body>
<footer>
Footer contents here...
</footer>
</html>
`)
//line base.qtpl:30
}
//line base.qtpl:30
func WriteTemplate(qq422016 qtio422016.Writer, p Partial) {
//line base.qtpl:30
qw422016 := qt422016.AcquireWriter(qq422016)
//line base.qtpl:30
StreamTemplate(qw422016, p)
//line base.qtpl:30
qt422016.ReleaseWriter(qw422016)
//line base.qtpl:30
}
//line base.qtpl:30
func Template(p Partial) string {
//line base.qtpl:30
qb422016 := qt422016.AcquireByteBuffer()
//line base.qtpl:30
WriteTemplate(qb422016, p)
//line base.qtpl:30
qs422016 := string(qb422016.B)
//line base.qtpl:30
qt422016.ReleaseByteBuffer(qb422016)
//line base.qtpl:30
return qs422016
//line base.qtpl:30
}
// Base template implementation. Other pages may inherit from it if they need
// overriding only certain Partial methods.
//line base.qtpl:35
type Base struct{}
//line base.qtpl:36
func (b *Base) StreamBody(qw422016 *qt422016.Writer) {
//line base.qtpl:36
qw422016.N().S(`This is the base body`)
}
//line base.qtpl:36
//line base.qtpl:36
func (b *Base) WriteBody(qq422016 qtio422016.Writer) {
//line base.qtpl:36
qw422016 := qt422016.AcquireWriter(qq422016)
//line base.qtpl:36
b.StreamBody(qw422016)
//line base.qtpl:36
qt422016.ReleaseWriter(qw422016)
//line base.qtpl:36
}
//line base.qtpl:36
func (b *Base) Body() string {
//line base.qtpl:36
qb422016 := qt422016.AcquireByteBuffer()
//line base.qtpl:36
b.WriteBody(qb422016)
//line base.qtpl:36
qs422016 := string(qb422016.B)
//line base.qtpl:36
qt422016.ReleaseByteBuffer(qb422016)
//line base.qtpl:36
return qs422016
//line base.qtpl:36
}

View File

@@ -0,0 +1,14 @@
Hello template, implements the Partial's methods.
{% code
type Hello struct {
Vars map[string]interface{}
}
%}
{% func (h *Hello) Body() %}
<h1>{%v h.Vars["message"] %}</h1>
<div>
Hello <b>{%v h.Vars["name"] %}!</b>
</div>
{% endfunc %}

View File

@@ -0,0 +1,82 @@
// This file is automatically generated by qtc from "hello.qtpl".
// See https://github.com/valyala/quicktemplate for details.
// Hello template, implements the Partial's methods.
//
//line hello.qtpl:3
package templates
//line hello.qtpl:3
import (
qtio422016 "io"
qt422016 "github.com/valyala/quicktemplate"
)
//line hello.qtpl:3
var (
_ = qtio422016.Copy
_ = qt422016.AcquireByteBuffer
)
//line hello.qtpl:4
type Hello struct {
Vars map[string]interface{}
}
//line hello.qtpl:9
func (h *Hello) StreamBody(qw422016 *qt422016.Writer) {
//line hello.qtpl:9
qw422016.N().S(`
<h1>`)
//line hello.qtpl:10
qw422016.E().V(h.Vars["message"])
//line hello.qtpl:10
qw422016.N().S(`</h1>
<div>
Hello <b>`)
//line hello.qtpl:12
qw422016.E().V(h.Vars["name"])
//line hello.qtpl:12
qw422016.N().S(`!</b>
</div>
`)
//line hello.qtpl:14
}
//line hello.qtpl:14
func (h *Hello) WriteBody(qq422016 qtio422016.Writer) {
//line hello.qtpl:14
qw422016 := qt422016.AcquireWriter(qq422016)
//line hello.qtpl:14
h.StreamBody(qw422016)
//line hello.qtpl:14
qt422016.ReleaseWriter(qw422016)
//line hello.qtpl:14
}
//line hello.qtpl:14
func (h *Hello) Body() string {
//line hello.qtpl:14
qb422016 := qt422016.AcquireByteBuffer()
//line hello.qtpl:14
h.WriteBody(qb422016)
//line hello.qtpl:14
qs422016 := string(qb422016.B)
//line hello.qtpl:14
qt422016.ReleaseByteBuffer(qb422016)
//line hello.qtpl:14
return qs422016
//line hello.qtpl:14
}

View File

@@ -0,0 +1,12 @@
Index template, implements the Partial's methods.
{% code
type Index struct {}
%}
{% func (i *Index) Body() %}
<h1>Index Page</h1>
<div>
This is our index page's body.
</div>
{% endfunc %}

View File

@@ -0,0 +1,72 @@
// This file is automatically generated by qtc from "index.qtpl".
// See https://github.com/valyala/quicktemplate for details.
// Index template, implements the Partial's methods.
//
//line index.qtpl:3
package templates
//line index.qtpl:3
import (
qtio422016 "io"
qt422016 "github.com/valyala/quicktemplate"
)
//line index.qtpl:3
var (
_ = qtio422016.Copy
_ = qt422016.AcquireByteBuffer
)
//line index.qtpl:4
type Index struct{}
//line index.qtpl:7
func (i *Index) StreamBody(qw422016 *qt422016.Writer) {
//line index.qtpl:7
qw422016.N().S(`
<h1>Index Page</h1>
<div>
This is our index page's body.
</div>
`)
//line index.qtpl:12
}
//line index.qtpl:12
func (i *Index) WriteBody(qq422016 qtio422016.Writer) {
//line index.qtpl:12
qw422016 := qt422016.AcquireWriter(qq422016)
//line index.qtpl:12
i.StreamBody(qw422016)
//line index.qtpl:12
qt422016.ReleaseWriter(qw422016)
//line index.qtpl:12
}
//line index.qtpl:12
func (i *Index) Body() string {
//line index.qtpl:12
qb422016 := qt422016.AcquireByteBuffer()
//line index.qtpl:12
i.WriteBody(qb422016)
//line index.qtpl:12
qs422016 := string(qb422016.B)
//line index.qtpl:12
qt422016.ReleaseByteBuffer(qb422016)
//line index.qtpl:12
return qs422016
//line index.qtpl:12
}