-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathscope.go
More file actions
176 lines (164 loc) · 3.93 KB
/
Copy pathscope.go
File metadata and controls
176 lines (164 loc) · 3.93 KB
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
package dohttp
import (
"github.com/samber/do/v2"
)
// ScopeTreeHTML generates an HTML page that displays the scope tree structure of the DI container.
// This function creates a visual representation of the scope hierarchy, showing the relationships
// between scopes and the services they contain.
//
// Parameters:
// - basePath: The base URL path for the web interface
// - injector: The injector instance to analyze
// - scopeID: The ID of the specific scope to highlight (optional)
//
// Returns the HTML content as a string and any error that occurred during generation.
//
// The generated page includes:
// - Visual representation of the scope hierarchy
// - Service icons indicating their type and capabilities
// - Navigation links between different views
// - Interactive scope inspection links
//
// Example:
//
// html, err := http.ScopeTreeHTML("/debug/di", injector, "")
// if err != nil {
// log.Fatal(err)
// }
// fmt.Fprint(w, html)
func ScopeTreeHTML(basePath string, injector do.Injector, scopeID string) (string, error) {
description := do.ExplainInjector(injector)
return fromTemplate(
`<!DOCTYPE html>
<html>
<head>
<title>Inspect scope tree - samber/do</title>
<style>
header {
margin-bottom: 40px;
}
.scopes {
margin-left: 10px;
padding-top: 10px;
padding-bottom: 10px;
padding-left: 30px;
border-left: 2px solid red;
}
.services {
padding-top: 5px;
padding-bottom: 5px;
}
</style>
</head>
<body>
<h1>Scope description</h1>
<small>
Menu:
<a href="{{.BasePath}}">Home</a>
-
<a href="{{.BasePath}}/scope">Scopes</a>
-
<a href="{{.BasePath}}/service">Services</a>
</small>
<header>
<p>
<b>Spec</b>:
<br><br>
😴 Lazy service
<br>
🔁 Eager service
<br>
🏭 Transient service
<br>
🔗 Service alias
<br>
🫀 Implements Healthchecker
<br>
🙅 Implements Shutdowner
</p>
</header>
{{if .Scopes}}
<ul class="scopes">
{{range .Scopes}}
<li class="scope">
{{.}}
</li>
{{end}}
</ul>
{{end}}
</body>
</html>`,
map[string]any{
keyBasePath: basePath,
keyScopes: mAp(description.DAG, func(item do.ExplainInjectorScopeOutput) string {
return scopeTreeScopeToHTML(basePath, item)
}),
},
)
}
func scopeTreeScopeToHTML(basePath string, description do.ExplainInjectorScopeOutput) string {
html, _ := fromTemplate(
`
Scope:
<a href="{{.BasePath}}/scope?scope_id={{.ScopeID}}">
{{.ScopeName}}
</a>
{{if .Services}}
<ul class="services">
{{range .Services}}
<li class="service">
{{.}}
</li>
{{end}}
</ul>
{{end}}
{{if .Scopes}}
<ul class="scopes">
{{range .Scopes}}
<li class="scope">
{{.}}
</li>
{{end}}
</ul>
{{end}}
`,
map[string]any{
keyBasePath: basePath,
keyScopeID: description.ScopeID,
keyScopeName: description.ScopeName,
keyServices: mAp(description.Services, func(item do.ExplainInjectorServiceOutput) string {
return scopeTreeServiceToHTML(basePath, description.ScopeID, item)
}),
keyScopes: mAp(description.Children, func(item do.ExplainInjectorScopeOutput) string {
return scopeTreeScopeToHTML(basePath, item)
}),
},
)
return html
}
func scopeTreeServiceToHTML(basePath string, scopeID string, description do.ExplainInjectorServiceOutput) string {
featuresIcons := ""
if description.IsHealthchecker {
featuresIcons += " 🫀"
}
if description.IsShutdowner {
featuresIcons += " 🙅"
}
html, _ := fromTemplate(
`
{{.ServiceTypeIcon}}
<a href="{{.BasePath}}/service?scope_id={{.ScopeID}}&service_name={{.ServiceName}}">
{{.ServiceName}}
</a>
{{.FeaturesIcons}}
`,
map[string]any{
keyBasePath: basePath,
keyScopeID: scopeID,
keyServiceName: description.ServiceName,
keyServiceTypeIcon: description.ServiceTypeIcon,
keyFeaturesIcons: featuresIcons,
},
)
return html
}