Skip to content

Instantly share code, notes, and snippets.

@cmelgarejo
Created July 14, 2019 17:44
Show Gist options
  • Save cmelgarejo/32aba47039f77234ae97e16016eb5457 to your computer and use it in GitHub Desktop.
Save cmelgarejo/32aba47039f77234ae97e16016eb5457 to your computer and use it in GitHub Desktop.
package server
import (
"log"
"github.com/cmelgarejo/go-gql-server/internal/handlers"
"github.com/cmelgarejo/go-gql-server/pkg/utils"
"github.com/gin-gonic/gin"
)
var host, port, gqlPath, gqlPgPath string
var isPgEnabled bool
func init() {
host = utils.MustGet("GQL_SERVER_HOST")
port = utils.MustGet("GQL_SERVER_PORT")
gqlPath = utils.MustGet("GQL_SERVER_GRAPHQL_PATH")
gqlPgPath = utils.MustGet("GQL_SERVER_GRAPHQL_PLAYGROUND_PATH")
isPgEnabled = utils.MustGetBool("GQL_SERVER_GRAPHQL_PLAYGROUND_ENABLED")
}
// Run spins up the server
func Run() {
endpoint := "http://" + host + ":" + port
r := gin.Default()
// Handlers
// Simple keep-alive/ping handler
r.GET("/ping", handlers.Ping())
// GraphQL handlers
// Playground handler
if isPgEnabled {
r.GET(gqlPgPath, handlers.PlaygroundHandler(gqlPath))
log.Println("GraphQL Playground @ " + endpoint + gqlPgPath)
}
r.POST(gqlPath, handlers.GraphqlHandler())
log.Println("GraphQL @ " + endpoint + gqlPath)
// Run the server
// Inform the user where the server is listening
log.Println("Running @ " + endpoint)
// Print out and exit(1) to the OS if the server cannot run
log.Fatalln(r.Run(host + ":" + port))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment