Skip to content

Commit

Permalink
Merge pull request #3212 from hashicorp/f/data-api-v2-output-routes
Browse files Browse the repository at this point in the history
`tools/data-api`: updating the list of home page routes to be dynamic
  • Loading branch information
tombuildsstuff authored Oct 20, 2023
2 parents b9f52a3 + 9951a30 commit c768ac8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 43 deletions.
117 changes: 75 additions & 42 deletions tools/data-api/internal/endpoints/home_page.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package endpoints

import (
"fmt"
"github.com/go-chi/chi/v5"
"html/template"
"log"
"net/http"
"sort"
"strings"
)

const homePageTemplate = `
Expand All @@ -15,52 +19,81 @@ const homePageTemplate = `
<h1>Pandora Data API</h1>
<h3>Routes</h3>
<ul>
<li>
<a href="/v1/resource-manager/commonTypes">
<strong>GET</strong>
<code>/v1/resource-manager/commonTypes</code>
</a>
</li>
<li>
<a href="/v1/resource-manager/services">
<strong>GET</strong>
<code>/v1/resource-manager/services</code>
</a>
</li>
<li>
<strong>GET</strong>
<code>/v1/resource-manager/services/{serviceName}</code>
</li>
<li>
<strong>GET</strong>
<code>/v1/resource-manager/services/{serviceName}/{serviceApiVersion}</code>
</li>
<li>
<strong>GET</strong>
<code>/v1/resource-manager/services/{serviceName}/{serviceApiVersion}</code>
</li>
<li>
<strong>GET</strong>
<code>/v1/resource-manager/services/{serviceName}/terraform</code>
</li>
<li>
<strong>GET</strong>
<code>/v1/resource-manager/services/{serviceName}/{resourceName}/operations</code>
</li>
<li>
<strong>GET</strong>
<code>/v1/resource-manager/services/{serviceName}/{resourceName}/schema</code>
</li>
{{range . }}
{{if .Linkable }}
<li>
<a href="{{.Uri}}">
<code>{{.Uri}}</code>
</a>
</li>
{{ else }}
<li>
<code>{{.Uri}}</code>
</li>
{{end}}
{{end}}
</ul>
</body>
</html>
`

func HomePage(w http.ResponseWriter, r *http.Request) {
templ, err := template.New("home-page").Parse(homePageTemplate)
if err != nil {
log.Printf("[ERROR] Serving Home Page: %+v", err)
w.WriteHeader(http.StatusInternalServerError)
func HomePage(router chi.Router) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
routes := routerToListOfRoutes(router.Routes())
links := homePageLinksFromRoutes(routes)

templ, err := template.New("home-page").Parse(homePageTemplate)
if err != nil {
log.Printf("[ERROR] Serving Home Page: %+v", err)
w.WriteHeader(http.StatusInternalServerError)
}
templ.Execute(w, links)
}
}

type homePageLink struct {
Uri string
Linkable bool
}

func homePageLinksFromRoutes(input []string) []homePageLink {
// ensure the ordering is consistent
sort.Strings(input)

output := make([]homePageLink, 0)

for _, item := range input {
containsTemplatedParameter := false
if strings.Contains(item, "{") {
containsTemplatedParameter = true
}
if !containsTemplatedParameter && strings.Contains(item, "}") {
containsTemplatedParameter = true
}
output = append(output, homePageLink{
Uri: item,
Linkable: !containsTemplatedParameter,
})
}
templ.Execute(w, nil)

return output
}

func routerToListOfRoutes(input []chi.Route) []string {
output := make([]string, 0)

for _, route := range input {
if route.SubRoutes != nil && len(route.SubRoutes.Routes()) > 0 {
prefix := strings.TrimSuffix(route.Pattern, "/*")
subRoutes := routerToListOfRoutes(route.SubRoutes.Routes())
for _, subRoute := range subRoutes {
output = append(output, fmt.Sprintf("%s%s", prefix, subRoute))
}
continue
}

output = append(output, strings.TrimSuffix(route.Pattern, "/"))
}

return output
}
2 changes: 1 addition & 1 deletion tools/data-api/internal/endpoints/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ func Router(directory string, serviceNames *[]string) func(chi.Router) {
serviceRepo := repositories.NewServicesRepository(directory, opts.ServiceType, serviceNames)
v1.Router(r, opts, serviceRepo)
})
router.Get("/", HomePage)
router.Get("/", HomePage(router))
}
}

0 comments on commit c768ac8

Please sign in to comment.