-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
43 lines (34 loc) · 982 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
42
43
package main
import (
"database/sql"
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
_ "github.com/mattn/go-sqlite3"
"github.com/rhawan/graphql/graph"
"github.com/rhawan/graphql/internal/database"
)
const defaultPort = "8080"
func main() {
db, err := sql.Open("sqlite3", "./data.db")
if err != nil {
log.Fatalf("failed to open database: %v", err)
}
defer db.Close()
categoryDb := database.NewCategory(db)
courseDb := database.NewCourse(db)
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{
CategoryDB: categoryDb,
CourseDB: courseDb,
}}))
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))
}