2015-09-29 02:46:58 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2016-06-15 08:21:39 +02:00
|
|
|
"net/http"
|
2015-09-29 02:46:58 +02:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2016-06-15 08:21:39 +02:00
|
|
|
"strings"
|
2015-09-29 02:46:58 +02:00
|
|
|
|
2020-02-17 15:58:56 +01:00
|
|
|
rice "github.com/GeertJohan/go.rice"
|
2015-09-29 02:46:58 +02:00
|
|
|
"github.com/flosch/pongo2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Pongo2Loader struct {
|
|
|
|
box *rice.Box
|
|
|
|
}
|
|
|
|
|
2015-09-29 03:58:50 +02:00
|
|
|
func NewPongo2TemplatesLoader() (*Pongo2Loader, error) {
|
2015-09-29 02:46:58 +02:00
|
|
|
fs := &Pongo2Loader{}
|
|
|
|
|
2015-09-29 03:58:50 +02:00
|
|
|
p2l, err := rice.FindBox("templates")
|
2015-09-29 02:46:58 +02:00
|
|
|
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 {
|
2015-10-06 08:51:49 +02:00
|
|
|
templates := []string{
|
2015-09-29 02:46:58 +02:00
|
|
|
"index.html",
|
2015-09-30 06:56:51 +02:00
|
|
|
"paste.html",
|
2015-10-09 03:50:10 +02:00
|
|
|
"API.html",
|
2015-10-12 10:03:02 +02:00
|
|
|
"400.html",
|
2015-09-29 05:46:43 +02:00
|
|
|
"401.html",
|
2015-10-12 10:03:02 +02:00
|
|
|
"404.html",
|
2015-09-29 02:46:58 +02:00
|
|
|
"oops.html",
|
2020-02-17 15:58:56 +01:00
|
|
|
"access.html",
|
2020-03-12 21:32:35 +01:00
|
|
|
"custom_page.html",
|
2015-09-29 02:46:58 +02:00
|
|
|
|
|
|
|
"display/audio.html",
|
|
|
|
"display/image.html",
|
|
|
|
"display/video.html",
|
|
|
|
"display/pdf.html",
|
2015-09-30 18:06:23 +02:00
|
|
|
"display/bin.html",
|
2015-10-15 18:24:23 +02:00
|
|
|
"display/story.html",
|
2015-10-07 21:00:42 +02:00
|
|
|
"display/md.html",
|
2015-09-29 02:46:58 +02:00
|
|
|
"display/file.html",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tName := range templates {
|
|
|
|
tpl, err := tSet.FromFile(tName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tMap[tName] = tpl
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-15 08:21:39 +02:00
|
|
|
|
|
|
|
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]
|
2016-06-15 14:33:51 +02:00
|
|
|
} else {
|
|
|
|
context["sitename"] = Config.siteName
|
2016-06-15 08:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
context["sitepath"] = Config.sitePath
|
2019-01-14 23:51:02 +01:00
|
|
|
context["selifpath"] = Config.selifPath
|
2020-03-12 21:32:35 +01:00
|
|
|
context["custom_pages_names"] = customPagesNames
|
|
|
|
|
2020-03-07 00:21:49 +01:00
|
|
|
var a string
|
|
|
|
if Config.authFile == "" {
|
|
|
|
a = "none"
|
|
|
|
} else if Config.basicAuth {
|
|
|
|
a = "basic"
|
|
|
|
} else {
|
|
|
|
a = "header"
|
|
|
|
}
|
|
|
|
context["auth"] = a
|
2016-06-15 08:21:39 +02:00
|
|
|
|
|
|
|
return tpl.ExecuteWriter(context, writer)
|
|
|
|
}
|