mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
- Add ErrSkillNotFound sentinel error in search/preview.go so callers can distinguish 404 from other errors with errors.Is instead of brittle string matching - Cap previewCache at 200 entries with TTL purge + full reset on overflow to prevent unbounded memory growth - Make Card and SearchResultsTable rows keyboard-accessible (role=button, tabIndex=0, Enter/Space handlers) - Refactor SkillPreviewModal to accept a single SearchResult prop instead of 6 individual fields — reduces prop drilling and keeps the modal in sync with the result shape - Fix search_online_test: add --list flag so output is not gated behind interactive TUI
104 lines
2.3 KiB
Go
104 lines
2.3 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
ghclient "skillshare/internal/github"
|
|
"skillshare/internal/search"
|
|
)
|
|
|
|
var (
|
|
previewCache sync.Map
|
|
previewCacheTTL = 5 * time.Minute
|
|
previewCacheMax = 200
|
|
)
|
|
|
|
type previewCacheEntry struct {
|
|
data *search.SkillPreview
|
|
expiresAt time.Time
|
|
}
|
|
|
|
func (s *Server) handlePreview(w http.ResponseWriter, r *http.Request) {
|
|
source := r.URL.Query().Get("source")
|
|
if source == "" {
|
|
writeError(w, http.StatusBadRequest, "missing required parameter: source")
|
|
return
|
|
}
|
|
branch := r.URL.Query().Get("branch")
|
|
|
|
// Parse owner/repo/path from source string
|
|
parts := strings.SplitN(source, "/", 3)
|
|
if len(parts) < 2 {
|
|
writeError(w, http.StatusBadRequest, "invalid source format: expected owner/repo[/path]")
|
|
return
|
|
}
|
|
owner := parts[0]
|
|
repo := parts[1]
|
|
if owner == "" || repo == "" {
|
|
writeError(w, http.StatusBadRequest, "invalid source: owner and repo must not be empty")
|
|
return
|
|
}
|
|
path := ""
|
|
if len(parts) == 3 {
|
|
path = parts[2]
|
|
}
|
|
|
|
// Check cache (include branch in key to avoid cross-branch collisions)
|
|
cacheKey := source + "@" + branch
|
|
if entry, ok := previewCache.Load(cacheKey); ok {
|
|
ce := entry.(*previewCacheEntry)
|
|
if time.Now().Before(ce.expiresAt) {
|
|
writeJSON(w, ce.data)
|
|
return
|
|
}
|
|
previewCache.Delete(cacheKey)
|
|
}
|
|
|
|
client := ghclient.NewClient()
|
|
preview, err := search.FetchSkillContent(client, owner, repo, path, branch)
|
|
if err != nil {
|
|
var rlErr *ghclient.RateLimitError
|
|
if errors.As(err, &rlErr) {
|
|
w.Header().Set("Retry-After", rlErr.Reset)
|
|
writeError(w, http.StatusTooManyRequests, err.Error())
|
|
return
|
|
}
|
|
if errors.Is(err, search.ErrSkillNotFound) {
|
|
writeError(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
// Evict expired entries and enforce max size
|
|
now := time.Now()
|
|
count := 0
|
|
previewCache.Range(func(k, v any) bool {
|
|
count++
|
|
if now.After(v.(*previewCacheEntry).expiresAt) {
|
|
previewCache.Delete(k)
|
|
count--
|
|
}
|
|
return true
|
|
})
|
|
if count >= previewCacheMax {
|
|
// Over limit after purge — drop all (simple reset)
|
|
previewCache.Range(func(k, _ any) bool {
|
|
previewCache.Delete(k)
|
|
return true
|
|
})
|
|
}
|
|
|
|
previewCache.Store(cacheKey, &previewCacheEntry{
|
|
data: preview,
|
|
expiresAt: now.Add(previewCacheTTL),
|
|
})
|
|
|
|
writeJSON(w, preview)
|
|
}
|