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

189 lines
4.9 KiB
Go

package http
import (
"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/pkg/util"
"git.arav.su/Arav/dwelling-home/web"
"git.arav.su/Arav/justcaptcha/pkg/captcha/inmemdb"
"git.arav.su/Arav/justcaptcha/pkg/dwcaptcha"
"git.arav.su/Arav/justguestbook"
)
type Handlers struct {
guestbookDB justguestbook.Guestbook
mindflowDB mindflow.Mindflow
captchaExpire time.Duration
guestbookOwner string
guestbookPageSize int64
}
func NewHandlers(captchaExpire time.Duration, gbOwner string, gbPageSize int64,
gdb justguestbook.Guestbook, mdb mindflow.Mindflow) *Handlers {
return &Handlers{
guestbookDB: gdb,
mindflowDB: mdb,
captchaExpire: captchaExpire,
guestbookOwner: gbOwner,
guestbookPageSize: gbPageSize}
}
func (h *Handlers) CloseDB() {
h.guestbookDB.Close()
}
func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) {
web.Index("", w)
}
func (h *Handlers) Stuff(w http.ResponseWriter, r *http.Request) {
web.Stuff("/ Stuff", util.GetServiceByHost(r.Host, util.ServiceGit),
util.GetServiceByHost(r.Host, util.ServiceFiles), w)
}
func (h *Handlers) Mindflow(w http.ResponseWriter, r *http.Request) {
posts, err := h.mindflowDB.GetAll()
if err != nil {
return
}
for _, entry := range posts {
entry.Body = strings.ReplaceAll(entry.Body, "\\n", "\n")
}
web.Mindflow("/ Mindflow", posts, r, w)
}
func (h *Handlers) MindflowAdmin(w http.ResponseWriter, r *http.Request) {
posts, err := h.mindflowDB.GetAll()
if err != nil {
return
}
for _, entry := range posts {
entry.Body = strings.ReplaceAll(entry.Body, "\\n", "\n")
}
web.MindflowAdmin("/ Mindflow Administration", posts, r, 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("/ About", util.GetServiceByHost(r.Host, util.ServiceFiles), lst, w)
}
func (h *Handlers) Article(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
artcl, err := web.GetArticle(name)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
web.Article(artcl.Title, artcl.Description, string(artcl.Body), artcl.Date, r.Host, name, r, 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 {
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("/ Guestbook", h.guestbookOwner, string(id), pageCount, entries, r, w)
}
func (h *Handlers) GuestbookAdmin(w http.ResponseWriter, r *http.Request) {
entriesCount, _ := h.guestbookDB.Count()
entries, err := h.guestbookDB.Entries(1, entriesCount)
for _, entry := range entries {
entry.Message = strings.ReplaceAll(entry.Message, "\\n", "\n")
}
if err != nil {
return
}
web.GuestbookAdmin("/ Guestbook Administration", h.guestbookOwner, entries, r, w)
}
func (h *Handlers) RSS(w http.ResponseWriter, r *http.Request) {
posts := []mindflow.Post{
{Category: mindflow.Category{ID: 1, Name: "Update"}, Date: time.Now(), Title: "LMAO", Body: "Testing!"},
}
web.RSS(r.URL.Scheme+"://"+r.Host, "Arav", posts, r, w)
}
func AssetsFS() http.FileSystem {
return web.Assets()
}
func RobotsTxt(w http.ResponseWriter, r *http.Request) {
data, _ := web.AssetsGetFile("robots.txt")
w.Write(data)
}
func SitemapXml(w http.ResponseWriter, r *http.Request) {
data, _ := web.AssetsGetFile("sitemap.xml")
w.Write(data)
}
/**** Errors ******************************************************************/
func ForbiddenError(err, msg string, w http.ResponseWriter) {
w.WriteHeader(http.StatusForbidden)
web.ForbiddenError("/ Forbidden", err, msg, w)
}
func NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
web.NotFound("Not Found", w)
}
func InternalError(err, msg string, w http.ResponseWriter) {
w.WriteHeader(http.StatusInternalServerError)
web.InternalError("/ Internal Error", err, msg, w)
}