2023-01-16 05:24:07 +04:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2024-06-13 04:49:55 +04:00
|
|
|
"context"
|
2023-05-22 21:32:09 +04:00
|
|
|
"log"
|
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"
|
2023-02-05 00:52:14 +04:00
|
|
|
"strings"
|
2023-01-16 05:24:07 +04:00
|
|
|
"time"
|
|
|
|
|
2023-03-11 22:45:22 +04:00
|
|
|
"git.arav.su/Arav/dwelling-home/pkg/mindflow"
|
|
|
|
"git.arav.su/Arav/dwelling-home/pkg/servicestat"
|
|
|
|
"git.arav.su/Arav/dwelling-home/web"
|
2023-08-12 22:58:54 +04:00
|
|
|
"git.arav.su/Arav/justcaptcha/v2/dwcaptcha"
|
|
|
|
"git.arav.su/Arav/justcaptcha/v2/inmemdb"
|
2023-05-22 01:14:25 +04:00
|
|
|
"git.arav.su/Arav/justguestbook"
|
2023-01-16 05:24:07 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handlers struct {
|
2023-05-22 01:14:25 +04:00
|
|
|
guestbookDB justguestbook.Guestbook
|
2023-05-19 21:54:46 +04:00
|
|
|
mindflowDB mindflow.Mindflow
|
2023-01-16 05:24:07 +04:00
|
|
|
captchaExpire time.Duration
|
2024-04-20 04:51:11 +04:00
|
|
|
owner string
|
2023-02-05 21:09:44 +04:00
|
|
|
guestbookPageSize int64
|
2023-01-16 05:24:07 +04:00
|
|
|
}
|
|
|
|
|
2024-04-20 04:51:11 +04:00
|
|
|
func NewHandlers(captchaExpire time.Duration, owner string, gbPageSize int64,
|
2023-05-22 01:14:25 +04:00
|
|
|
gdb justguestbook.Guestbook, mdb mindflow.Mindflow) *Handlers {
|
2023-01-16 05:24:07 +04:00
|
|
|
return &Handlers{
|
2023-05-19 21:54:46 +04:00
|
|
|
guestbookDB: gdb,
|
|
|
|
mindflowDB: mdb,
|
2023-01-16 05:24:07 +04:00
|
|
|
captchaExpire: captchaExpire,
|
2024-04-20 04:51:11 +04:00
|
|
|
owner: owner,
|
2023-01-16 05:24:07 +04:00
|
|
|
guestbookPageSize: gbPageSize}
|
|
|
|
}
|
|
|
|
|
2024-07-04 03:56:57 +04:00
|
|
|
func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) {
|
|
|
|
web.Index(r).Render(context.Background(), w)
|
|
|
|
}
|
|
|
|
|
2023-06-12 23:05:06 +04:00
|
|
|
func (h *Handlers) Privacy(w http.ResponseWriter, r *http.Request) {
|
2024-06-19 01:08:05 +04:00
|
|
|
web.Privacy(r).Render(context.Background(), w)
|
2023-06-12 23:05:06 +04:00
|
|
|
}
|
|
|
|
|
2023-02-05 00:52:14 +04:00
|
|
|
func (h *Handlers) Stuff(w http.ResponseWriter, r *http.Request) {
|
2024-06-19 01:08:05 +04:00
|
|
|
web.Stuff(r).Render(context.Background(), w)
|
2023-02-05 00:52:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) Mindflow(w http.ResponseWriter, r *http.Request) {
|
2023-05-22 05:21:00 +04:00
|
|
|
posts, err := h.mindflowDB.Posts()
|
2023-05-19 21:54:46 +04:00
|
|
|
if err != nil {
|
2024-06-19 01:08:05 +04:00
|
|
|
log.Println("Cannot load posts.", err)
|
2024-07-04 03:56:57 +04:00
|
|
|
Error(w, http.StatusInternalServerError, "Cannot load posts.", "", r)
|
2023-05-19 21:54:46 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:32:09 +04:00
|
|
|
categories, err := h.mindflowDB.Categories()
|
|
|
|
if err != nil {
|
2024-06-19 01:08:05 +04:00
|
|
|
log.Println("Cannot load categories.", err)
|
2024-07-04 03:56:57 +04:00
|
|
|
Error(w, http.StatusInternalServerError, "Cannot load categories.", "", r)
|
2023-05-22 21:32:09 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-19 01:08:05 +04:00
|
|
|
web.Mindflow(posts, categories, r).Render(context.Background(), w)
|
2023-02-05 00:52:14 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 03:17:58 +04:00
|
|
|
func (h *Handlers) MindflowAdmin(w http.ResponseWriter, r *http.Request) {
|
2023-05-22 05:21:00 +04:00
|
|
|
posts, err := h.mindflowDB.Posts()
|
2023-05-19 21:54:46 +04:00
|
|
|
if err != nil {
|
2024-07-04 03:56:57 +04:00
|
|
|
Error(w, http.StatusInternalServerError, err.Error(), "failed to load posts", r)
|
2023-05-19 21:54:46 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:32:09 +04:00
|
|
|
categories, err := h.mindflowDB.Categories()
|
|
|
|
if err != nil {
|
2024-07-04 03:56:57 +04:00
|
|
|
Error(w, http.StatusInternalServerError, err.Error(), "failed to load categories", r)
|
2023-05-22 21:32:09 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-19 01:08:05 +04:00
|
|
|
web.MindflowAdmin(posts, categories, r).Render(context.Background(), w)
|
2023-05-10 03:17:58 +04:00
|
|
|
}
|
|
|
|
|
2023-02-05 00:52:14 +04:00
|
|
|
func (h *Handlers) About(w http.ResponseWriter, r *http.Request) {
|
2023-02-05 05:13:58 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 04:37:50 +04:00
|
|
|
web.About(&lst, r).Render(context.Background(), w)
|
2023-02-05 00:52:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) Article(w http.ResponseWriter, r *http.Request) {
|
2023-02-05 04:41:11 +04:00
|
|
|
name := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
|
2023-09-24 20:00:49 +04:00
|
|
|
artcl := web.GetArticle(name)
|
|
|
|
if artcl == nil {
|
2024-07-04 03:56:57 +04:00
|
|
|
Error(w, http.StatusNotFound, "", "An article \""+name+"\" doesn't exist.", r)
|
2023-02-05 00:52:14 +04:00
|
|
|
return
|
|
|
|
}
|
2023-02-05 04:41:11 +04:00
|
|
|
|
2024-06-19 01:08:05 +04:00
|
|
|
web.Article(artcl.Title, artcl.Description, string(artcl.Body), name, artcl.Date, r).Render(context.Background(), w)
|
2023-02-05 00:52:14 +04:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-06-04 18:13:29 +04:00
|
|
|
log.Println("failed to load gb entries:", err)
|
2023-02-05 21:11:29 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-11 23:34:08 +04:00
|
|
|
for _, entry := range entries {
|
|
|
|
entry.Message = strings.ReplaceAll(entry.Message, "\\n", "\n")
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:11:29 +04:00
|
|
|
entriesCount, _ := h.guestbookDB.Count()
|
|
|
|
pageCount := int64(math.Ceil(float64(entriesCount) / float64(h.guestbookPageSize)))
|
|
|
|
|
2023-02-05 23:23:55 +04:00
|
|
|
dwc := dwcaptcha.NewDwellingCaptcha(h.captchaExpire)
|
|
|
|
_, id := inmemdb.New(r.RemoteAddr, dwc)
|
|
|
|
|
2024-06-30 00:42:40 +04:00
|
|
|
web.Guestbook(string(id), h.owner, entries, pageCount, page, r).Render(context.Background(), w)
|
2023-02-05 21:11:29 +04:00
|
|
|
}
|
|
|
|
|
2023-02-06 01:59:50 +04:00
|
|
|
func (h *Handlers) GuestbookAdmin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
entriesCount, _ := h.guestbookDB.Count()
|
|
|
|
entries, err := h.guestbookDB.Entries(1, entriesCount)
|
|
|
|
if err != nil {
|
2024-07-04 03:56:57 +04:00
|
|
|
Error(w, http.StatusInternalServerError, err.Error(), "cannot load gb", r)
|
2023-02-06 01:59:50 +04:00
|
|
|
return
|
|
|
|
}
|
2023-06-04 18:08:21 +04:00
|
|
|
for _, entry := range entries {
|
|
|
|
entry.Message = strings.ReplaceAll(entry.Message, "\\n", "\n")
|
|
|
|
}
|
2023-02-06 01:59:50 +04:00
|
|
|
|
2024-06-19 01:08:05 +04:00
|
|
|
web.GuestbookAdmin(h.owner, entries, r).Render(context.Background(), w)
|
2023-02-06 01:59:50 +04:00
|
|
|
}
|
|
|
|
|
2023-02-05 00:52:14 +04:00
|
|
|
func (h *Handlers) RSS(w http.ResponseWriter, r *http.Request) {
|
2023-05-22 05:21:00 +04:00
|
|
|
posts, err := h.mindflowDB.Posts()
|
2023-05-22 03:25:14 +04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-04-20 04:52:35 +04:00
|
|
|
|
|
|
|
scheme := r.Header.Get("Scheme")
|
|
|
|
if scheme == "" {
|
2023-09-23 19:57:53 +04:00
|
|
|
scheme = "http"
|
|
|
|
}
|
2023-05-22 03:25:14 +04:00
|
|
|
|
2024-06-13 04:49:55 +04:00
|
|
|
web.RSS(scheme+"://"+r.Host, h.owner, posts, r).Render(context.Background(), w)
|
2023-01-16 05:24:07 +04:00
|
|
|
}
|
|
|
|
|
2024-04-20 04:52:35 +04:00
|
|
|
func ServeAsset(path string) func(http.ResponseWriter, *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, _ := web.AssetsGetFile(path)
|
|
|
|
w.Write(data)
|
|
|
|
}
|
2023-05-10 00:16:39 +04:00
|
|
|
}
|
|
|
|
|
2024-07-04 03:56:57 +04:00
|
|
|
func Error(w http.ResponseWriter, code int, reason, message string, r *http.Request) {
|
2023-08-13 02:17:47 +04:00
|
|
|
w.WriteHeader(code)
|
2024-07-04 03:56:57 +04:00
|
|
|
web.ErrorXXX(code, reason, message, r).Render(context.Background(), w)
|
2023-02-05 23:23:55 +04:00
|
|
|
}
|
2023-05-28 04:52:09 +04:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|