2021-07-17 16:29:50 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2023-08-28 14:21:42 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-07-17 16:29:50 +00:00
|
|
|
"github.com/99designs/gqlgen/graphql/playground"
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-08-28 14:21:42 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
|
|
|
"github.com/authorizerdev/authorizer/server/token"
|
2021-07-17 16:29:50 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// PlaygroundHandler is the handler for the /playground route
|
2021-07-17 16:29:50 +00:00
|
|
|
func PlaygroundHandler() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2023-08-28 14:21:42 +00:00
|
|
|
var h http.HandlerFunc
|
|
|
|
|
|
|
|
disablePlayground, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyDisablePlayGround)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("error while getting disable playground value")
|
2023-09-09 01:41:13 +00:00
|
|
|
disablePlayground = false
|
2023-08-28 14:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if env set to false, then check if logged in as super admin, if logged in then return graphql else 401 error
|
|
|
|
// if env set to true, then disabled the playground with 404 error
|
|
|
|
if !disablePlayground {
|
|
|
|
if token.IsSuperAdmin(c) {
|
|
|
|
h = playground.Handler("GraphQL", "/graphql")
|
|
|
|
} else {
|
|
|
|
log.Debug("not logged in as super admin")
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "not logged in as super admin"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Debug("playground is disabled")
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "playground is disabled"})
|
|
|
|
return
|
|
|
|
}
|
2021-07-17 16:29:50 +00:00
|
|
|
h.ServeHTTP(c.Writer, c.Request)
|
|
|
|
}
|
|
|
|
}
|