forked from burntcarrot/heapview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (52 loc) · 1.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/burntcarrot/heaputil"
)
func main() {
filePath := flag.String("file", "", "Path to the heap dump file")
flag.Parse()
if *filePath == "" {
log.Fatal("Please provide the path to the heap dump file using the -file flag.")
}
file, err := os.Open(*filePath)
if err != nil {
log.Fatalf("Failed to open file: %v", err)
}
defer file.Close()
reader := bufio.NewReader(file)
graphContent, err := GenerateGraph(reader)
if err != nil {
log.Fatalf("Failed to generate graph: %v", err)
}
_, err = file.Seek(0, 0)
if err != nil {
log.Fatalf("Failed to seek to starting point: %v", err)
}
reader.Reset(file)
records, err := heaputil.ParseDump(reader)
if err != nil {
log.Fatalf("Failed to parse records: %v", err)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
html, err := GenerateHTML(records, graphContent)
if err != nil {
http.Error(w, fmt.Sprintf("Error generating HTML: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
_, err = io.WriteString(w, html)
if err != nil {
http.Error(w, fmt.Sprintf("Error writing response: %v", err), http.StatusInternalServerError)
}
})
port := ":8080"
log.Printf("Server is running on port %s\n", port)
log.Fatal(http.ListenAndServe(port, nil))
}