Skip to content

Commit 2dc021e

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

6 files changed

Lines changed: 451 additions & 0 deletions

File tree

cmd/explorer.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
port, err := cmd.Flags().GetString("port")
24+
if err != nil {
25+
return err
26+
}
27+
return server(port)
28+
},
29+
}
30+
31+
func init() {
32+
explorerCmd.Flags().String("port", "8091", "Branch name to deploy")
33+
}
34+
35+
func server(port string) error {
36+
r := mux.NewRouter()
37+
r.HandleFunc("/", serveExplorer)
38+
r.HandleFunc("/query", runQuery).Methods("POST")
39+
40+
fmt.Printf("Open http://localhost:%s in your browser to start using the explorer", port)
41+
err := http.ListenAndServe(fmt.Sprintf(":%s", port), r)
42+
if err != nil {
43+
return err
44+
}
45+
return nil
46+
}
47+
48+
func runQuery(w http.ResponseWriter, r *http.Request) {
49+
err := r.ParseForm()
50+
result := ""
51+
if err != nil {
52+
result = err.Error()
53+
}
54+
raw := r.FormValue("raw")
55+
if raw != "" {
56+
validateToken(lagoonCLIConfig.Current)
57+
current := lagoonCLIConfig.Current
58+
token := lagoonCLIConfig.Lagoons[current].Token
59+
lc := lclient.New(
60+
lagoonCLIConfig.Lagoons[current].GraphQL,
61+
"",
62+
"",
63+
&token,
64+
false)
65+
rawResp, err := lc.ProcessRaw(context.TODO(), raw, nil)
66+
if err != nil {
67+
result = err.Error()
68+
} else {
69+
resp, err := json.MarshalIndent(rawResp, "", "\t")
70+
if err != nil {
71+
result = err.Error()
72+
} else {
73+
result = string(resp)
74+
}
75+
}
76+
}
77+
_, _ = w.Write([]byte(result))
78+
}
79+
80+
func serveExplorer(w http.ResponseWriter, r *http.Request) {
81+
tmpl, _ := template.New("").ParseFS(explorer.Explorer, "templates/base.html")
82+
_ = tmpl.ExecuteTemplate(w, "base", nil)
83+
}

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: 358 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)