mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-08-31 13:17:09 +00:00
103 lines
2.1 KiB
Go
103 lines
2.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"database/sql"
|
|
"html/template"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"atcr.io/pkg/appview/db"
|
|
)
|
|
|
|
// SearchHandler handles the search page
|
|
type SearchHandler struct {
|
|
DB *sql.DB
|
|
Templates *template.Template
|
|
RegistryURL string
|
|
}
|
|
|
|
func (h *SearchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
query := r.URL.Query().Get("q")
|
|
|
|
data := struct {
|
|
PageData
|
|
SearchQuery string
|
|
}{
|
|
PageData: NewPageData(r, h.RegistryURL),
|
|
SearchQuery: query,
|
|
}
|
|
|
|
if err := h.Templates.ExecuteTemplate(w, "search", data); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
|
|
// SearchResultsHandler handles the HTMX request for search results
|
|
type SearchResultsHandler struct {
|
|
DB *sql.DB
|
|
Templates *template.Template
|
|
RegistryURL string
|
|
}
|
|
|
|
func (h *SearchResultsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
query := r.URL.Query().Get("q")
|
|
|
|
// Validate and sanitize input
|
|
query = strings.TrimSpace(query)
|
|
if query == "" {
|
|
// Return empty results if no query
|
|
data := struct {
|
|
PageData
|
|
Pushes []db.Push
|
|
HasMore bool
|
|
NextOffset int
|
|
}{
|
|
PageData: NewPageData(r, h.RegistryURL),
|
|
Pushes: []db.Push{},
|
|
HasMore: false,
|
|
}
|
|
|
|
if err := h.Templates.ExecuteTemplate(w, "push-list.html", data); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
return
|
|
}
|
|
|
|
// Limit query length to prevent abuse
|
|
if len(query) > 200 {
|
|
query = query[:200]
|
|
}
|
|
|
|
limit := 50
|
|
offset := 0
|
|
|
|
if o := r.URL.Query().Get("offset"); o != "" {
|
|
offset, _ = strconv.Atoi(o)
|
|
}
|
|
|
|
pushes, total, err := db.SearchPushes(h.DB, query, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
data := struct {
|
|
PageData
|
|
Pushes []db.Push
|
|
HasMore bool
|
|
NextOffset int
|
|
}{
|
|
PageData: NewPageData(r, h.RegistryURL),
|
|
Pushes: pushes,
|
|
HasMore: offset+limit < total,
|
|
NextOffset: offset + limit,
|
|
}
|
|
|
|
if err := h.Templates.ExecuteTemplate(w, "push-list.html", data); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|