-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.go
124 lines (104 loc) · 3.22 KB
/
api.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Package api Go-Template
package api
import (
"context"
"fmt"
"net/http"
"os"
"time"
graphql "go-template/gqlmodels"
"go-template/internal/config"
"go-template/internal/jwt"
authMw "go-template/internal/middleware/auth"
"go-template/internal/mysql"
"go-template/internal/server"
throttle "go-template/pkg/utl/throttle"
"go-template/resolver"
graphql2 "github.com/99designs/gqlgen/graphql"
"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/gorilla/websocket"
"github.com/labstack/echo/v4"
_ "github.com/lib/pq" // here
"github.com/volatiletech/sqlboiler/v4/boil"
)
// Start starts the API service
func Start(cfg *config.Configuration) (*echo.Echo, error) {
fmt.Printf("connection started")
db, err := mysql.Connect()
if err != nil {
return nil, err
}
boil.SetDB(db)
jwt, err := jwt.New(
cfg.JWT.SigningAlgorithm,
os.Getenv("JWT_SECRET"),
cfg.JWT.DurationMinutes,
cfg.JWT.MinSecretLength)
if err != nil {
return nil, err
}
e := server.New()
gqlMiddleware := authMw.GqlMiddleware()
// throttlerMiddleware puts the current user's IP address into context of gqlgen
throttlerMiddleware := throttle.GqlMiddleware()
graphQLPathname := "/graphql"
playgroundHandler := playground.Handler("GraphQL playground", graphQLPathname)
observers := map[string]chan *graphql.User{}
graphqlHandler := handler.New(graphql.NewExecutableSchema(graphql.Config{
Resolvers: &resolver.Resolver{Observers: observers},
}))
if os.Getenv("ENVIRONMENT_NAME") == "local" {
boil.DebugMode = true
}
// graphql apis
graphqlHandler.AroundOperations(func(ctx context.Context, next graphql2.OperationHandler) graphql2.ResponseHandler {
return authMw.GraphQLMiddleware(ctx, jwt, next)
})
e.POST(graphQLPathname, func(c echo.Context) error {
req := c.Request()
res := c.Response()
graphqlHandler.ServeHTTP(res, req)
return nil
}, gqlMiddleware, throttlerMiddleware)
e.GET(graphQLPathname, func(c echo.Context) error {
req := c.Request()
res := c.Response()
graphqlHandler.ServeHTTP(res, req)
return nil
}, gqlMiddleware, throttlerMiddleware)
graphqlHandler.AddTransport(transport.Websocket{
KeepAlivePingInterval: 10 * time.Second,
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
})
graphqlHandler.AddTransport(transport.Options{})
graphqlHandler.AddTransport(transport.GET{})
graphqlHandler.AddTransport(transport.POST{})
graphqlHandler.AddTransport(transport.MultipartForm{})
graphqlHandler.SetQueryCache(lru.New(1000))
graphqlHandler.Use(extension.Introspection{})
graphqlHandler.Use(extension.AutomaticPersistedQuery{
Cache: lru.New(100),
})
// graphql playground
e.GET("/playground", func(c echo.Context) error {
req := c.Request()
res := c.Response()
playgroundHandler.ServeHTTP(res, req)
return nil
})
server.Start(e, &server.Config{
Port: cfg.Server.Port,
ReadTimeoutSeconds: cfg.Server.ReadTimeout,
WriteTimeoutSeconds: cfg.Server.WriteTimeout,
Debug: cfg.Server.Debug,
})
return e, nil
}