server.go 1.1 KB
package main

import (
	"github.com/99designs/gqlgen/graphql/handler"
	"github.com/99designs/gqlgen/graphql/playground"
	"github.com/gin-gonic/gin"
	_ "github.com/tiptok/godevp/pkg/infrastructure/pg"
	"github.com/tiptok/godevp/pkg/port/graphql/graph"
	"github.com/tiptok/godevp/pkg/port/graphql/graph/generated"
	"log"
	"net/http"
	"os"
)

const defaultPort = "8080"

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		port = defaultPort
	}

	ginRun(port)
}

func httpRun(port string) {

	srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))

	http.Handle("/ui", playground.Handler("GraphQL playground", "/query"))
	http.Handle("/query", srv)

	log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
	log.Fatal(http.ListenAndServe(":"+port, nil))
}

func ginRun(port string) {
	svr := gin.New()
	svr.Use(gin.Recovery())

	h := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
	svr.Any("/ui", gin.WrapH(playground.Handler("GraphQL playground", "/query")))
	svr.Any("/query", gin.WrapH(h))
	svr.Run(":" + port)
}