Skip to content

Commit

Permalink
added graphql tutorial support
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Mar 13, 2024
1 parent b061626 commit ebdb95d
Show file tree
Hide file tree
Showing 26 changed files with 592 additions and 50 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ package:
image:
$(REPO_ROOT)hack/scripts/image.sh

.PHONY: hello-world
hello-world:
$(REPO_ROOT)hack/scripts/hello-world.sh
.PHONY: examples
examples:
$(REPO_ROOT)hack/scripts/examples.sh

.PHONY: docs
docs:
Expand Down
3 changes: 2 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ you started.
}

func init() {
initCmd.Flags().BoolVarP(&cfg.Flags.Quickstart, "quickstart", "", false, `use defaults to setup KubeFox for quickstart guide`)
initCmd.Flags().BoolVarP(&cfg.Flags.Quickstart, "quickstart", "", false, `use defaults to setup KubeFox for quickstart tutorial`)
initCmd.Flags().BoolVarP(&cfg.Flags.GraphQL, "graphql", "", false, `use defaults to setup KubeFox for graphql tutorial`)
rootCmd.AddCommand(initCmd)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

// XXX Update this before making release. This is hardcoded to ensure that
// the correct version is shown when Fox is setup using `go install`.
const version = "v0.8.3"
const version = "v0.9.0"

type BuildInfo struct {
Version string `json:"version,omitempty"`
Expand Down
3 changes: 2 additions & 1 deletion docs/fox_init.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ fox init [flags]
### Options

```
--graphql use defaults to setup KubeFox for graphql tutorial
-h, --help help for init
--quickstart use defaults to setup KubeFox for quickstart guide
--quickstart use defaults to setup KubeFox for quickstart tutorial
```

### Options inherited from parent commands
Expand Down
1 change: 1 addition & 0 deletions efs/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

const (
HelloWorldPath = "hello-world"
GraphQLPath = "graphql"
)

// Go will not embed directories containing a go.mod file. To resolve this the
Expand Down
11 changes: 11 additions & 0 deletions efs/graphql/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2023 XigXog
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0

name: graphql
title: KubeFox GraphQL Demo
description: A simple App demonstrating the use of KubeFox with Hasura GraphQL Engine.
79 changes: 79 additions & 0 deletions efs/graphql/components/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2023 XigXog
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0

package main

import (
"embed"
"html/template"

"github.com/xigxog/kubefox/kit"
"github.com/xigxog/kubefox/kit/graphql"
)

//go:embed static/*
//go:embed templates/*
var EFS embed.FS

var (
tpl *template.Template
graphqlAdapter kit.ComponentDep
hasuraAdapter kit.ComponentDep
)

func main() {
k := kit.New()

var err error
tpl, err = template.ParseFS(EFS, "templates/*.html")
if err != nil {
k.Log().Fatal(err)
}

graphqlAdapter = k.HTTPAdapter("graphql")
hasuraAdapter = k.HTTPAdapter("hasura")

k.Static("/{{.Vars.subPath}}/hasura/static", "static", EFS)
k.Route("Path(`/{{.Vars.subPath}}/hasura/heroes`)", listHeroes)
k.Route("PathPrefix(`/{{.Vars.subPath}}/hasura`)", forwardHasura)

k.Start()
}

func listHeroes(k kit.Kontext) error {
client := graphql.New(k, graphqlAdapter)

// For additional documentation check out
// https://github.com/hasura/go-graphql-client.
var query struct {
Superhero []struct {
Name string `graphql:"superhero_name"`
RealName string `graphql:"full_name"`
Alignment struct {
Value string `graphql:"alignment"`
}
} `graphql:"superhero(order_by: {superhero_name: asc})"`
}
if err := client.Query(&query, nil); err != nil {
return err
}

return k.Resp().SendHTMLTemplate(tpl, "index.html", query)
}

func forwardHasura(k kit.Kontext) error {
req := k.Forward(hasuraAdapter)
req.RewritePath(k.PathSuffix())

resp, err := req.Send()
if err != nil {
return err
}

return k.Resp().Forward(resp)
}
Binary file not shown.
48 changes: 48 additions & 0 deletions efs/graphql/components/server/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2023 XigXog
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

html {
font-family: sans-serif;
}

h1 {
text-align: center;
}

.styled-table {
border-collapse: collapse;
margin: 25px auto;
font-size: 0.9em;
min-width: 80%;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
}

.styled-table th {
background-color: #009879;
color: white;
text-align: left;
}

.styled-table th,
.styled-table td {
padding: 12px 15px;
}

.styled-table tr {
border-bottom: 1px solid #dddddd;
}

.styled-table tr:nth-of-type(even) {
background-color: #f3f3f3;
}

.styled-table tr:last-of-type {
border-bottom: 2px solid #009879;
}
42 changes: 42 additions & 0 deletions efs/graphql/components/server/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<!--
Copyright 2023 XigXog
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
SPDX-License-Identifier: MPL-2.0
-->

<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />

<title>KubeFox Hasura Demo</title>

<link rel="stylesheet" href="./static/style.css" />
<link rel="icon" href="./static/favicon.ico" type="image/x-icon" />
</head>

<body>
<h1>🦸 Superheroes</h1>

<table class="styled-table">
<tr>
<th>Name</th>
<th>Real Name</th>
<th>Alignment</th>
</tr>
{{ range .Superhero }}
<tr>
<td>{{.Name}}</td>
<td>{{.RealName}}</td>
<td>{{.Alignment.Value}}</td>
</tr>
{{ end}}
</table>
</body>
</html>
25 changes: 25 additions & 0 deletions efs/graphql/go.mod.trim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module github.com/xigxog/kubefox/graphql

go 1.21

require github.com/xigxog/kubefox v0.6.1

require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/hasura/go-graphql-client v0.12.1 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240311173647-c811ad7063a7 // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
nhooyr.io/websocket v1.8.10 // indirect
)
48 changes: 48 additions & 0 deletions efs/graphql/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0QDGLKzqOmktBjT+Is=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM=
github.com/hasura/go-graphql-client v0.12.1 h1:tL+BCoyubkYYyaQ+tJz+oPe/pSxYwOJHwe5SSqqi6WI=
github.com/hasura/go-graphql-client v0.12.1/go.mod h1:F4N4kR6vY8amio3gEu3tjSZr8GPOXJr3zj72DKixfLE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xigxog/kubefox v0.6.1 h1:oAZYGvTMnN9GN4o1RlodVvQR45UaGku8/YREuXRi/20=
github.com/xigxog/kubefox v0.6.1/go.mod h1:eIgVSKT2xPmKVYOBDaM5P+r0cpk2Kb3rp/8eju+2vE0=
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
go.opentelemetry.io/proto/otlp v1.1.0 h1:2Di21piLrCqJ3U3eXGCTPHE9R8Nh+0uglSnOyxikMeI=
go.opentelemetry.io/proto/otlp v1.1.0/go.mod h1:GpBHCBWiqvVLDqmHZsoMM3C5ySeKTC7ej/RNTae6MdY=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
google.golang.org/genproto/googleapis/api v0.0.0-20240311173647-c811ad7063a7 h1:oqta3O3AnlWbmIE3bFnWbu4bRxZjfbWCp0cKSuZh01E=
google.golang.org/genproto/googleapis/api v0.0.0-20240311173647-c811ad7063a7/go.mod h1:VQW3tUculP/D4B+xVCo+VgSq8As6wA9ZjHl//pmk+6s=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240311173647-c811ad7063a7 h1:8EeVk1VKMD+GD/neyEHGmz7pFblqPjHoi+PGQIlLx2s=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240311173647-c811ad7063a7/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk=
google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=
nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
38 changes: 38 additions & 0 deletions efs/graphql/hack/environments/dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 XigXog
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0

---
apiVersion: kubefox.xigxog.io/v1alpha1
kind: Environment
metadata:
name: dev
spec:
releasePolicy:
type: Testing
data:
vars:
db: dev
#subPath: dev
---
apiVersion: kubefox.xigxog.io/v1alpha1
kind: VirtualEnvironment
metadata:
name: dev
spec:
environment: dev
---
apiVersion: kubefox.xigxog.io/v1alpha1
kind: VirtualEnvironment
metadata:
name: dev-john
spec:
environment: dev
data:
vars:
db: john
subPath: john
27 changes: 27 additions & 0 deletions efs/graphql/hack/environments/prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2023 XigXog
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0

---
apiVersion: kubefox.xigxog.io/v1alpha1
kind: Environment
metadata:
name: prod
spec:
releasePolicy:
type: Stable
data:
vars:
db: prod
subPath: prod
---
apiVersion: kubefox.xigxog.io/v1alpha1
kind: VirtualEnvironment
metadata:
name: prod
spec:
environment: prod
Loading

0 comments on commit ebdb95d

Please sign in to comment.