-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
104 lines (86 loc) · 2.37 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"context"
"log"
"net/http"
"os"
"sync"
"time"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/ottolauncher/church-app/graph"
db "github.com/ottolauncher/church-app/graph/db/mongo"
"github.com/ottolauncher/church-app/graph/generated"
"go.mongodb.org/mongo-driver/mongo"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
const defaultPort = "8080"
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CSRFWithConfig(middleware.CSRFConfig{
TokenLookup: "header:X-XSRF-TOKEN",
}))
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
}))
var (
once sync.Once
dao *mongo.Client
)
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
once.Do(func() {
dao = db.Init()
})
src := dao.Database("churchApp")
defer func() {
if err := dao.Disconnect(context.TODO()); err != nil {
log.Fatal(err)
}
}()
um := db.NewUserManager(src)
tm := db.NewTaskManager(src)
// e.Use(auth.AuthMiddleware(um))
config := generated.Config{Resolvers: &graph.Resolver{UM: um, TM: tm}}
srv := handler.NewDefaultServer(generated.NewExecutableSchema(config))
e.GET("/", func(c echo.Context) error {
return c.JSON(http.StatusOK, echo.Map{"message": "alive"})
})
e.POST("/login", um.Login)
e.POST("/register", um.Register)
e.GET("/playground", func(c echo.Context) error {
playground.Handler("GraphQL playground", "/query").ServeHTTP(c.Response(), c.Request())
return nil
})
cfg := middleware.JWTConfig{
Claims: &db.JWTCustomClaims{},
SigningKey: db.SecretKey,
}
r := e.Group("/query")
r.Use(middleware.JWTWithConfig(cfg))
r.POST("/query", func(c echo.Context) error {
srv.ServeHTTP(c.Response(), c.Request())
return nil
})
h2s := &http2.Server{
MaxConcurrentStreams: 250,
MaxReadFrameSize: 1048576,
IdleTimeout: 10 * time.Second,
}
s := http.Server{
Addr: ":" + port,
Handler: h2c.NewHandler(e, h2s),
}
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
if err := s.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal(err)
}
}