mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Addresses P1 and P2 from code review on the RWMutex commit: P1 — Stream handlers now snapshot config under RLock before streaming: - handleDiffStream: clone Targets map to prevent concurrent map panic - handleCheckStream, handleAuditStream: snapshot source/projectRoot - handleUpdateStream: snapshot source (per-item Lock already correct) P2 — All read handlers now use narrow snapshot instead of defer RUnlock: - RLock → copy needed fields to locals → RUnlock → do work with locals - Prevents slow I/O (git fetch, version check, audit scan) from blocking the auto-reload middleware's exclusive Lock on concurrent requests Other changes: - computeTargetDiff takes source parameter (no more s.cfg.Source access) - Add cloneTargets() helper for safe map copying - Add writeOpsLogTo/writeAuditLogTo for lock-free oplog writes
153 lines
3.5 KiB
Go
153 lines
3.5 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"skillshare/internal/config"
|
|
)
|
|
|
|
type hubConfigResponse struct {
|
|
Hubs []hubEntryJSON `json:"hubs"`
|
|
Default string `json:"default"`
|
|
}
|
|
|
|
type hubEntryJSON struct {
|
|
Label string `json:"label"`
|
|
URL string `json:"url"`
|
|
BuiltIn bool `json:"builtIn,omitempty"`
|
|
}
|
|
|
|
func (s *Server) handleGetHubSaved(w http.ResponseWriter, r *http.Request) {
|
|
// Snapshot config under RLock, then release before I/O.
|
|
s.mu.RLock()
|
|
hub := s.hubConfig()
|
|
s.mu.RUnlock()
|
|
resp := hubConfigResponse{
|
|
Default: hub.Default,
|
|
Hubs: make([]hubEntryJSON, 0, len(hub.Hubs)),
|
|
}
|
|
for _, h := range hub.Hubs {
|
|
resp.Hubs = append(resp.Hubs, hubEntryJSON{
|
|
Label: h.Label,
|
|
URL: h.URL,
|
|
BuiltIn: h.BuiltIn,
|
|
})
|
|
}
|
|
writeJSON(w, resp)
|
|
}
|
|
|
|
func (s *Server) handlePutHubSaved(w http.ResponseWriter, r *http.Request) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
start := time.Now()
|
|
|
|
var req hubConfigResponse
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
|
|
return
|
|
}
|
|
|
|
hubCfg := config.HubConfig{
|
|
Default: req.Default,
|
|
Hubs: make([]config.HubEntry, 0, len(req.Hubs)),
|
|
}
|
|
for _, h := range req.Hubs {
|
|
hubCfg.Hubs = append(hubCfg.Hubs, config.HubEntry{
|
|
Label: h.Label,
|
|
URL: h.URL,
|
|
BuiltIn: h.BuiltIn,
|
|
})
|
|
}
|
|
|
|
s.setHubConfig(hubCfg)
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
s.writeOpsLog("hub-save", "ok", start, map[string]any{"count": len(hubCfg.Hubs)}, "")
|
|
writeJSON(w, map[string]bool{"success": true})
|
|
}
|
|
|
|
func (s *Server) handlePostHubSaved(w http.ResponseWriter, r *http.Request) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
start := time.Now()
|
|
|
|
var req struct {
|
|
Label string `json:"label"`
|
|
URL string `json:"url"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid JSON: "+err.Error())
|
|
return
|
|
}
|
|
|
|
hubCfg := s.hubConfig()
|
|
if err := hubCfg.AddHub(config.HubEntry{Label: req.Label, URL: req.URL}); err != nil {
|
|
writeError(w, http.StatusConflict, err.Error())
|
|
return
|
|
}
|
|
|
|
s.setHubConfig(*hubCfg)
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
s.writeOpsLog("hub-add", "ok", start, map[string]any{"label": req.Label}, "")
|
|
writeJSON(w, map[string]bool{"success": true})
|
|
}
|
|
|
|
func (s *Server) handleDeleteHubSaved(w http.ResponseWriter, r *http.Request) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
start := time.Now()
|
|
|
|
label := strings.TrimSpace(r.PathValue("label"))
|
|
if label == "" {
|
|
writeError(w, http.StatusBadRequest, "label is required")
|
|
return
|
|
}
|
|
|
|
hubCfg := s.hubConfig()
|
|
if err := hubCfg.RemoveHub(label); err != nil {
|
|
writeError(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
|
|
s.setHubConfig(*hubCfg)
|
|
if err := s.saveAndReloadConfig(); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
s.writeOpsLog("hub-remove", "ok", start, map[string]any{"label": label}, "")
|
|
writeJSON(w, map[string]bool{"success": true})
|
|
}
|
|
|
|
// hubConfig returns a copy of the current hub config.
|
|
func (s *Server) hubConfig() *config.HubConfig {
|
|
if s.IsProjectMode() {
|
|
h := s.projectCfg.Hub
|
|
return &h
|
|
}
|
|
h := s.cfg.Hub
|
|
return &h
|
|
}
|
|
|
|
// setHubConfig sets the hub config on the appropriate config object.
|
|
func (s *Server) setHubConfig(h config.HubConfig) {
|
|
if s.IsProjectMode() {
|
|
s.projectCfg.Hub = h
|
|
} else {
|
|
s.cfg.Hub = h
|
|
}
|
|
}
|