-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (57 loc) · 1.59 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
package main
import (
"context"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/shawnzxx/graphql-twitter/config"
"github.com/shawnzxx/graphql-twitter/domain"
"github.com/shawnzxx/graphql-twitter/graph"
"github.com/shawnzxx/graphql-twitter/postgres"
"log"
"net/http"
"time"
)
func main() {
ctx := context.Background()
// load configuration
config.LoadEnv(".env")
conf := config.New()
// init db pool and configure
db := postgres.New(ctx, conf)
// migrate db when every time app restart
if err := db.Migrate(); err != nil {
log.Fatal(err)
}
router := chi.NewRouter()
// middleware section
router.Use(middleware.Logger)
router.Use(middleware.RequestID)
router.Use(middleware.Recoverer)
router.Use(middleware.RedirectSlashes)
router.Use(middleware.Timeout(time.Second * 60))
// DI layers
//
// -> Graphql Handler depends on Service
// -> Service depends on Repository
// -> Repository depends on Database instance
// 1: inject db into Repos layer
userRepo := postgres.NewUserRepo(db)
// 2: inject db repo into Domain Service layer
authService := domain.NewAuthService(userRepo)
// graphql settings
router.Handle("/", playground.Handler("Twitter clone", "/query"))
router.Handle("/query", handler.NewDefaultServer(
graph.NewExecutableSchema(
graph.Config{
// 3: inject domain service into graphql handler
Resolvers: &graph.Resolver{
AuthService: authService,
},
},
),
))
// start server
log.Fatal(http.ListenAndServe(":8080", router))
}