mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
The web UI skill preview always fetched SKILL.md via api.github.com, so skills from GHE or other non-github.com hubs showed only index metadata (name, description, tags) instead of the full rendered content. FetchPreview now resolves the source platform: github.com and GHE use the host's REST Contents API, while other platforms (GitLab, Bitbucket, Azure, SSH) fall back to a shallow clone. Sources whose content can't be retrieved degrade gracefully with a clear notice in the modal. Fixes #193
86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"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")
|
|
|
|
// 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.FetchPreview(client, source, 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) || errors.Is(err, search.ErrPreviewUnsupported) {
|
|
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)
|
|
}
|