forked from ungerik/go-start
/
functions.go
133 lines (111 loc) · 3.25 KB
/
functions.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package view
import (
"io"
"strconv"
"github.com/ungerik/go-start/config"
"github.com/ungerik/go-start/errs"
"github.com/ungerik/go-start/templatesystem"
"github.com/ungerik/go-start/utils"
"github.com/ungerik/web.go"
// "github.com/ungerik/go-start/debug"
)
var viewIdChan = make(chan string, 16)
var viewIdCounter int64
func init() {
go func() {
for {
viewIdCounter++
// Use base32 encoding for ids to make them shorter
viewIdChan <- "i" + strconv.FormatInt(viewIdCounter, 32)
}
}()
}
// var viewsByID map[string]View = map[string]View{}
var viewsByPath = map[string]View{}
func NewViewID(view View) (id string) {
id = <-viewIdChan
// viewsByID[id] = view
return id
}
// func DeleteViewID(id string) {
// if _, exists := viewsByID[id]; !exists {
// panic("View ID '" + id + "' does not exist")
// }
// delete(viewsByID, id)
// }
func FindStaticFile(filename string) (filePath string, found bool, modifiedTime int64) {
return utils.FindFile2ModifiedTime(Config.BaseDirs, Config.StaticDirs, filename)
}
func FindTemplateFile(filename string) (filePath string, found bool, modifiedTime int64) {
return utils.FindFile2ModifiedTime(Config.BaseDirs, Config.TemplateDirs, filename)
}
//func ViewChanged(view View) {
//}
// RunServer starts a webserver with the given paths.
// If paths is nil, only static files will be served.
func RunServer(paths *ViewPath) {
if !Config.initialized {
err := Config.Init()
if err != nil {
panic(err)
}
}
addr := Config.ListenAndServeAt
if !Config.IsProductionServer && Config.Debug.ListenAndServeAt != "" {
addr = Config.Debug.ListenAndServeAt
}
RunServerAddr(addr, paths)
}
// RunServerAddr starts a webserver with the given paths and address.
// If paths is nil, only static files will be served.
func RunServerAddr(addr string, paths *ViewPath) {
if !Config.initialized {
err := Config.Init()
if err != nil {
panic(err)
}
}
config.Logger.Print("view.Config.IsProductionServer = ", Config.IsProductionServer)
config.Logger.Print("view.Config.Debug.Mode = ", Config.Debug.Mode)
if paths != nil {
paths.initAndRegisterViewsRecursive("/")
}
web.Config.StaticDirs = utils.CombineDirs(Config.BaseDirs, Config.StaticDirs)
web.Config.RecoverPanic = Config.Debug.Mode
web.Config.CookieSecret = Config.CookieSecret
web.Run(addr)
}
func RenderTemplate(filename string, out io.Writer, context interface{}) (err error) {
filePath, found, _ := FindTemplateFile(filename)
if !found {
return errs.Format("Template file not found: %s", filename)
}
var templ templatesystem.Template
templ, err = Config.TemplateSystem.ParseFile(filePath)
if err != nil {
return
}
// context = append(context, Config)
return templ.Render(out, context)
}
func RenderTemplateString(tmplString string, name string, out io.Writer, context interface{}) (err error) {
var templ templatesystem.Template
templ, err = Config.TemplateSystem.ParseString(tmplString, name)
if err != nil {
return
}
// context = append(context, Config)
return templ.Render(out, context)
}
func RenderChildViewsHTML(parent View, ctx *Context) (err error) {
parent.IterateChildren(func(parent View, child View) (next bool) {
if child != nil {
err = child.Render(ctx)
if err != nil {
return false
}
}
return true
})
return err
}