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

134 lines
3.4 KiB
Go
Raw Normal View History

2023-01-16 05:24:07 +04:00
package http
import (
2023-02-05 21:11:29 +04:00
"math"
2023-01-16 05:24:07 +04:00
"net/http"
2023-02-05 21:11:29 +04:00
"strconv"
"strings"
2023-01-16 05:24:07 +04:00
"time"
"git.arav.top/Arav/dwelling-home/pkg/mindflow"
"git.arav.top/Arav/dwelling-home/pkg/servicestat"
"git.arav.top/Arav/dwelling-home/pkg/util"
2023-01-16 05:24:07 +04:00
"git.arav.top/Arav/dwelling-home/web"
2023-02-05 21:10:52 +04:00
gbsqlite "git.arav.top/Arav/justguestbook/database/sqlite"
2023-01-16 05:24:07 +04:00
)
type Handlers struct {
2023-02-05 21:10:52 +04:00
guestbookDB *gbsqlite.SQLiteDatabase
2023-01-16 05:24:07 +04:00
captchaExpire time.Duration
guestbookOwner string
guestbookPageSize int64
2023-01-16 05:24:07 +04:00
}
2023-02-05 21:10:52 +04:00
func NewHandlers(captchaExpire time.Duration, dbPath, gbOwner string, gbPageSize int64) *Handlers {
gbdb, err := gbsqlite.New(dbPath + "/guestbook.sqlite")
if err != nil {
panic(err)
}
2023-01-16 05:24:07 +04:00
return &Handlers{
2023-02-05 21:10:52 +04:00
guestbookDB: gbdb,
2023-01-16 05:24:07 +04:00
captchaExpire: captchaExpire,
guestbookOwner: gbOwner,
guestbookPageSize: gbPageSize}
}
2023-02-05 21:10:52 +04:00
func (h *Handlers) CloseDB() {
h.guestbookDB.Close()
}
2023-01-16 05:24:07 +04:00
func (h *Handlers) AssetsFS() http.FileSystem {
return web.Assets()
}
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 := []mindflow.Post{
{Category: "Kek", Date: time.Now(), Title: "LMAO", Body: "Testing!"},
}
web.Mindflow("/ Mindflow", 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)
}
2023-02-05 21:11:29 +04:00
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
}
entriesCount, _ := h.guestbookDB.Count()
pageCount := int64(math.Ceil(float64(entriesCount) / float64(h.guestbookPageSize)))
web.Guestbook("/ Guestbook", h.guestbookOwner, "", pageCount, entries, r, w)
}
func (h *Handlers) RSS(w http.ResponseWriter, r *http.Request) {
posts := []mindflow.Post{
{Category: "Kek", Date: time.Now(), Title: "LMAO", Body: "Testing!"},
}
web.RSS(r.URL.Scheme+"://"+r.Host, "Arav", posts, r, w)
2023-01-16 05:24:07 +04:00
}
func (h *Handlers) Robots(w http.ResponseWriter, r *http.Request) {
data, _ := web.AssetsGetFile("robots.txt")
w.Write(data)
}
2023-02-05 05:25:20 +04:00
func (h *Handlers) NotFound(w http.ResponseWriter, r *http.Request) {
web.NotFound("Not Found", w)
}