-
Notifications
You must be signed in to change notification settings - Fork 157
/
run.go
218 lines (165 loc) · 5.77 KB
/
run.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package run
import (
"context"
"fmt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/hatchet-dev/hatchet/api/v1/server/authn"
"github.com/hatchet-dev/hatchet/api/v1/server/authz"
apitokens "github.com/hatchet-dev/hatchet/api/v1/server/handlers/api-tokens"
"github.com/hatchet-dev/hatchet/api/v1/server/handlers/events"
"github.com/hatchet-dev/hatchet/api/v1/server/handlers/metadata"
stepruns "github.com/hatchet-dev/hatchet/api/v1/server/handlers/step-runs"
"github.com/hatchet-dev/hatchet/api/v1/server/handlers/tenants"
"github.com/hatchet-dev/hatchet/api/v1/server/handlers/users"
"github.com/hatchet-dev/hatchet/api/v1/server/handlers/workers"
"github.com/hatchet-dev/hatchet/api/v1/server/handlers/workflows"
"github.com/hatchet-dev/hatchet/api/v1/server/middleware/populator"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen"
"github.com/hatchet-dev/hatchet/internal/config/server"
hatchetmiddleware "github.com/hatchet-dev/hatchet/api/v1/server/middleware"
)
type apiService struct {
*users.UserService
*tenants.TenantService
*events.EventService
*workflows.WorkflowService
*workers.WorkerService
*metadata.MetadataService
*apitokens.APITokenService
*stepruns.StepRunService
}
func newAPIService(config *server.ServerConfig) *apiService {
return &apiService{
UserService: users.NewUserService(config),
TenantService: tenants.NewTenantService(config),
EventService: events.NewEventService(config),
WorkflowService: workflows.NewWorkflowService(config),
WorkerService: workers.NewWorkerService(config),
MetadataService: metadata.NewMetadataService(config),
APITokenService: apitokens.NewAPITokenService(config),
StepRunService: stepruns.NewStepRunService(config),
}
}
type APIServer struct {
config *server.ServerConfig
}
func NewAPIServer(config *server.ServerConfig) *APIServer {
return &APIServer{
config: config,
}
}
func (t *APIServer) Run(ctx context.Context) error {
errCh := make(chan error)
oaspec, err := gen.GetSwagger()
if err != nil {
return err
}
e := echo.New()
// application middleware
populatorMW := populator.NewPopulator(t.config)
populatorMW.RegisterGetter("tenant", func(config *server.ServerConfig, parentId, id string) (result interface{}, uniqueParentId string, err error) {
tenant, err := config.Repository.Tenant().GetTenantByID(id)
if err != nil {
return nil, "", err
}
return tenant, "", nil
})
populatorMW.RegisterGetter("api-token", func(config *server.ServerConfig, parentId, id string) (result interface{}, uniqueParentId string, err error) {
apiToken, err := config.Repository.APIToken().GetAPITokenById(id)
if err != nil {
return nil, "", err
}
// at the moment, API tokens should have a tenant id, because there are no other types of
// API tokens. If we add other types of API tokens, we'll need to pass in a parent id to query
// for.
tenantId, ok := apiToken.TenantID()
if !ok {
return nil, "", fmt.Errorf("api token has no tenant id")
}
return apiToken, tenantId, nil
})
populatorMW.RegisterGetter("tenant-invite", func(config *server.ServerConfig, parentId, id string) (result interface{}, uniqueParentId string, err error) {
tenantInvite, err := config.Repository.TenantInvite().GetTenantInvite(id)
if err != nil {
return nil, "", err
}
return tenantInvite, tenantInvite.TenantID, nil
})
populatorMW.RegisterGetter("workflow", func(config *server.ServerConfig, parentId, id string) (result interface{}, uniqueParentId string, err error) {
workflow, err := config.Repository.Workflow().GetWorkflowById(id)
if err != nil {
return nil, "", err
}
return workflow, workflow.TenantID, nil
})
populatorMW.RegisterGetter("workflow-run", func(config *server.ServerConfig, parentId, id string) (result interface{}, uniqueParentId string, err error) {
workflowRun, err := config.Repository.WorkflowRun().GetWorkflowRunById(parentId, id)
if err != nil {
return nil, "", err
}
return workflowRun, workflowRun.TenantID, nil
})
populatorMW.RegisterGetter("step-run", func(config *server.ServerConfig, parentId, id string) (result interface{}, uniqueParentId string, err error) {
stepRun, err := config.Repository.StepRun().GetStepRunById(parentId, id)
if err != nil {
return nil, "", err
}
return stepRun, stepRun.TenantID, nil
})
populatorMW.RegisterGetter("event", func(config *server.ServerConfig, parentId, id string) (result interface{}, uniqueParentId string, err error) {
event, err := config.Repository.Event().GetEventById(id)
if err != nil {
return nil, "", err
}
return event, event.TenantID, nil
})
populatorMW.RegisterGetter("worker", func(config *server.ServerConfig, parentId, id string) (result interface{}, uniqueParentId string, err error) {
worker, err := config.Repository.Worker().GetWorkerById(id)
if err != nil {
return nil, "", err
}
return worker, worker.TenantID, nil
})
authnMW := authn.NewAuthN(t.config)
authzMW := authz.NewAuthZ(t.config)
mw, err := hatchetmiddleware.NewMiddlewareHandler(oaspec)
if err != nil {
return err
}
mw.Use(populatorMW.Middleware)
mw.Use(authnMW.Middleware)
mw.Use(authzMW.Middleware)
allHatchetMiddleware, err := mw.Middleware()
if err != nil {
return err
}
// register echo middleware
e.Use(
middleware.Logger(),
middleware.Recover(),
allHatchetMiddleware,
)
service := newAPIService(t.config)
myStrictApiHandler := gen.NewStrictHandler(service, []gen.StrictMiddlewareFunc{})
gen.RegisterHandlers(e, myStrictApiHandler)
go func() {
if err := e.Start(fmt.Sprintf(":%d", t.config.Runtime.Port)); err != nil {
errCh <- err
}
}()
Loop:
for {
select {
case err := <-errCh:
return err
case <-ctx.Done():
break Loop
}
}
err = e.Shutdown(ctx)
if err != nil {
return err
}
return nil
}