Skip to content

Commit d313679

Browse files
committed
feat: add basic graphql ui interface for local use
1 parent b9cc079 commit d313679

6 files changed

Lines changed: 399 additions & 0 deletions

File tree

cmd/explorer.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"html/template"
8+
"net/http"
9+
10+
"github.com/gorilla/mux"
11+
"github.com/spf13/cobra"
12+
"github.com/uselagoon/lagoon-cli/internal/explorer"
13+
lclient "github.com/uselagoon/machinery/api/lagoon/client"
14+
)
15+
16+
var explorerCmd = &cobra.Command{
17+
Use: "explorer",
18+
Short: "Open GraphQL Explorer",
19+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
20+
validateToken(lagoonCLIConfig.Current) // get a new token if the current one is invalid
21+
},
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
return server()
24+
},
25+
}
26+
27+
func server() error {
28+
r := mux.NewRouter()
29+
r.HandleFunc("/", serveExplorer)
30+
r.HandleFunc("/query", runQuery).Methods("POST")
31+
32+
fmt.Println("Open http://localhost:8091 in your browser to start using the explorer")
33+
err := http.ListenAndServe(":8091", r)
34+
if err != nil {
35+
return err
36+
}
37+
return nil
38+
}
39+
40+
func runQuery(w http.ResponseWriter, r *http.Request) {
41+
err := r.ParseForm()
42+
result := ""
43+
if err != nil {
44+
result = err.Error()
45+
}
46+
raw := r.FormValue("raw")
47+
if raw != "" {
48+
validateToken(lagoonCLIConfig.Current)
49+
current := lagoonCLIConfig.Current
50+
token := lagoonCLIConfig.Lagoons[current].Token
51+
lc := lclient.New(
52+
lagoonCLIConfig.Lagoons[current].GraphQL,
53+
"",
54+
"",
55+
&token,
56+
false)
57+
rawResp, err := lc.ProcessRaw(context.TODO(), raw, nil)
58+
if err != nil {
59+
result = err.Error()
60+
} else {
61+
resp, err := json.MarshalIndent(rawResp, "", "\t")
62+
if err != nil {
63+
result = err.Error()
64+
} else {
65+
result = string(resp)
66+
}
67+
}
68+
}
69+
_, _ = w.Write([]byte(result))
70+
}
71+
72+
func serveExplorer(w http.ResponseWriter, r *http.Request) {
73+
tmpl, _ := template.New("").ParseFS(explorer.Explorer, "templates/base.html")
74+
_ = tmpl.ExecuteTemplate(w, "base", nil)
75+
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e
205205
rootCmd.AddCommand(rawCmd)
206206
rootCmd.AddCommand(resetPasswordCmd)
207207
rootCmd.AddCommand(logsCmd)
208+
rootCmd.AddCommand(explorerCmd)
208209
}
209210

210211
// version/build information command

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/Masterminds/semver/v3 v3.4.0
99
github.com/golang-jwt/jwt v3.2.2+incompatible
1010
github.com/google/go-github/v66 v66.0.0
11+
github.com/gorilla/mux v1.8.1
1112
github.com/guregu/null v4.0.0+incompatible
1213
github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc
1314
github.com/jedib0t/go-pretty/v6 v6.6.8

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD
2626
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
2727
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2828
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
29+
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
30+
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
2931
github.com/guregu/null v4.0.0+incompatible h1:4zw0ckM7ECd6FNNddc3Fu4aty9nTlpkkzH7dPn4/4Gw=
3032
github.com/guregu/null v4.0.0+incompatible/go.mod h1:ePGpQaN9cw0tj45IR5E5ehMvsFlLlQZAkkOXZurJ3NM=
3133
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=

internal/explorer/fs.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package explorer
2+
3+
import "embed"
4+
5+
//go:embed templates/base.html
6+
var Explorer embed.FS

internal/explorer/templates/base.html

Lines changed: 314 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)