-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
161 lines (139 loc) · 3.9 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"context"
"embed"
_ "embed"
"encoding/json"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"time"
"github.com/carlmjohnson/versioninfo"
"github.com/grishy/gopkgview/graph"
"github.com/pkg/browser"
"github.com/urfave/cli/v2"
)
//go:embed frontend/dist/*
var frontend embed.FS
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer stop()
// We are not long-running, time is enough
log.SetFlags(log.Lmicroseconds)
// Add a handler for force exiting if we don't exit gracefully (stuck)
go func() {
<-ctx.Done()
time.Sleep(3 * time.Second)
log.Print("Force exit")
os.Exit(1)
}()
app := &cli.App{
Name: "gopkgview",
Usage: "Show dependencies of a Go package",
Version: versioninfo.Short(),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "root",
EnvVars: []string{"GO_PKGVIEW_ROOT"},
Usage: "Path to start from",
DefaultText: "./",
},
&cli.StringFlag{
Name: "gomod",
EnvVars: []string{"GO_PKGVIEW_GOMOD"},
Usage: "Path to go.mod file to detect external dependencies",
},
&cli.StringFlag{
Name: "addr",
EnvVars: []string{"GO_PKGVIEW_ADDR"},
Usage: "Address to listen on",
DefaultText: ":0",
},
&cli.UintFlag{
Name: "max-goroutines",
EnvVars: []string{"GO_PKGVIEW_MAX_GOROUTINES"},
Usage: "Maximum number of goroutines to use for parsing in parallel",
Value: 20,
},
&cli.BoolFlag{
Name: "skip-browser",
EnvVars: []string{"GO_PKGVIEW_SKIP_BROWSER"},
Usage: "Don't open browser on start",
},
},
Action: func(cCtx *cli.Context) error {
addr := cCtx.String("addr")
root := cCtx.String("root")
gomod := cCtx.String("gomod")
skipBrowser := cCtx.Bool("skip-browser")
maxGoroutines := cCtx.Uint("max-goroutines")
if gomod == "" {
gomod = filepath.Join(root, "go.mod")
}
log.Println("Creating graph...")
packageGraph, err := graph.New(gomod, root, maxGoroutines)
if err != nil {
return fmt.Errorf("failed to build graph: %w", err)
}
log.Println("Graph created")
// Generate JSON blob for the UI
frontendData := map[string]interface{}{
"nodes": packageGraph.Nodes(),
"edges": packageGraph.Edges(),
}
dataJSON, err := json.Marshal(frontendData)
if err != nil {
return fmt.Errorf("failed to marshal to JSON: %w", err)
}
// Serve the JSON blob
handler := func(data []byte) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*") // Disable CORS for simplicity of UI development
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(data); err != nil {
log.Printf("Failed to write JSON: %v", err)
}
}
}
fsys, err := fs.Sub(frontend, "frontend/dist")
if err != nil {
return fmt.Errorf("failed to get frontend subdirectory: %w", err)
}
mux := http.NewServeMux()
mux.Handle("/data", handler(dataJSON))
mux.Handle("/", http.FileServer(http.FS(fsys)))
// Start on any available port
listener, err := net.Listen("tcp", addr)
if err != nil {
return fmt.Errorf("failed to listen: %w", err)
}
defer listener.Close()
server := &http.Server{Handler: mux}
go func() {
log.Print("Starting server on ", listener.Addr())
if !skipBrowser {
webAddr := "http://" + listener.Addr().String()
log.Println("Opening browser on ", webAddr)
if err := browser.OpenURL(webAddr); err != nil {
log.Printf("Failed to open browser: %v", err)
}
}
if err := server.Serve(listener); err != http.ErrServerClosed {
log.Fatalf("Serve(): %v", err)
}
}()
<-ctx.Done()
log.Print("Shutting down...")
return server.Shutdown(ctx)
},
}
if err := app.Run(os.Args); err != nil {
fmt.Println("Error:")
fmt.Printf(" > %+v\n", err)
}
}