diagnostics.go 909 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package routes
  2. import (
  3. "net/http"
  4. "github.com/goccy/go-json"
  5. "github.com/soluble-ai/go-jnode"
  6. "codeberg.org/vnpower/pixivfe/v2/audit"
  7. "codeberg.org/vnpower/pixivfe/v2/utils"
  8. )
  9. func Diagnostics(w http.ResponseWriter, r *http.Request) error {
  10. return Render(w, r, Data_diagnostics{})
  11. }
  12. func ResetDiagnosticsData(w http.ResponseWriter, r *http.Request) {
  13. audit.RecordedSpans = audit.RecordedSpans[:0]
  14. utils.RedirectToWhenceYouCame(w, r)
  15. }
  16. func DiagnosticsData(w http.ResponseWriter, _ *http.Request) error {
  17. data := jnode.NewArrayNode()
  18. for _, span := range audit.RecordedSpans {
  19. bytes, err := json.Marshal(span)
  20. if err != nil {
  21. return err
  22. }
  23. obj, err := jnode.FromJSON(bytes)
  24. if err != nil {
  25. return err
  26. }
  27. obj.Put("LogLine", span.LogLine())
  28. data.Append(obj)
  29. }
  30. w.Header().Set("content-type", "application/json")
  31. w.WriteHeader(200)
  32. return json.NewEncoder(w).Encode(data)
  33. }