/
funcmap.go
53 lines (46 loc) · 1.53 KB
/
funcmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package engine
import (
"html/template"
"net/http"
"strings"
log "github.com/sirupsen/logrus"
"github.com/xyproto/datablock"
)
// Functions for concurrent use by rendering.go and handlers.go
// Lua2funcMap runs in a Lua file and returns the functions as a template.FuncMap (or an error)
func (ac *Config) Lua2funcMap(w http.ResponseWriter, req *http.Request, filename, luafilename, ext string, errChan chan error, funcMapChan chan template.FuncMap) {
// Make functions from the given Lua data available
funcs := make(template.FuncMap)
// Try reading data.lua, if possible
luablock, err := ac.cache.Read(luafilename, ac.shouldCache(ext))
if err != nil {
// Could not find and/or read data.lua
luablock = datablock.EmptyDataBlock
// This only means the file wasn't cached, so just ignore this error
}
// luablock can be empty if there was an error or if the file was empty
if luablock.HasData() {
// There was Lua code available. Now make the functions and
// variables available for the template.
funcs, err = ac.LuaFunctionMap(w, req, luablock.Bytes(), luafilename)
if err != nil {
funcMapChan <- funcs
errChan <- err
return
}
if ac.debugMode && ac.verboseMode {
s := "These functions from " + luafilename
s += " are useable for " + filename + ": "
// Create a comma separated list of the available functions
for key := range funcs {
s += key + ", "
}
// Remove the final comma
s = strings.TrimSuffix(s, ", ")
// Output the message
log.Info(s)
}
}
funcMapChan <- funcs
errChan <- err
}