-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
41 lines (34 loc) · 929 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi"
"github.com/rs/cors"
"github.com/xuqingfeng/TopHackerNews/graph"
)
const defaultPort = "8080"
const defaultAppEnv = "prod"
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
appEnv := os.Getenv("APP_ENV")
if appEnv == "" {
appEnv = defaultAppEnv
}
router := chi.NewRouter()
if appEnv == "local" {
router.Use(cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3000"},
}).Handler)
}
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
router.Handle("/", playground.Handler("GraphQL playground", "/graphql"))
router.Handle("/graphql", srv)
log.Printf("api serves on :%s/graphql", port)
log.Fatal(http.ListenAndServe(":"+port, router))
}