Repository navigation
Expand file tree
/
Copy pathapiserver.go
More file actions
192 lines (169 loc) · 5.16 KB
/
Copy pathapiserver.go
File metadata and controls
192 lines (169 loc) · 5.16 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package console
import (
"encoding/json"
"fmt"
"net/http"
"github.com/hasura/graphql-engine/cli/v2/internal/hasura"
"github.com/hasura/graphql-engine/cli/v2/internal/metadatautil"
"github.com/pkg/errors"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/hasura/graphql-engine/cli/v2"
"github.com/hasura/graphql-engine/cli/v2/migrate"
"github.com/hasura/graphql-engine/cli/v2/migrate/api"
_ "github.com/hasura/graphql-engine/cli/v2/migrate/database/hasuradb"
"github.com/sirupsen/logrus"
)
type APIServer struct {
Router *gin.Engine
Migrate *migrate.Migrate
Address string
Port string
EC *cli.ExecutionContext
}
type errMessage struct {
ErrorMessage string `json:"error"`
}
func (e errMessage) Error() string {
b, err := json.Marshal(e)
if err == nil {
return string(b)
}
return e.ErrorMessage
}
func cliProjectUpdateCheck(ec *cli.ExecutionContext) gin.HandlerFunc {
const updateRequiredMessage = "looks like you are trying to use hasura with multiple databases, this requires changes to your project directory. Please use `hasura scripts update-project-v3` to update your project"
return func(c *gin.Context) {
type response struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Name string `json:"name,omitempty"`
StatusCode int `json:"-"`
}
// if a user is on config v2 and they have multiple databases prompt them to upgrade
if ec.Config.Version <= cli.V2 && ec.HasMetadataV3 {
sources, err := metadatautil.GetSources(ec.APIClient.V1Metadata.ExportMetadata)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, &response{Code: "internal_error", Message: errMessage{"cannot list sources"}.Error()})
return
}
if len(sources) > 1 {
r := response{
Code: "internal_error",
Message: errMessage{updateRequiredMessage}.Error(),
}
c.AbortWithStatusJSON(http.StatusInternalServerError, &r)
return
}
}
c.Next()
}
}
func NewAPIServer(address string, port string, ec *cli.ExecutionContext) (*APIServer, error) {
migrate, err := migrate.NewMigrate(ec, false, "", hasura.SourceKindPG)
if err != nil {
return nil, errors.Wrap(err, "error creating migrate instance")
}
gin.SetMode(gin.ReleaseMode)
router := gin.New()
// Setup API Router
// Switch to "release" mode in production.
gin.SetMode(gin.ReleaseMode)
// An Engine instance with the Logger and Recovery middleware already attached.
router.Use(allowCors())
router.Use(cliProjectUpdateCheck(ec))
apiServer := &APIServer{Router: router, Migrate: migrate, Address: address, Port: port, EC: ec}
apiServer.setRoutes(ec.MigrationDir, ec.Logger)
return apiServer, nil
}
func (r *APIServer) GetHTTPServer() *http.Server {
// create servers
return &http.Server{
Addr: fmt.Sprintf("%s:%s", r.Address, r.Port),
Handler: r.Router,
}
}
func (r *APIServer) setRoutes(migrationDir string, logger *logrus.Logger) {
apis := r.Router.Group("/apis")
{
apis.Use(r.setLogger(logger))
apis.Use(r.setFilePath(migrationDir))
apis.Use(r.setMigrate(r.Migrate))
apis.Use(r.setEC(r.EC))
apis.Use(r.setConfigVersion(r.EC.Config.Version))
// Migrate api endpoints and middleware
migrateAPIs := apis.Group("/migrate")
{
settingsAPIs := migrateAPIs.Group("/settings")
{
settingsAPIs.Any("", api.SettingsAPI)
}
squashAPIs := migrateAPIs.Group("/squash")
{
squashAPIs.POST("/create", api.SquashCreateAPI)
squashAPIs.POST("/delete", api.SquashDeleteAPI)
}
migrateAPIs.Any("", api.MigrateAPI)
}
// Migrate api endpoints and middleware
metadataAPIs := apis.Group("/metadata")
{
metadataAPIs.Any("", api.MetadataAPI)
}
}
}
func (r *APIServer) setMigrate(t *migrate.Migrate) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("migrate", t)
c.Next()
}
}
func (r *APIServer) setEC(ec *cli.ExecutionContext) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("ec", ec)
c.Next()
}
}
func (r *APIServer) setFilePath(dir string) gin.HandlerFunc {
return func(c *gin.Context) {
host := migrate.GetFilePath(dir)
c.Set("filedir", host)
c.Next()
}
}
func (r *APIServer) setConfigVersion(configVersion cli.ConfigVersion) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("version", int(configVersion))
c.Next()
}
}
/*
func (r *APIServer) setMetadataFile(file string) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("metadataFile", file)
c.Next()
}
}
*/
func (r *APIServer) setLogger(logger *logrus.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("logger", logger)
c.Next()
}
}
func allowCors() gin.HandlerFunc {
var config = cors.DefaultConfig()
config.AddAllowHeaders("X-Hasura-User-Id")
config.AddAllowHeaders(cli.XHasuraAccessKey)
config.AddAllowHeaders(cli.XHasuraAdminSecret)
config.AddAllowHeaders("hasura-client-name")
config.AddAllowHeaders("hasura-collaborator-token")
config.AddAllowHeaders("X-Hasura-Role")
config.AddAllowHeaders("X-Hasura-Allowed-Roles")
config.AddAllowHeaders("Hasura-Internal-Request-Source")
config.AddAllowMethods("DELETE")
config.AddAllowHeaders("Hasura-Internal-Request-Source")
config.AllowAllOrigins = true
config.AllowCredentials = false
return cors.New(config)
}