|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + _ "embed" |
| 10 | + "encoding/json" |
| 11 | + "flag" |
| 12 | + "fmt" |
| 13 | + "io" |
| 14 | + "net/http" |
| 15 | + "os" |
| 16 | + "os/exec" |
| 17 | + "path/filepath" |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/cockroachdb/cockroach/pkg/cli/exit" |
| 21 | + "github.com/cockroachdb/errors" |
| 22 | + "github.com/cockroachdb/errors/oserror" |
| 23 | +) |
| 24 | + |
| 25 | +//go:embed viewer.html |
| 26 | +var viewerHTML string |
| 27 | + |
| 28 | +type FileInfo struct { |
| 29 | + Path string `json:"path"` |
| 30 | + Name string `json:"name"` |
| 31 | + TestName string `json:"testName"` |
| 32 | +} |
| 33 | + |
| 34 | +func main() { |
| 35 | + var port int |
| 36 | + flag.IntVar(&port, "port", 8080, "Port to serve on") |
| 37 | + flag.Parse() |
| 38 | + |
| 39 | + dir := flag.Arg(0) |
| 40 | + if err := mainErr(port, dir); err != nil { |
| 41 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 42 | + exit.WithCode(exit.UnspecifiedError()) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func mainErr(port int, dir string) error { |
| 47 | + if dir == "" { |
| 48 | + // Find git repo root and default to generated testdata |
| 49 | + cmd := exec.Command("git", "rev-parse", "--show-toplevel") |
| 50 | + output, err := cmd.Output() |
| 51 | + if err != nil { |
| 52 | + return errors.Errorf("failed to find git repo root: %w", err) |
| 53 | + } |
| 54 | + repoRoot := strings.TrimSpace(string(output)) |
| 55 | + dir = filepath.Join(repoRoot, "pkg/kv/kvserver/asim/tests/testdata/generated") |
| 56 | + } |
| 57 | + |
| 58 | + absDir, err := filepath.Abs(dir) |
| 59 | + if err != nil { |
| 60 | + return errors.Errorf("failed to resolve directory: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + if _, err := os.Stat(absDir); oserror.IsNotExist(err) { |
| 64 | + return errors.Errorf("directory does not exist: %s", absDir) |
| 65 | + } |
| 66 | + |
| 67 | + fmt.Printf("Serving files from: %s\n", absDir) |
| 68 | + fmt.Printf("Viewer available at: http://localhost:%d\n", port) |
| 69 | + |
| 70 | + http.HandleFunc("/", serveViewer) |
| 71 | + http.HandleFunc("/api/files", makeFileListHandler(absDir)) |
| 72 | + http.HandleFunc("/api/file/", makeFileHandler(absDir)) |
| 73 | + |
| 74 | + return http.ListenAndServe(fmt.Sprintf(":%d", port), nil) |
| 75 | +} |
| 76 | + |
| 77 | +func serveViewer(w http.ResponseWriter, r *http.Request) { |
| 78 | + w.Header().Set("Content-Type", "text/html") |
| 79 | + w.Write([]byte(viewerHTML)) |
| 80 | +} |
| 81 | + |
| 82 | +func makeFileListHandler(baseDir string) http.HandlerFunc { |
| 83 | + return func(w http.ResponseWriter, r *http.Request) { |
| 84 | + var files []FileInfo |
| 85 | + |
| 86 | + err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error { |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + if !info.IsDir() && strings.HasSuffix(path, ".json") { |
| 91 | + relPath, _ := filepath.Rel(baseDir, path) |
| 92 | + |
| 93 | + // Extract test name from file name only (ignore directories) |
| 94 | + baseName := filepath.Base(path) |
| 95 | + testName := strings.TrimSuffix(baseName, ".json") |
| 96 | + |
| 97 | + files = append(files, FileInfo{ |
| 98 | + Path: relPath, |
| 99 | + Name: baseName, |
| 100 | + TestName: testName, |
| 101 | + }) |
| 102 | + } |
| 103 | + return nil |
| 104 | + }) |
| 105 | + |
| 106 | + if err != nil { |
| 107 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + w.Header().Set("Content-Type", "application/json") |
| 112 | + w.Header().Set("Access-Control-Allow-Origin", "*") |
| 113 | + json.NewEncoder(w).Encode(files) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func makeFileHandler(baseDir string) http.HandlerFunc { |
| 118 | + return func(w http.ResponseWriter, r *http.Request) { |
| 119 | + // Extract file path from URL |
| 120 | + filePath := strings.TrimPrefix(r.URL.Path, "/api/file/") |
| 121 | + |
| 122 | + // Prevent directory traversal |
| 123 | + cleanPath := filepath.Clean(filePath) |
| 124 | + if strings.Contains(cleanPath, "..") { |
| 125 | + http.Error(w, "Invalid path", http.StatusBadRequest) |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + fullPath := filepath.Join(baseDir, cleanPath) |
| 130 | + |
| 131 | + // Check if file exists and is within baseDir |
| 132 | + if !strings.HasPrefix(fullPath, baseDir) { |
| 133 | + http.Error(w, "Invalid path", http.StatusBadRequest) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + file, err := os.Open(fullPath) |
| 138 | + if err != nil { |
| 139 | + http.Error(w, "File not found", http.StatusNotFound) |
| 140 | + return |
| 141 | + } |
| 142 | + defer file.Close() |
| 143 | + |
| 144 | + w.Header().Set("Content-Type", "application/json") |
| 145 | + w.Header().Set("Access-Control-Allow-Origin", "*") |
| 146 | + io.Copy(w, file) |
| 147 | + } |
| 148 | +} |
0 commit comments