Skip to content
Permalink
Browse files

Update template execute, pass session and other modules

  • Loading branch information...
Raggaer committed Jun 11, 2019
1 parent c36923a commit 38c93e1d88949d951fabe7744b28aaebd386d520
Showing with 102 additions and 19 deletions.
  1. +4 −0 README.md
  2. +1 −1 app/controllers/controller.go
  3. +11 −2 app/lua/template.go
  4. +24 −4 app/template/template.go
  5. +12 −1 example/views/home.html
  6. +8 −0 go.mod
  7. +25 −0 go.sum
  8. +7 −3 main.go
  9. +9 −7 test/suite.go
  10. +1 −1 test/views/execute.html
@@ -52,3 +52,7 @@ All the testing is done on the `test` folder, where a simple example of how `bis
Then a request is simulated using `net/http` package

Since the testing package is basically a `bison` application, it can be used as a base/example on how should your directory structure look like

## Example

You can find a complete of how to make an application using `bison` on the `example` directory.
@@ -82,7 +82,7 @@ func (h *Handler) MainRoute(ctx *fasthttp.RequestCtx) {
state := lua.NewState([]*lua.Module{
lua.NewHTTPModule(ctx, params),
lua.NewConfigModule(h.Config.Custom),
lua.NewTemplateModule(h.Tpl, ctx),
lua.NewTemplateModule(h.Tpl, ctx, session),
lua.NewURLModule(),
lua.NewCacheModule(h.Cache),
lua.NewSessionModule(session),
@@ -3,6 +3,7 @@ package lua
import (
"html/template"

"github.com/fasthttp-contrib/sessions"
"github.com/valyala/fasthttp"
glua "github.com/yuin/gopher-lua"
)
@@ -11,13 +12,15 @@ import (
type TemplateModule struct {
Tpl *template.Template
RequestContext *fasthttp.RequestCtx
Session sessions.Session
}

// NewTemplateModule returns a new template module
func NewTemplateModule(tpl *template.Template, ctx *fasthttp.RequestCtx) *Module {
func NewTemplateModule(tpl *template.Template, ctx *fasthttp.RequestCtx, session sessions.Session) *Module {
module := &TemplateModule{
Tpl: tpl,
RequestContext: ctx,
Session: session,
}
return &Module{
Name: "template",
@@ -32,7 +35,13 @@ func NewTemplateModule(tpl *template.Template, ctx *fasthttp.RequestCtx) *Module
func (t *TemplateModule) Render(state *glua.LState) int {
name := state.ToString(1)
data := state.ToTable(2)
if err := t.Tpl.ExecuteTemplate(t.RequestContext, name, TableToMap(data)); err != nil {
tbl := TableToMap(data)

// Add custom needed fields for the execute function
tbl["_RequestContext"] = t.RequestContext
tbl["_Session"] = t.Session

if err := t.Tpl.ExecuteTemplate(t.RequestContext, name, tbl); err != nil {
state.RaiseError("Unale to render template %s - %s", name, err)
}
return 0
@@ -8,13 +8,18 @@ import (

"github.com/Raggaer/bison/app/config"
"github.com/Raggaer/bison/app/lua"
"github.com/fasthttp-contrib/sessions"
cache "github.com/patrickmn/go-cache"
"github.com/valyala/fasthttp"
glua "github.com/yuin/gopher-lua"
)

// TemplateFuncData data needed for template functions
type TemplateFuncData struct {
Config *config.Config
Files map[string]*glua.FunctionProto
Cache *cache.Cache
Config *config.Config
ControllersPath string
Files map[string]*glua.FunctionProto
}

// LoadTemplates load the given view directory
@@ -37,15 +42,30 @@ func LoadTemplates(dir string, data *TemplateFuncData) (*template.Template, erro

func templateFuncMap(h *TemplateFuncData) template.FuncMap {
return map[string]interface{}{
"execute": func(file string) template.HTML {
proto, ok := h.Files[filepath.Join("controllers", file)]
"execute": func(file string, data map[string]interface{}) template.HTML {
// Load values from the map
_, ok := data["_RequestContext"].(*fasthttp.RequestCtx)
if !ok {
return ""
}
session, ok := data["_Session"].(sessions.Session)
if !ok {
return ""
}

proto, ok := h.Files[filepath.Join(h.ControllersPath, file)]
if !ok {
return ""
}

// Create state with basic bison modules
state := lua.NewState([]*lua.Module{
lua.NewConfigModule(h.Config.Custom),
lua.NewURLModule(),
lua.NewCacheModule(h.Cache),
lua.NewSessionModule(session),
lua.NewJSONModule(),
lua.NewEnvironmentModule(),
})
defer state.Close()

@@ -1 +1,12 @@
Hello World
<html>
<head>
<title>
{{ execute "title.lua" . }}
</title>
</head>
<body>
<b>
Hello World!
</b>
</body>
</html>
8 go.mod
@@ -6,7 +6,15 @@ require (
github.com/buaazp/fasthttprouter v0.1.1
github.com/clbanning/mxj v1.8.4
github.com/fasthttp-contrib/sessions v0.0.0-20160905201309-74f6ac73d5d5
github.com/gorilla/sessions v1.1.3
github.com/klauspost/compress v1.7.0 // indirect
github.com/klauspost/cpuid v1.2.1 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/valyala/fasthttp v1.3.0
github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 // indirect
golang.org/x/net v0.0.0-20190607181551-461777fb6f67 // indirect
golang.org/x/sys v0.0.0-20190610200419-93c9922d18ae // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20190610231749-f8d1dee965f7 // indirect
)
25 go.sum
@@ -7,18 +7,43 @@ github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I=
github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
github.com/fasthttp-contrib/sessions v0.0.0-20160905201309-74f6ac73d5d5 h1:M4CVMQ5ueVmGZAtkW2bsO+ftesCYpfxl27JTqtzKBzE=
github.com/fasthttp-contrib/sessions v0.0.0-20160905201309-74f6ac73d5d5/go.mod h1:MQXNGeXkpojWTxbN7vXoE3f7EmlA11MlJbsrJpVBINA=
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.1.3 h1:uXoZdcdA5XdXF3QzuSlheVRUvjl+1rKY7zBXL68L9RU=
github.com/gorilla/sessions v1.1.3/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
github.com/klauspost/compress v1.4.0 h1:8nsMz3tWa9SWWPL60G1V6CUsf4lLjWLTNEtibhe8gh8=
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.7.0 h1:xhgn4klsgedJtXrB3U5hm1HCMOAmYV3c6e+xCwDtshM=
github.com/klauspost/compress v1.7.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e h1:+lIPJOWl+jSiJOc70QXJ07+2eg2Jy2EC7Mi11BWujeM=
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w=
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62 h1:pyecQtsPmlkCsMkYhT5iZ+sUXuwee+OvfuJjinEA3ko=
github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62/go.mod h1:65XQgovT59RWatovFwnwocoUxiI/eENTnOY5GK3STuY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.3.0 h1:++0WUtakkqBuHHY5JRFFl6O44I03XLBqxNnrBX0yH7Y=
github.com/valyala/fasthttp v1.3.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036 h1:1b6PAtenNyhsmo/NKXVe34h7JEZKva1YB/ne7K7mqKM=
github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190607181551-461777fb6f67/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190610200419-93c9922d18ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190610231749-f8d1dee965f7/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
10 main.go
@@ -50,13 +50,17 @@ func main() {
return
}

cacheStorage := cache.New(time.Minute*5, time.Minute*10)

// Load all templates
if viewsPath == "" {
viewsPath = filepath.Join("app", "views")
}
tpl, err := template.LoadTemplates(viewsPath, &template.TemplateFuncData{
Config: config,
Files: files,
Config: config,
Cache: cacheStorage,
Files: files,
ControllersPath: controllersPath,
})
if err != nil {
fmt.Printf("Unable to load views files: %v", err)
@@ -80,7 +84,7 @@ func main() {
Routes: routes,
Files: files,
Tpl: tpl,
Cache: cache.New(time.Minute*5, time.Minute*10),
Cache: cacheStorage,
ControllersPath: controllersPath,
RouterPath: routerPath,
ViewsPath: viewsPath,
@@ -36,8 +36,9 @@ func createTestServer(p chan<- int, t *testing.T) io.Closer {

// Load all templates
tpl, err := template.LoadTemplates(filepath.Join("views"), &template.TemplateFuncData{
Config: config,
Files: files,
Config: config,
Files: files,
ControllersPath: filepath.Join("controllers"),
})
if err != nil {
log.Fatal(err)
@@ -52,11 +53,12 @@ func createTestServer(p chan<- int, t *testing.T) io.Closer {

// Create fasthttp server
handler := &controllers.Handler{
Config: config,
Routes: routes,
Files: files,
Tpl: tpl,
Cache: cache.New(time.Minute*5, time.Minute*10),
Config: config,
Routes: routes,
Files: files,
Tpl: tpl,
Cache: cache.New(time.Minute*5, time.Minute*10),
ControllersPath: filepath.Join("controllers"),
}

for _, rx := range routes {
@@ -1 +1 @@
<p>Framework: {{ execute "template/execute_tpl.lua" }}</p><p>Author: {{ .author }}</p>
<p>Framework: {{ execute "template/execute_tpl.lua" . }}</p><p>Author: {{ .author }}</p>

0 comments on commit 38c93e1

Please sign in to comment.
You can’t perform that action at this time.