Skip to content

Commit c53e12b

Browse files
authored
fix: disable playground on production for graphql
1 parent a0a9ac8 commit c53e12b

File tree

7 files changed

+25
-6
lines changed

7 files changed

+25
-6
lines changed

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var systemConfig *system_config.Config
1313
var configFilePath = "/etc/swiftwave/config.yml"
1414

1515
func init() {
16+
rootCmd.PersistentFlags().Bool("dev", false, "Run in development mode")
1617
rootCmd.AddCommand(initCmd)
1718
rootCmd.AddCommand(setupCmd)
1819
rootCmd.AddCommand(infoCmd)

cmd/start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var startCmd = &cobra.Command{
1010
Short: "Start swiftwave service",
1111
Long: `Start swiftwave service`,
1212
Run: func(cmd *cobra.Command, args []string) {
13+
systemConfig.IsDevelopmentMode = isDevelopmentMode(cmd)
1314
swiftwave.Start(systemConfig)
1415
},
1516
}

cmd/utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os/exec"
88

99
"github.com/fatih/color"
10+
"github.com/spf13/cobra"
1011
)
1112

1213
func checkIfCommandExists(command string) bool {
@@ -87,3 +88,8 @@ func printError(message string) {
8788
func printInfo(message string) {
8889
color.Blue(InfoSymbol + " " + message)
8990
}
91+
92+
func isDevelopmentMode(cmd *cobra.Command) bool {
93+
dev, _ := cmd.Flags().GetBool("dev")
94+
return dev
95+
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func main() {
4545
color.Blue("Please run 'swiftwave init' to initialize a configuration file.")
4646
os.Exit(1)
4747
}
48+
// Set the development mode to false
49+
config.IsDevelopmentMode = false
4850
}
4951

5052
// Start the command line interface

swiftwave_service/graphql/server.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package graphql
22

33
import (
44
"github.com/99designs/gqlgen/graphql/handler"
5+
"github.com/99designs/gqlgen/graphql/handler/extension"
56
"github.com/99designs/gqlgen/graphql/handler/transport"
67
"github.com/99designs/gqlgen/graphql/playground"
78
"github.com/labstack/echo/v4"
@@ -18,7 +19,10 @@ func (server *Server) Initialize() {
1819
),
1920
)
2021
graphqlHandler.AddTransport(&transport.Websocket{})
21-
playgroundHandler := playground.Handler("GraphQL", "/graphql")
22+
23+
if server.SystemConfig.IsDevelopmentMode {
24+
graphqlHandler.Use(extension.Introspection{})
25+
}
2226

2327
server.EchoServer.GET("/graphql", func(c echo.Context) error {
2428
graphqlHandler.ServeHTTP(c.Response(), c.Request())
@@ -30,8 +34,12 @@ func (server *Server) Initialize() {
3034
return nil
3135
})
3236

33-
server.EchoServer.GET("/playground", func(c echo.Context) error {
34-
playgroundHandler.ServeHTTP(c.Response(), c.Request())
35-
return nil
36-
})
37+
if server.SystemConfig.IsDevelopmentMode {
38+
// Create GraphQL Playground
39+
playgroundHandler := playground.Handler("GraphQL", "/graphql")
40+
server.EchoServer.GET("/playground", func(c echo.Context) error {
41+
playgroundHandler.ServeHTTP(c.Response(), c.Request())
42+
return nil
43+
})
44+
}
3745
}

swiftwave_service/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func StartServer(config *system_config.Config, manager *core.ServiceManager, wor
8989
// Start the server
9090
address := fmt.Sprintf("%s:%d", config.ServiceConfig.BindAddress, config.ServiceConfig.BindPort)
9191
if config.ServiceConfig.UseTLS {
92-
println("Starting TLS Server")
92+
println("TLS Server Started on " + address)
9393

9494
tlsCfg := &tls.Config{
9595
Certificates: fetchCertificates(config.ServiceConfig.SSLCertificateDir),

system_config/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package system_config
22

33
type Config struct {
44
Version string `yaml:"version"`
5+
IsDevelopmentMode bool `yaml:"-"`
56
Mode Mode `yaml:"mode"`
67
ServiceConfig ServiceConfig `yaml:"service"`
78
HAProxyConfig HAProxyConfig `yaml:"haproxy"`

0 commit comments

Comments
 (0)