add load more command

This commit is contained in:
2026-02-12 17:52:32 +03:00
parent 4e1d1ca35f
commit a058b622e3
7 changed files with 161 additions and 33 deletions
+30 -11
View File
@@ -31,6 +31,7 @@ type memoryData struct {
MemoURL string
Tier int
ShowCount int
AllowLoadMore bool
}
type errorData struct {
@@ -38,27 +39,30 @@ type errorData struct {
}
type Handler struct {
service *memory.Service
logger *slog.Logger
mux *http.ServeMux
memosURL string // internal Memos URL (for attachment files)
publicURL string // public Memos URL (for memo links)
service *memory.Service
logger *slog.Logger
mux *http.ServeMux
memosURL string // internal Memos URL (for attachment files)
publicURL string // public Memos URL (for memo links)
allowLoadMore bool
}
func NewHandler(service *memory.Service, memosURL, publicURL string, logger *slog.Logger) *Handler {
func NewHandler(service *memory.Service, memosURL, publicURL string, allowLoadMore bool, logger *slog.Logger) *Handler {
pub := publicURL
if pub == "" {
pub = memosURL
}
h := &Handler{
service: service,
memosURL: strings.TrimRight(memosURL, "/"),
publicURL: strings.TrimRight(pub, "/"),
logger: logger,
mux: http.NewServeMux(),
service: service,
memosURL: strings.TrimRight(memosURL, "/"),
publicURL: strings.TrimRight(pub, "/"),
allowLoadMore: allowLoadMore,
logger: logger,
mux: http.NewServeMux(),
}
h.mux.HandleFunc("GET /", h.handleMemory)
h.mux.HandleFunc("GET /health", h.handleHealth)
h.mux.HandleFunc("POST /more", h.handleLoadMore)
return h
}
@@ -116,6 +120,7 @@ func (h *Handler) handleMemory(w http.ResponseWriter, r *http.Request) {
MemoURL: memoURL,
Tier: mem.Tier,
ShowCount: mem.ShowCount,
AllowLoadMore: h.allowLoadMore,
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
@@ -129,6 +134,20 @@ func (h *Handler) handleHealth(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, "ok")
}
func (h *Handler) handleLoadMore(w http.ResponseWriter, r *http.Request) {
if !h.allowLoadMore {
http.Error(w, "load more is disabled", http.StatusForbidden)
return
}
_, err := h.service.LoadNewMemory(r.Context())
if err != nil {
h.logger.Error("failed to load new memory", "error", err)
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
var months = [...]string{
1: "января", 2: "февраля", 3: "марта",
4: "апреля", 5: "мая", 6: "июня",