Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
feat: adding a rest api to get and update the gateway configuration f…
Browse files Browse the repository at this point in the history
…ile.
  • Loading branch information
chirino committed Jun 1, 2020
1 parent 297bc13 commit 0858fcf
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 23 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ require (
github.com/chirino/graphql v0.0.0-20200523142529-5ceea4b22455
github.com/fsnotify/fsnotify v1.4.7
github.com/ghodss/yaml v1.0.0
github.com/go-chi/chi v4.1.1+incompatible
github.com/go-chi/render v1.0.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-chi/chi v4.1.1+incompatible h1:MmTgB0R8Bt/jccxp+t6S/1VGIKdJw5J74CK/c9tTfA4=
github.com/go-chi/chi v4.1.1+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8=
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
Expand Down
7 changes: 1 addition & 6 deletions internal/cmd/config/add/upstream/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/chirino/graphql-gw/internal/gateway"
"github.com/chirino/graphql/schema"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -113,15 +112,11 @@ func run(cmd *cobra.Command, args []string) {
}

// Update the config

c.Upstreams[upstreamName] = gateway.UpstreamWrapper{Upstream: upstream}

configYml, err := yaml.Marshal(&c)
configFile := filepath.Join("./", config.File)
err = ioutil.WriteFile(configFile, configYml, 0644)
err = config.Store(*c)
if err != nil {
log.Fatalf(root.Verbosity, err)
}

log.Printf(`upstream added`)
}
13 changes: 13 additions & 0 deletions internal/cmd/config/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,16 @@ func Load(config *Config) error {

return nil
}

func Store(config Config) error {
configYml, err := yaml.Marshal(&config)
if err != nil {
return err
}

err = ioutil.WriteFile(File, configYml, 0644)
if err != nil {
return err
}
return nil
}
7 changes: 1 addition & 6 deletions internal/cmd/config/link/cmd.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package new

import (
"io/ioutil"
"path/filepath"
"strings"

"github.com/chirino/graphql-gw/internal/cmd/config"
"github.com/chirino/graphql-gw/internal/cmd/root"
"github.com/chirino/graphql-gw/internal/gateway"
"github.com/chirino/graphql/schema"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -111,9 +108,7 @@ func run(_ *cobra.Command, _ []string) {
c.Types = append(c.Types, *t)
}

configYml, err := yaml.Marshal(c)
configFile := filepath.Join("./", config.File)
err = ioutil.WriteFile(configFile, configYml, 0644)
err = config.Store(*c)
if err != nil {
log.Fatalf(root.Verbosity, err)
}
Expand Down
8 changes: 1 addition & 7 deletions internal/cmd/config/mount/cmd.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package new

import (
"io/ioutil"
"path/filepath"

"github.com/chirino/graphql-gw/internal/cmd/config"
"github.com/chirino/graphql-gw/internal/cmd/root"
"github.com/chirino/graphql-gw/internal/gateway"
"github.com/chirino/graphql/schema"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -92,9 +88,7 @@ func run(_ *cobra.Command, _ []string) {
c.Types = append(c.Types, *t)
}

configYml, err := yaml.Marshal(c)
configFile := filepath.Join("./", config.File)
err = ioutil.WriteFile(configFile, configYml, 0644)
err = config.Store(*c)
if err != nil {
log.Fatalf(root.Verbosity, err)
}
Expand Down
13 changes: 9 additions & 4 deletions internal/cmd/serve/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import (
"github.com/chirino/graphql-gw/internal/cmd/config"
"github.com/chirino/graphql-gw/internal/cmd/root"
"github.com/chirino/graphql-gw/internal/gateway"
"github.com/chirino/graphql-gw/internal/gateway/admin"
"github.com/chirino/graphql/graphiql"
"github.com/chirino/graphql/httpgql"
"github.com/fsnotify/fsnotify"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -169,10 +172,12 @@ func mountGatewayOnHttpServer(c *config.Config) error {
gatewayHandler.Indent = " "
}
graphqlURL := fmt.Sprintf("%s/graphql", c.Server.URL)
mux := http.NewServeMux()
mux.Handle("/graphql", gatewayHandler)
mux.Handle("/", graphiql.New(graphqlURL, true))
c.Server.Config.Handler = mux
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Mount("/admin", admin.CreateHttpHandler())
r.Handle("/graphql", gatewayHandler)
r.Handle("/", graphiql.New(graphqlURL, true))
c.Server.Config.Handler = r
config.Value.Log.Printf("GraphQL endpoint running at %s", graphqlURL)
config.Value.Log.Printf("GraphQL UI running at %s", c.Server.URL)
return nil
Expand Down

0 comments on commit 0858fcf

Please sign in to comment.