forked from JawherKl/go-crud-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
48 lines (38 loc) · 1.33 KB
/
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
42
43
44
45
46
47
48
package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/gorilla/handlers"
"github.com/jawherkl/go-crud-graphql/graph"
"github.com/jawherkl/go-crud-graphql/graph/generated"
"github.com/joho/godotenv"
)
const defaultPort = "8080"
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
err := godotenv.Load(".env");
if err != nil {
log.Fatal("Error loading .env file")
}
// Connexion à la base de données
Database := graph.Connect()
// Création du serveur GraphQL
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{DB: Database}}))
// Configuration des en-têtes CORS
corsHandler := handlers.CORS(
handlers.AllowedOrigins([]string{"http://localhost:3000"}), // Autoriser les requêtes depuis ce domaine
handlers.AllowedMethods([]string{"GET", "POST", "OPTIONS"}),
handlers.AllowedHeaders([]string{"Content-Type", "Authorization"}),
)
// Configuration des routes
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", corsHandler(srv))
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}