2020-05-13 12:07:44 +00:00
|
|
|
package alps
|
2019-12-02 14:31:00 +00:00
|
|
|
|
|
|
|
import (
|
2019-12-09 15:02:12 +00:00
|
|
|
"fmt"
|
2019-12-02 14:31:00 +00:00
|
|
|
"html/template"
|
|
|
|
"io"
|
2019-12-10 15:41:56 +00:00
|
|
|
"io/ioutil"
|
2020-05-13 14:58:22 +00:00
|
|
|
"net/url"
|
2019-12-10 15:41:56 +00:00
|
|
|
"os"
|
2020-05-13 14:58:22 +00:00
|
|
|
"strings"
|
2019-12-02 14:31:00 +00:00
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2019-12-16 11:57:30 +00:00
|
|
|
const themesDir = "themes"
|
2019-12-10 15:41:56 +00:00
|
|
|
|
2019-12-10 16:36:21 +00:00
|
|
|
// GlobalRenderData contains data available in all templates.
|
|
|
|
type GlobalRenderData struct {
|
2020-05-13 14:58:22 +00:00
|
|
|
Path []string
|
2020-05-13 14:46:35 +00:00
|
|
|
URL *url.URL
|
2020-01-16 12:36:03 +00:00
|
|
|
|
2019-12-10 16:36:21 +00:00
|
|
|
LoggedIn bool
|
|
|
|
|
|
|
|
// if logged in
|
|
|
|
Username string
|
|
|
|
|
2020-05-13 14:46:35 +00:00
|
|
|
Title string
|
|
|
|
|
2020-10-22 15:36:26 +00:00
|
|
|
HavePlugin func(name string) bool
|
|
|
|
|
2019-12-17 14:01:15 +00:00
|
|
|
// additional plugin-specific data
|
2019-12-10 16:36:21 +00:00
|
|
|
Extra map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2019-12-17 12:27:20 +00:00
|
|
|
// BaseRenderData is the base type for templates. It should be extended with
|
2019-12-17 14:01:15 +00:00
|
|
|
// additional template-specific fields:
|
|
|
|
//
|
|
|
|
// type MyRenderData struct {
|
|
|
|
// BaseRenderData
|
|
|
|
// // add additional fields here
|
|
|
|
// }
|
2019-12-17 12:27:20 +00:00
|
|
|
type BaseRenderData struct {
|
2019-12-17 14:01:15 +00:00
|
|
|
GlobalData GlobalRenderData
|
|
|
|
// additional plugin-specific data
|
2019-12-11 14:24:39 +00:00
|
|
|
Extra map[string]interface{}
|
2019-12-10 16:36:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 14:01:15 +00:00
|
|
|
// Global implements RenderData.
|
|
|
|
func (brd *BaseRenderData) Global() *GlobalRenderData {
|
|
|
|
return &brd.GlobalData
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderData is implemented by template data structs. It can be used to inject
|
|
|
|
// additional data to all templates.
|
|
|
|
type RenderData interface {
|
|
|
|
// GlobalData returns a pointer to the global render data.
|
|
|
|
Global() *GlobalRenderData
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBaseRenderData initializes a new BaseRenderData.
|
|
|
|
//
|
|
|
|
// It can be used by routes to pre-fill the base data:
|
|
|
|
//
|
|
|
|
// type MyRenderData struct {
|
|
|
|
// BaseRenderData
|
|
|
|
// // add additional fields here
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// data := &MyRenderData{
|
2020-05-13 12:07:44 +00:00
|
|
|
// BaseRenderData: *alps.NewBaseRenderData(ctx),
|
2019-12-17 14:01:15 +00:00
|
|
|
// // other fields...
|
|
|
|
// }
|
2020-11-19 15:09:05 +00:00
|
|
|
func NewBaseRenderData(ectx echo.Context) *BaseRenderData {
|
|
|
|
ctx, isactx := ectx.(*Context)
|
|
|
|
|
2020-05-13 14:46:35 +00:00
|
|
|
global := GlobalRenderData{
|
|
|
|
Extra: make(map[string]interface{}),
|
2020-11-19 15:09:05 +00:00
|
|
|
Path: strings.Split(ectx.Request().URL.Path, "/")[1:],
|
2020-05-13 14:46:35 +00:00
|
|
|
Title: "Webmail",
|
2020-11-19 15:09:05 +00:00
|
|
|
URL: ectx.Request().URL,
|
2020-10-22 15:36:26 +00:00
|
|
|
|
|
|
|
HavePlugin: func(name string) bool {
|
2020-11-19 15:09:05 +00:00
|
|
|
if !isactx {
|
|
|
|
return false
|
|
|
|
}
|
2020-10-22 15:36:26 +00:00
|
|
|
for _, plugin := range ctx.Server.plugins {
|
|
|
|
if plugin.Name() == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
2020-05-13 14:46:35 +00:00
|
|
|
}
|
2019-12-10 16:36:21 +00:00
|
|
|
|
2020-11-19 15:09:05 +00:00
|
|
|
if isactx && ctx.Session != nil {
|
2019-12-10 16:36:21 +00:00
|
|
|
global.LoggedIn = true
|
2019-12-11 11:48:18 +00:00
|
|
|
global.Username = ctx.Session.username
|
2019-12-10 16:36:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 12:27:20 +00:00
|
|
|
return &BaseRenderData{
|
2019-12-17 14:01:15 +00:00
|
|
|
GlobalData: global,
|
|
|
|
Extra: make(map[string]interface{}),
|
2019-12-10 16:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 14:46:35 +00:00
|
|
|
func (brd *BaseRenderData) WithTitle(title string) *BaseRenderData {
|
|
|
|
brd.GlobalData.Title = title
|
|
|
|
return brd
|
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
type renderer struct {
|
2020-01-08 09:52:28 +00:00
|
|
|
logger echo.Logger
|
2019-12-10 15:41:56 +00:00
|
|
|
defaultTheme string
|
2020-01-08 09:52:28 +00:00
|
|
|
|
|
|
|
base *template.Template
|
|
|
|
themes map[string]*template.Template
|
2019-12-02 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
func (r *renderer) Render(w io.Writer, name string, data interface{}, ectx echo.Context) error {
|
2019-12-11 11:48:18 +00:00
|
|
|
// ectx is the raw *echo.context, not our own *Context
|
|
|
|
ctx := ectx.Get("context").(*Context)
|
2019-12-09 15:02:12 +00:00
|
|
|
|
2020-01-10 18:34:59 +00:00
|
|
|
var renderData RenderData
|
|
|
|
if data == nil {
|
2020-01-20 12:22:18 +00:00
|
|
|
renderData = &struct{ BaseRenderData }{*NewBaseRenderData(ctx)}
|
2020-01-10 18:34:59 +00:00
|
|
|
} else {
|
|
|
|
var ok bool
|
|
|
|
renderData, ok = data.(RenderData)
|
|
|
|
if !ok {
|
2020-01-20 21:06:47 +00:00
|
|
|
return fmt.Errorf("data passed to template %q doesn't implement RenderData", name)
|
2020-01-10 18:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 10:50:29 +00:00
|
|
|
for _, plugin := range ctx.Server.plugins {
|
2020-01-10 18:34:59 +00:00
|
|
|
if err := plugin.Inject(ctx, name, renderData); err != nil {
|
2020-01-20 21:06:47 +00:00
|
|
|
return fmt.Errorf("failed to run plugin %q: %v", plugin.Name(), err)
|
2019-12-09 15:02:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
// TODO: per-user theme selection
|
|
|
|
t := r.base
|
|
|
|
if r.defaultTheme != "" {
|
|
|
|
t = r.themes[r.defaultTheme]
|
|
|
|
}
|
|
|
|
return t.ExecuteTemplate(w, name, data)
|
2019-12-02 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
func loadTheme(name string, base *template.Template) (*template.Template, error) {
|
|
|
|
theme, err := base.Clone()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-16 11:57:30 +00:00
|
|
|
theme, err = theme.ParseGlob(themesDir + "/" + name + "/*.html")
|
2019-12-10 15:41:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return theme, nil
|
|
|
|
}
|
|
|
|
|
2020-01-08 10:50:29 +00:00
|
|
|
func (r *renderer) Load(plugins []Plugin) error {
|
2019-12-16 11:51:42 +00:00
|
|
|
base := template.New("")
|
2019-12-04 17:30:01 +00:00
|
|
|
|
2019-12-11 14:08:31 +00:00
|
|
|
for _, p := range plugins {
|
|
|
|
if err := p.LoadTemplate(base); err != nil {
|
2020-01-20 21:06:47 +00:00
|
|
|
return fmt.Errorf("failed to load template for plugin %q: %v", p.Name(), err)
|
2019-12-11 14:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
themes := make(map[string]*template.Template)
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(themesDir)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2020-01-08 09:52:28 +00:00
|
|
|
return err
|
2019-12-04 17:30:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 15:41:56 +00:00
|
|
|
for _, fi := range files {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-20 21:06:47 +00:00
|
|
|
r.logger.Printf("Loading theme %q", fi.Name())
|
2019-12-10 15:41:56 +00:00
|
|
|
var err error
|
|
|
|
if themes[fi.Name()], err = loadTheme(fi.Name(), base); err != nil {
|
2020-01-20 21:06:47 +00:00
|
|
|
return fmt.Errorf("failed to load theme %q: %v", fi.Name(), err)
|
2019-12-10 15:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:52:28 +00:00
|
|
|
if r.defaultTheme != "" {
|
|
|
|
if _, ok := themes[r.defaultTheme]; !ok {
|
2020-01-20 21:06:47 +00:00
|
|
|
return fmt.Errorf("failed to find default theme %q", r.defaultTheme)
|
2019-12-04 17:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 09:52:28 +00:00
|
|
|
r.base = base
|
|
|
|
r.themes = themes
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRenderer(logger echo.Logger, defaultTheme string) *renderer {
|
2019-12-10 15:41:56 +00:00
|
|
|
return &renderer{
|
2020-01-08 09:52:28 +00:00
|
|
|
logger: logger,
|
2019-12-10 15:41:56 +00:00
|
|
|
defaultTheme: defaultTheme,
|
2020-01-08 09:52:28 +00:00
|
|
|
}
|
2019-12-02 14:31:00 +00:00
|
|
|
}
|