Skip to content

Commit 4e0db9b

Browse files
committed
improve html route render to only show relevant info
1 parent 72b4ee3 commit 4e0db9b

1 file changed

Lines changed: 38 additions & 11 deletions

File tree

pkg/teapot/routes_helpers.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ func NewListRoutesHandler(router *Router, filter RouteFilter) http.HandlerFunc {
6767

6868
// Default to HTML
6969
w.Header().Set("Content-Type", "text/html; charset=utf-8")
70+
71+
// Only show Query/Headers columns when at least one route uses them
72+
hasQuery := false
73+
hasHeaders := false
74+
for _, rt := range routes {
75+
if len(rt.QueryParams) > 0 {
76+
hasQuery = true
77+
}
78+
if len(rt.HeaderParams) > 0 {
79+
hasHeaders = true
80+
}
81+
if hasQuery && hasHeaders {
82+
break
83+
}
84+
}
85+
7086
_, _ = fmt.Fprintf(w, `<!DOCTYPE html>
7187
<html>
7288
<head>
@@ -102,14 +118,21 @@ func NewListRoutesHandler(router *Router, filter RouteFilter) http.HandlerFunc {
102118
<tr>
103119
<th>Method</th>
104120
<th>Pattern</th>
105-
<th>Query</th>
106-
<th>Headers</th>
107-
<th>Name</th>
121+
`, len(routes))
122+
if hasQuery {
123+
_, _ = fmt.Fprint(w, ` <th>Query</th>
124+
`)
125+
}
126+
if hasHeaders {
127+
_, _ = fmt.Fprint(w, ` <th>Headers</th>
128+
`)
129+
}
130+
_, _ = fmt.Fprint(w, ` <th>Name</th>
108131
<th>Action</th>
109132
</tr>
110133
</thead>
111134
<tbody>
112-
`, len(routes))
135+
`)
113136

114137
for _, route := range routes {
115138
methodClass := strings.ToLower(route.Method)
@@ -121,18 +144,22 @@ func NewListRoutesHandler(router *Router, filter RouteFilter) http.HandlerFunc {
121144
if action == "" {
122145
action = "-"
123146
}
124-
query := formatQueryParams(route.QueryParams)
125-
headers := formatHeaderParams(route.HeaderParams)
126-
127147
_, _ = fmt.Fprintf(w, ` <tr>
128148
<td class="method %s">%s</td>
129149
<td class="pattern">%s</td>
130-
<td class="query">%s</td>
131-
<td class="query">%s</td>
132-
<td class="name">%s</td>
150+
`, methodClass, route.Method, route.Pattern)
151+
if hasQuery {
152+
_, _ = fmt.Fprintf(w, ` <td class="query">%s</td>
153+
`, formatQueryParams(route.QueryParams))
154+
}
155+
if hasHeaders {
156+
_, _ = fmt.Fprintf(w, ` <td class="query">%s</td>
157+
`, formatHeaderParams(route.HeaderParams))
158+
}
159+
_, _ = fmt.Fprintf(w, ` <td class="name">%s</td>
133160
<td class="action">%s</td>
134161
</tr>
135-
`, methodClass, route.Method, route.Pattern, query, headers, name, action)
162+
`, name, action)
136163
}
137164

138165
_, _ = fmt.Fprintf(w, ` </tbody>

0 commit comments

Comments
 (0)