github.com/Go-GraphQL-Group/GraphQL-Service@v0.0.0-20181226133140-0967350219a7/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  	"os"
     7  
     8  	"github.com/99designs/gqlgen/handler"
     9  	"github.com/Go-GraphQL-Group/GraphQL-Service/graphql"
    10  	"github.com/Go-GraphQL-Group/GraphQL-Service/resolver"
    11  	"github.com/Go-GraphQL-Group/GraphQL-Service/service"
    12  	"github.com/codegangsta/negroni"
    13  	"github.com/gorilla/mux"
    14  )
    15  
    16  const defaultPort = "9090"
    17  
    18  func NewServer() *negroni.Negroni {
    19  	router := mux.NewRouter()
    20  	initRoutes(router)
    21  
    22  	n := negroni.Classic()
    23  	n.Use(negroni.NewStatic(http.Dir("./static")))
    24  	n.UseHandler(router)
    25  	return n
    26  }
    27  
    28  func initRoutes(router *mux.Router) {
    29  	router.HandleFunc("/api/login", service.LoginHandler).Methods("POST")
    30  	router.Use(service.TokenMiddleware)
    31  	router.HandleFunc("/api", service.ApiHandler).Methods("GET")
    32  	// router.HandleFunc("/", handler.Playground("GraphQL playground", "/api/query"))
    33  	router.HandleFunc("/api/query", handler.GraphQL(graphql.NewExecutableSchema(graphql.Config{Resolvers: &resolver.Resolver{}})))
    34  	router.HandleFunc("/api/logout", service.LogoutHandler).Methods("POST", "GET")
    35  }
    36  
    37  func main() {
    38  	port := os.Getenv("PORT")
    39  	if port == "" {
    40  		port = defaultPort
    41  	}
    42  
    43  	server := NewServer()
    44  	server.Run(":" + port)
    45  
    46  	log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
    47  }