-
Notifications
You must be signed in to change notification settings - Fork 13
/
api.go
140 lines (121 loc) · 3.57 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package api
import (
"context"
"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/postgres"
"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) {
// Initialize Echo instance
e := server.New()
// Set up database connection
if err := setupDatabase(); err != nil {
return nil, err
}
// Set up JWT
jwt, err := jwt.New(
cfg.JWT.SigningAlgorithm,
os.Getenv("JWT_SECRET"),
cfg.JWT.DurationMinutes,
cfg.JWT.MinSecretLength)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
// Set up GraphQL
observers := map[string]chan *graphql.User{}
graphqlHandler := handler.New(graphql.NewExecutableSchema(graphql.Config{
Resolvers: &resolver.Resolver{Observers: observers},
}))
graphqlHandler.AroundOperations(func(ctx context.Context, next graphql2.OperationHandler) graphql2.ResponseHandler {
return authMw.GraphQLMiddleware(ctx, jwt, next)
})
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),
})
// Set up GraphQL endpoints
setupGraphQLEndpoints(e, graphqlHandler)
// Set up GraphQL playground
setupGraphQLPlayground(e)
// Start the server
server.Start(e, &server.Config{
Port: cfg.Server.Port,
ReadTimeoutSeconds: cfg.Server.ReadTimeout,
WriteTimeoutSeconds: cfg.Server.WriteTimeout,
Debug: cfg.Server.Debug,
})
return e, nil
}
func setupDatabase() error {
db, err := postgres.Connect()
if err != nil {
return err
}
boil.SetDB(db)
if os.Getenv("ENVIRONMENT_NAME") == "local" {
boil.DebugMode = true
}
return nil
}
func setupGraphQLEndpoints(e *echo.Echo, graphqlHandler *handler.Server) {
graphQLPathname := "/graphql"
gqlMiddleware := authMw.GqlMiddleware()
throttlerMiddleware := throttle.GqlMiddleware()
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)
}
func setupGraphQLPlayground(e *echo.Echo) {
graphQLPathname := "/graphql"
playgroundHandler := playground.Handler("GraphQL playground", graphQLPathname)
e.GET("/playground", func(c echo.Context) error {
req := c.Request()
res := c.Response()
playgroundHandler.ServeHTTP(res, req)
return nil
})
}