package node import ( "encoding/json" "net/http" "time" "strconv" "gitea.difrex.ru/Umbrella/fetcher/i2es" "github.com/gorilla/mux" 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, } } type PageData struct { Echoes []echo CurrentPage string PageNum int Posts []i2es.ESDoc } func (p *PageData) GetDate(date string) string { d, err := strconv.ParseInt(date, 0, 64) if err != nil { return err.Error() } return time.Unix(d, 0).UTC().Format("02 Jan 06 15:04 MST") } func (p *PageData) Inc() int { return p.PageNum + 1 } func (p *PageData) Dec() int { return p.PageNum - 1 } func (s *ssr) newPageData(page string, posts []i2es.ESDoc, num int) *PageData { return &PageData{ Echoes: s.es.GetEchoesList(), Posts: posts, CurrentPage: page, PageNum: num, } } func (s *ssr) ssrRootHandler(w http.ResponseWriter, r *http.Request) { tpl, err := s.getTemplate("root") if err != nil { log.Error(err) return } if err := tpl.Execute(w, s.newPageData("feed", s.es.GetLatestPosts(50), 1)); 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 } vars := mux.Vars(r) var num int if _, ok := vars["page"]; ok { num = getPageNum(mux.Vars(r)["page"]) } if err := tpl.Execute(w, s.newPageData("forum", s.es.GetThreads(num), num)); err != nil { log.Error(err) } } func (s *ssr) threadViewHandler(w http.ResponseWriter, r *http.Request) { tpl, err := s.getTemplate("thread") if err != nil { log.Error(err) return } topicid, ok := mux.Vars(r)["topicid"] if !ok { log.Warn("empty topicid") w.WriteHeader(http.StatusBadRequest) w.Write([]byte("error: empty topicid")) return } posts := s.es.GetTopic(topicid) thread := "nil" if len(posts) > 0 { thread = posts[0].Subg } if err := tpl.Execute(w, s.newPageData(thread, posts, 1)); err != nil { log.Error(err) } } func getPageNum(page string) int { i, err := strconv.ParseInt(page, 0, 64) if err != nil { log.Error(err) return 1 } if i < 1 { return 1 } return int(i) } func (s *ssr) echoViewHandler(w http.ResponseWriter, r *http.Request) { tpl, err := s.getTemplate("echo") if err != nil { log.Error(err) return } vars := mux.Vars(r) echo, ok := vars["echo"] if !ok { log.Warn("empty echo") w.WriteHeader(http.StatusBadRequest) w.Write([]byte("error: empty echo")) return } page := 1 if _, ok := vars["page"]; ok { page = getPageNum(vars["page"]) } if err := tpl.Execute(w, s.newPageData(echo, s.es.GetThreads(page, echo), page)); err != nil { log.Error(err) } } func (s *ssr) singleMessageHandler(w http.ResponseWriter, r *http.Request) { tpl, err := s.getTemplate("message") if err != nil { log.Error(err) return } msgid, ok := mux.Vars(r)["msgid"] if !ok { log.Warn("empty msgid") w.WriteHeader(http.StatusBadRequest) w.Write([]byte("error: empty msgid")) return } if err := tpl.Execute(w, s.newPageData(msgid, s.es.GetMessage(msgid), 1)); err != nil { log.Error(err) } } func (s *ssr) searchHandler(w http.ResponseWriter, r *http.Request) { tpl, err := s.getTemplate("search") if err != nil { log.Error(err) return } q := r.URL.Query().Get("query") if q != "" { m, err := json.Marshal(q) if err != nil { log.Error(err) m = []byte("") } posts := s.es.DoSearch(string(m)) for i := range posts { posts[i].Date = parseTime(posts[i].Date) } if err := tpl.Execute(w, s.newPageData("search", posts, 1)); err != nil { log.Error(err) } } }