-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
222 lines (194 loc) · 5.84 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// lean displays "go mod graph" output as an interactable graph. lean
// allows you to see how your dependencies would change if you deleted one or
// more edges.
//
// While existing tools (e.g. golang.org/x/cmd/digraph) are good at answering
// questions about the actual graph, they're not so helpful with the kind of
// hypothetical questions you have when you're about to undertake a refactoring.
// lean can help you see the scope of the work ahead of you.
//
// Usage:
//
// go mod graph | lean
// go mod graph | digraph transpose | lean
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"github.com/jadekler/lean/internal"
"github.com/jadekler/lean/static"
)
// Only exists to make the module sizer pluggable, since we don't want our tests
// to cause file system reads.
type ReplaceableModuleSizer interface {
ModuleSize(string) (int64, error)
}
// Only exists to make the ast parser pluggable, since we don't want our tests
// to cause file system reads.
type ReplaceableASTParser interface {
ModuleUsagesForModule(from, to string) int
}
var (
mu sync.Mutex
originalGraph *graph
userGraph *graph
shoppingCart = make(map[string]map[string]struct{})
moduleSizer ReplaceableModuleSizer = &internal.ModuleSizer{}
astParser ReplaceableASTParser = &internal.ASTParser{}
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage: go mod graph | lean")
os.Exit(2)
}
func main() {
log.SetFlags(0)
log.SetPrefix("lean: ")
flag.Usage = usage
flag.Parse()
if flag.NArg() != 0 {
usage()
}
mu.Lock()
var err error
originalGraph, err = newGraph(os.Stdin)
if err != nil {
log.Fatal(err)
}
userGraph = originalGraph.copy()
mu.Unlock()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// / matches everything, so we need to make sure we're at the root.
if r.URL.RequestURI() != "/" {
return
}
w.Header().Add("Content-Type", "text/html")
if _, err := w.Write([]byte(static.Files["index.html"])); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
fileRequested := filepath.Base(r.URL.String())
switch {
case strings.Contains(fileRequested, ".js"):
w.Header().Add("Content-Type", "application/javascript")
case strings.Contains(fileRequested, ".css"):
w.Header().Add("Content-Type", "text/css")
case strings.Contains(fileRequested, ".html"):
w.Header().Add("Content-Type", "text/html")
}
if f, ok := static.Files[fileRequested]; ok {
if _, err := w.Write([]byte(f)); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
})
http.HandleFunc("/graph", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
if err := json.NewEncoder(w).Encode(userGraph.connected(userGraph.root)); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
http.HandleFunc("/shoppingCart", func(w http.ResponseWriter, r *http.Request) {
if err := json.NewEncoder(w).Encode(shoppingCart); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
http.HandleFunc("/reset", func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
userGraph = originalGraph.copy()
shoppingCart = make(map[string]map[string]struct{})
out := make(map[string]interface{})
out["graph"] = userGraph.edges
out["shoppingCart"] = shoppingCart
if err := json.NewEncoder(w).Encode(out); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
http.HandleFunc("/hypotheticalCut", func(w http.ResponseWriter, r *http.Request) {
var in map[string]string
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
from := in["from"]
to := in["to"]
mu.Lock()
defer mu.Unlock()
cutEdges, cutVertices, err := userGraph.hypotheticalCut(from, to)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
out := make(map[string]interface{})
out["edges"] = cutEdges
out["vertices"] = cutVertices
if err := json.NewEncoder(w).Encode(out); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
http.HandleFunc("/edge", func(w http.ResponseWriter, r *http.Request) {
var in map[string]string
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
from := in["from"]
to := in["to"]
mu.Lock()
defer mu.Unlock()
if r.Method == http.MethodDelete { // Remove edge.
if _, ok := shoppingCart[from]; !ok {
shoppingCart[from] = make(map[string]struct{})
}
shoppingCart[from][to] = struct{}{}
if err := userGraph.removeEdge(from, to); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
} else if r.Method == http.MethodPost { // Add an edge back.
if _, ok := shoppingCart[from]; ok {
if _, ok := shoppingCart[from][to]; ok {
delete(shoppingCart[from], to)
}
}
if err := userGraph.addEdge(from, to); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
m := make(map[string]interface{})
m["graph"] = userGraph.connected(userGraph.root)
m["shoppingCart"] = shoppingCart
if err := json.NewEncoder(w).Encode(m); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
log.Println("Listening on :3000")
log.Fatal(http.ListenAndServe(":3000", nil))
}