-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (59 loc) · 1.88 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"github.com/gorilla/websocket"
"github.com/nashl/online-store-server/auth"
dbConnection "github.com/nashl/online-store-server/database"
"github.com/nashl/online-store-server/graph"
"github.com/nashl/online-store-server/graph/generated"
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
_ "github.com/joho/godotenv/autoload"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/go-chi/chi"
"github.com/rs/cors"
)
const defaultPort = "9990"
func main() {
router := chi.NewRouter()
// Add CORS middleware around every request
// See https://github.com/rs/cors for full option listing
router.Use(cors.New(cors.Options{
AllowedOrigins: []string{os.Getenv("CLIENT_ORIGIN")},
AllowCredentials: true,
Debug: true,
}).Handler)
dbConnection.NewDatabase()
router.Use(auth.Middleware(dbConnection.DB))
//start connecting to the database
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
//srv := handler.New(starwars.NewExecutableSchema(starwars.NewResolver()))
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
srv.AddTransport(&transport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// Check against your desired domains here
return r.Host == "example.org"
},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
})
router.Handle("/", playground.Handler("GraphQL playground", "/query"))
router.Handle("/query", srv)
err := http.ListenAndServe(":8080", router)
if err != nil {
panic(err)
}
//
//http.Handle("/", 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))
}