lessmore/node/ssr.go

68 lines
1.2 KiB
Go

package node
import (
"html/template"
"net/http"
"os"
"gitea.difrex.ru/Umbrella/fetcher/i2es"
log "github.com/sirupsen/logrus"
)
type ssr struct {
es ESConf
templatesDir string
}
func newSSR(templatesDir string, es ESConf) *ssr {
return &ssr{
es: es,
templatesDir: templatesDir,
}
}
func (s *ssr) templatePath(name string) string {
return s.templatesDir + string(os.PathSeparator) + name
}
func (s *ssr) getTemplate(name string) (*template.Template, error) {
t := template.New("root.html")
tpl, err := t.ParseFiles(
s.templatePath(name+".html"),
s.templatePath("style.html"),
s.templatePath("echoes.html"),
s.templatePath("post.html"),
s.templatePath("latest_posts.html"),
s.templatePath("header.html"),
s.templatePath("footer.html"),
)
if err != nil {
return nil, err
}
return tpl, nil
}
func (s *ssr) ssrRootHandler(w http.ResponseWriter, r *http.Request) {
LogRequest(r)
tpl, err := s.getTemplate("root")
if err != nil {
log.Error(err)
return
}
var root struct {
Echoes []echo
CurrentPage string
Posts []i2es.ESDoc
}
root.Echoes = s.es.GetEchoesList()
root.Posts = s.es.GetLatestPosts(100)
if err := tpl.Execute(w, root); err != nil {
log.Error(err)
}
}