forked from ZizzyDizzyMC/linx-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.go
108 lines (90 loc) · 2.13 KB
/
templates.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
package main
import (
"bytes"
"io"
"net/http"
"path"
"path/filepath"
"strings"
rice "github.com/GeertJohan/go.rice"
"github.com/flosch/pongo2"
)
type Pongo2Loader struct {
box *rice.Box
}
func NewPongo2TemplatesLoader() (*Pongo2Loader, error) {
fs := &Pongo2Loader{}
p2l, err := rice.FindBox("templates")
if err != nil {
return nil, err
}
fs.box = p2l
return fs, nil
}
func (fs *Pongo2Loader) Get(path string) (io.Reader, error) {
myBytes, err := fs.box.Bytes(path)
if err != nil {
return nil, err
}
return bytes.NewReader(myBytes), nil
}
func (fs *Pongo2Loader) Abs(base, name string) string {
me := path.Join(filepath.Dir(base), name)
return me
}
func populateTemplatesMap(tSet *pongo2.TemplateSet, tMap map[string]*pongo2.Template) error {
templates := []string{
"index.html",
"paste.html",
"API.html",
"400.html",
"401.html",
"404.html",
"oops.html",
"access.html",
"custom_page.html",
"display/audio.html",
"display/image.html",
"display/video.html",
"display/pdf.html",
"display/bin.html",
"display/story.html",
"display/md.html",
"display/file.html",
}
for _, tName := range templates {
tpl, err := tSet.FromFile(tName)
if err != nil {
return err
}
tMap[tName] = tpl
}
return nil
}
func renderTemplate(tpl *pongo2.Template, context pongo2.Context, r *http.Request, writer io.Writer) error {
if Config.siteName == "" {
parts := strings.Split(r.Host, ":")
context["sitename"] = parts[0]
} else {
context["sitename"] = Config.siteName
}
context["sitepath"] = Config.sitePath
context["selifpath"] = Config.selifPath
context["custom_pages_names"] = customPagesNames
// Add the context for Config.extraFooterText
context["extra_footer_text"] = Config.extraFooterText
// Add the context for Config.disableAccessKey
context["disable_access_key"] = Config.disableAccessKey
context["default_randomize"] = Config.defaultRandomFilename
context["hide_powered_by"] = Config.hidePoweredBy
var a string
if Config.authFile == "" {
a = "none"
} else if Config.basicAuth {
a = "basic"
} else {
a = "header"
}
context["auth"] = a
return tpl.ExecuteWriter(context, writer)
}