1
0
dwelling-home/internal/http/web_handlers.go

191 lines
5.1 KiB
Go

package http
import (
"context"
"log"
"math"
"net/http"
"strconv"
"strings"
"time"
"git.arav.su/Arav/dwelling-home/pkg/mindflow"
"git.arav.su/Arav/dwelling-home/pkg/servicestat"
"git.arav.su/Arav/dwelling-home/web"
"git.arav.su/Arav/justcaptcha/v2/dwcaptcha"
"git.arav.su/Arav/justcaptcha/v2/inmemdb"
"git.arav.su/Arav/justguestbook"
)
type Handlers struct {
guestbookDB justguestbook.Guestbook
mindflowDB mindflow.Mindflow
captchaExpire time.Duration
owner string
guestbookPageSize int64
}
func NewHandlers(captchaExpire time.Duration, owner string, gbPageSize int64,
gdb justguestbook.Guestbook, mdb mindflow.Mindflow) *Handlers {
return &Handlers{
guestbookDB: gdb,
mindflowDB: mdb,
captchaExpire: captchaExpire,
owner: owner,
guestbookPageSize: gbPageSize}
}
func (h *Handlers) Privacy(w http.ResponseWriter, r *http.Request) {
web.Privacy(r).Render(context.Background(), w)
}
func (h *Handlers) Stuff(w http.ResponseWriter, r *http.Request) {
web.Stuff(r).Render(context.Background(), w)
}
func (h *Handlers) Mindflow(w http.ResponseWriter, r *http.Request) {
posts, err := h.mindflowDB.Posts()
if err != nil {
log.Println("Cannot load posts.", err)
Error(w, http.StatusInternalServerError, "Cannot load posts.", "", r.Referer())
return
}
categories, err := h.mindflowDB.Categories()
if err != nil {
log.Println("Cannot load categories.", err)
Error(w, http.StatusInternalServerError, "Cannot load categories.", "", r.Referer())
return
}
web.Mindflow(posts, categories, r).Render(context.Background(), w)
}
func (h *Handlers) MindflowAdmin(w http.ResponseWriter, r *http.Request) {
posts, err := h.mindflowDB.Posts()
if err != nil {
Error(w, http.StatusInternalServerError, err.Error(), "failed to load posts", r.Referer())
return
}
categories, err := h.mindflowDB.Categories()
if err != nil {
Error(w, http.StatusInternalServerError, err.Error(), "failed to load categories", r.Referer())
return
}
web.MindflowAdmin(posts, categories, r).Render(context.Background(), w)
}
func (h *Handlers) About(w http.ResponseWriter, r *http.Request) {
var lst servicestat.ServiceList = make(servicestat.ServiceList)
reimu, err := servicestat.GatherStatus("http://reimu.arav.home.arpa:14882/processes")
if err == nil {
for k, v := range reimu {
lst[k] = v
}
}
sakuya, err := servicestat.GatherStatus("http://sakuya.arav.home.arpa:14882/processes")
if err == nil {
for k, v := range sakuya {
lst[k] = v
}
}
web.About(&lst, r).Render(context.Background(), w)
}
func (h *Handlers) Article(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
artcl := web.GetArticle(name)
if artcl == nil {
Error(w, http.StatusNotFound, "", "An article \""+name+"\" doesn't exist.", r.Referer())
return
}
web.Article(artcl.Title, artcl.Description, string(artcl.Body), name, artcl.Date, r).Render(context.Background(), w)
}
func (h *Handlers) Guestbook(w http.ResponseWriter, r *http.Request) {
var page int64
var pageSize int64
pageStr := r.URL.Query().Get("p")
if pageStr != "" {
page, _ = strconv.ParseInt(pageStr, 10, 64)
} else {
page = 1
}
pageSzStr := r.URL.Query().Get("ps")
if pageSzStr != "" {
pageSize, _ = strconv.ParseInt(pageSzStr, 10, 64)
} else {
pageSize = h.guestbookPageSize
}
entries, err := h.guestbookDB.Entries(page, pageSize)
if err != nil {
log.Println("failed to load gb entries:", err)
return
}
for _, entry := range entries {
entry.Message = strings.ReplaceAll(entry.Message, "\\n", "\n")
}
entriesCount, _ := h.guestbookDB.Count()
pageCount := int64(math.Ceil(float64(entriesCount) / float64(h.guestbookPageSize)))
dwc := dwcaptcha.NewDwellingCaptcha(h.captchaExpire)
_, id := inmemdb.New(r.RemoteAddr, dwc)
web.Guestbook(string(id), h.owner, entries, pageCount, r).Render(context.Background(), w)
}
func (h *Handlers) GuestbookAdmin(w http.ResponseWriter, r *http.Request) {
entriesCount, _ := h.guestbookDB.Count()
entries, err := h.guestbookDB.Entries(1, entriesCount)
if err != nil {
Error(w, http.StatusInternalServerError, err.Error(), "cannot load gb", r.Referer())
return
}
for _, entry := range entries {
entry.Message = strings.ReplaceAll(entry.Message, "\\n", "\n")
}
web.GuestbookAdmin(h.owner, entries, r).Render(context.Background(), w)
}
func (h *Handlers) RSS(w http.ResponseWriter, r *http.Request) {
posts, err := h.mindflowDB.Posts()
if err != nil {
return
}
scheme := r.Header.Get("Scheme")
if scheme == "" {
scheme = "http"
}
web.RSS(scheme+"://"+r.Host, h.owner, posts, r).Render(context.Background(), w)
}
func ServeAsset(path string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
data, _ := web.AssetsGetFile(path)
w.Write(data)
}
}
func Error(w http.ResponseWriter, code int, reason, message, referer string) {
w.WriteHeader(code)
web.ErrorXXX(code, reason, message, referer).Render(context.Background(), w)
}
func cleanupNewlines(text string) (out string) {
out = strings.ReplaceAll(text, "\r\n", "\n")
out = strings.ReplaceAll(out, "\n\r", "\n")
return strings.ReplaceAll(out, "\r", "\n")
}