zotregistry.io/zot@v1.4.4-0.20231124084042-02a8ed785457/pkg/debug/gqlplayground/gqlplayground.go (about)

     1  //go:build debug
     2  // +build debug
     3  
     4  package debug
     5  
     6  import (
     7  	"embed"
     8  	"html/template"
     9  	"net/http"
    10  
    11  	"github.com/gorilla/mux"
    12  
    13  	"zotregistry.io/zot/pkg/api/constants"
    14  	debugCst "zotregistry.io/zot/pkg/debug/constants"
    15  	"zotregistry.io/zot/pkg/log"
    16  	"zotregistry.io/zot/pkg/storage"
    17  )
    18  
    19  //go:embed index.html.tmpl
    20  var playgroundHTML embed.FS
    21  
    22  // SetupGQLPlaygroundRoutes ...
    23  func SetupGQLPlaygroundRoutes(router *mux.Router,
    24  	storeController storage.StoreController, log log.Logger,
    25  ) {
    26  	log.Info().Msg("setting up graphql playground route")
    27  
    28  	templ, err := template.ParseFS(playgroundHTML, "index.html.tmpl")
    29  	if err != nil {
    30  		log.Fatal().Err(err).Msg("")
    31  	}
    32  
    33  	//nolint:lll
    34  	router.PathPrefix(debugCst.GQLPlaygroundEndpoint).HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
    35  		writer.Header().Add("Content-Type", "text/html")
    36  
    37  		proto := ""
    38  
    39  		if req.TLS == nil {
    40  			proto += "http://"
    41  		} else {
    42  			proto += "https://"
    43  		}
    44  
    45  		target := proto + req.Host + constants.FullSearchPrefix
    46  
    47  		// respond with the output of template execution
    48  		_ = templ.Execute(writer, struct {
    49  			Target string
    50  		}{Target: target})
    51  	})
    52  }