lessmore/node/ssr.go

59 lines
1.1 KiB
Go
Raw Normal View History

2021-03-25 18:11:00 +03:00
package node
import (
"net/http"
"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,
}
}
2021-03-26 11:11:14 +03:00
type PageData struct {
Echoes []echo
CurrentPage string
Posts []i2es.ESDoc
2021-03-25 18:11:00 +03:00
}
2021-03-26 11:11:14 +03:00
func (s *ssr) newPageData(page string, posts []i2es.ESDoc) *PageData {
return &PageData{
Echoes: s.es.GetEchoesList(),
Posts: posts,
CurrentPage: page,
2021-03-25 18:11:00 +03:00
}
}
func (s *ssr) ssrRootHandler(w http.ResponseWriter, r *http.Request) {
tpl, err := s.getTemplate("root")
if err != nil {
log.Error(err)
return
}
2021-03-26 11:11:14 +03:00
if err := tpl.Execute(w, s.newPageData("", s.es.GetLatestPosts(50))); err != nil {
log.Error(err)
}
}
func (s *ssr) ssrForumHandler(w http.ResponseWriter, r *http.Request) {
tpl, err := s.getTemplate("forum")
if err != nil {
log.Error(err)
return
2021-03-25 18:11:00 +03:00
}
2021-03-26 11:11:14 +03:00
if err := tpl.Execute(w, s.newPageData("", s.es.GetThreads())); err != nil {
2021-03-25 18:11:00 +03:00
log.Error(err)
}
}