-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (78 loc) · 2.18 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/kamva/mgm/v3"
"go.mongodb.org/mongo-driver/mongo/options"
"messagewith-server/auth"
"messagewith-server/chats"
"messagewith-server/env"
"messagewith-server/graph"
"messagewith-server/graph/generated"
"messagewith-server/graph/model"
"messagewith-server/mails"
"messagewith-server/middlewares"
"messagewith-server/sessions"
"messagewith-server/users"
"net/http"
"time"
)
func getGraphqlHandler() gin.HandlerFunc {
srv := handler.New(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{
ChatMessages: []*model.Chat{},
ChatObservers: map[string]chan []*model.Chat{},
}}))
srv.AddTransport(&transport.POST{})
srv.AddTransport(&transport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
KeepAlivePingInterval: 10 * time.Second,
})
srv.SetQueryCache(lru.New(1000))
srv.Use(extension.Introspection{})
srv.Use(extension.AutomaticPersistedQuery{
Cache: lru.New(100),
})
return func(c *gin.Context) {
srv.ServeHTTP(c.Writer, c.Request)
}
}
func playgroundHandler() gin.HandlerFunc {
srv := playground.Handler("GraphQL", "/query")
return func(c *gin.Context) {
srv.ServeHTTP(c.Writer, c.Request)
}
}
func initDatabaseConnection() {
err := mgm.SetDefaultConfig(nil, "messagewith", options.Client().ApplyURI(env.DatabaseURI))
if err != nil {
panic(err)
}
}
func main() {
env.InitEnvConstants()
initDatabaseConnection()
mails.InitService()
users.InitService()
sessions.InitService()
auth.InitService()
chats.InitService()
r := gin.Default()
r.Use(middlewares.GinContextToContextMiddleware())
r.Use(middlewares.AuthMiddleware())
graphqlHandler := getGraphqlHandler()
r.POST("/query", graphqlHandler)
r.GET("/query", graphqlHandler)
r.GET("/", playgroundHandler())
r.Run(":8000")
}