-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
46 lines (35 loc) · 1 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
package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
_ "github.com/go-sql-driver/mysql"
"github.com/joho/godotenv"
"github.com/taro-28/saas-sample-api/db"
"github.com/taro-28/saas-sample-api/gql"
loaders "github.com/taro-28/saas-sample-api/loader"
)
const defaultPort = "8080"
func main() {
// Load connection string from .env file
godotenv.Load()
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
db, cleanup, err := db.Connect()
if err != nil {
log.Fatalf("failed to connect to db: %v", err)
}
defer cleanup()
var srv http.Handler = handler.NewDefaultServer(gql.NewExecutableSchema(gql.Config{Resolvers: &gql.Resolver{
DB: db,
}}))
srv = loaders.Middleware(db, srv)
http.Handle("/", playground.Handler("SaaS Sample API", "/query"))
http.Handle("/query", srv)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}