Files
skillshare/internal/server/handler_log.go
T
Willie 5a65993304 feat: add operation log — JSONL-based persistent audit trail for CLI, API, and web UI
Add oplog package (internal/oplog) that records every mutating command
(install, uninstall, sync, push, pull, collect, backup, restore, update,
audit) as a JSONL entry with timestamp, status, duration, and args.

- CLI: `skillshare log` with --audit, --tail, --clear, --project flags
- API: GET/DELETE /api/log endpoints with type and limit params
- Web UI: LogPage with ops/audit tabs, table view, clear and refresh
- Instrumentation across 12 command handlers (CLI + server)
2026-02-10 02:56:59 +08:00

61 lines
1.2 KiB
Go

package server
import (
"net/http"
"strconv"
"skillshare/internal/oplog"
)
func (s *Server) handleListLog(w http.ResponseWriter, r *http.Request) {
logType := r.URL.Query().Get("type")
limitStr := r.URL.Query().Get("limit")
filename := oplog.OpsFile
if logType == "audit" {
filename = oplog.AuditFile
}
limit := 100
if n, err := strconv.Atoi(limitStr); err == nil && n > 0 {
limit = n
}
entries, err := oplog.Read(s.configPath(), filename, limit)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to read log: "+err.Error())
return
}
if entries == nil {
entries = []oplog.Entry{}
}
// Count total entries (read without limit)
all, _ := oplog.Read(s.configPath(), filename, 0)
total := len(all)
writeJSON(w, map[string]any{
"entries": entries,
"total": total,
})
}
func (s *Server) handleClearLog(w http.ResponseWriter, r *http.Request) {
s.mu.Lock()
defer s.mu.Unlock()
logType := r.URL.Query().Get("type")
filename := oplog.OpsFile
if logType == "audit" {
filename = oplog.AuditFile
}
if err := oplog.Clear(s.configPath(), filename); err != nil {
writeError(w, http.StatusInternalServerError, "failed to clear log: "+err.Error())
return
}
writeJSON(w, map[string]any{"success": true})
}