-
Notifications
You must be signed in to change notification settings - Fork 4
/
model.go
489 lines (404 loc) · 11.3 KB
/
model.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package program
import (
"context"
"crypto/rsa"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/99designs/gqlgen/graphql/playground"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
"github.com/teamkeel/keel/cmd/database"
"github.com/teamkeel/keel/codegen"
"github.com/teamkeel/keel/config"
"github.com/teamkeel/keel/db"
"github.com/teamkeel/keel/events"
"github.com/teamkeel/keel/functions"
"github.com/teamkeel/keel/mail"
"github.com/teamkeel/keel/migrations"
"github.com/teamkeel/keel/node"
"github.com/teamkeel/keel/proto"
"github.com/teamkeel/keel/runtime"
"github.com/teamkeel/keel/runtime/runtimectx"
"github.com/teamkeel/keel/schema/reader"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
const (
ModeRun = iota
)
const (
StatusCheckingDependencies = iota
StatusParsePrivateKey
StatusSetupDatabase
StatusSetupFunctions
StatusLoadSchema
StatusRunMigrations
StatusUpdateFunctions
StatusStartingFunctions
StatusRunning
StatusQuitting
)
func Run(model *Model) {
// The runtime currently does logging with logrus, which is super noisy.
// For now we just discard the logs as they are not useful in the CLI
logrus.SetOutput(io.Discard)
if model.TracingEnabled {
exporter, err := otlptracehttp.New(context.Background(), otlptracehttp.WithInsecure())
if err != nil {
panic(err)
}
provider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(
resource.NewSchemaless(attribute.String("service.name", "runtime")),
),
)
otel.SetTracerProvider(provider)
otel.SetTextMapPropagator(propagation.TraceContext{})
} else {
// We still need a tracing provider for auditing and events,
// even if the data is not being exported.
otel.SetTracerProvider(sdktrace.NewTracerProvider())
otel.SetTextMapPropagator(propagation.TraceContext{})
}
defer func() {
_ = database.Stop()
if model.FunctionsServer != nil {
_ = model.FunctionsServer.Kill()
}
}()
_, err := tea.NewProgram(model).Run()
if err != nil {
panic(err)
}
if model.Err != nil {
os.Exit(1)
}
}
type Model struct {
// The directory of the Keel project
ProjectDir string
// The mode the Model is running in
Mode int
// Port to run the runtime server on in ModeRun
Port string
// If true then the database will be reset. Only
// applies to ModeRun.
ResetDatabase bool
// If set then @teamkeel/* npm packages will be installed
// from this path, rather than NPM.
NodePackagesPath string
// If set then runtime will be configured with private key
// located at this path in pem format.
PrivateKeyPath string
// The private key to configure on runtime, or nil.
PrivateKey *rsa.PrivateKey
// Pattern to pass to vitest to isolate specific tests
TestPattern string
// If true then an OTLP export will be setup for the runtime and the
// env var KEEL_TRACING_ENABLED will be passed to the functions runtime
TracingEnabled bool
// Model state - used in View()
Status int
Err error
Schema *proto.Schema
Config *config.ProjectConfig
SchemaFiles []*reader.SchemaFile
Database db.Database
DatabaseConnInfo *db.ConnectionInfo
GeneratedFiles codegen.GeneratedFiles
MigrationChanges []*migrations.DatabaseChange
FunctionsServer *node.DevelopmentServer
RuntimeHandler http.Handler
JobHandler runtime.JobHandler
SubscriberHandler runtime.SubscriberHandler
RuntimeRequests []*RuntimeRequest
FunctionsLog []*FunctionLog
TestOutput string
Secrets map[string]string
Environment string
// Channels for communication between long-running
// commands and the Bubbletea program
runtimeRequestsCh chan tea.Msg
functionsLogCh chan tea.Msg
watcherCh chan tea.Msg
// Maintain the current dimensions of the user's terminal
width int
height int
}
type RuntimeRequest struct {
Time time.Time
Method string
Path string
}
type FunctionLog struct {
Time time.Time
Value string
}
var _ tea.Model = &Model{}
func (m *Model) Init() tea.Cmd {
m.runtimeRequestsCh = make(chan tea.Msg, 1)
m.functionsLogCh = make(chan tea.Msg, 1)
m.watcherCh = make(chan tea.Msg, 1)
m.Environment = "development"
m.Status = StatusCheckingDependencies
return CheckDependencies()
}
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
m.Status = StatusQuitting
return m, tea.Quit
}
case tea.WindowSizeMsg:
// This msg is sent once on program start
// and then subsequently every time the user
// resizes their terminal window.
m.width = msg.Width
m.height = msg.Height
return m, nil
case CheckDependenciesMsg:
m.Err = msg.Err
if m.Err != nil {
return m, tea.Quit
}
m.Status = StatusSetupDatabase
return m, StartDatabase(m.ResetDatabase, m.Mode, m.ProjectDir)
case StartDatabaseMsg:
m.DatabaseConnInfo = msg.ConnInfo
m.Err = msg.Err
// If the database can't be started we exit
if m.Err != nil {
return m, tea.Quit
}
database, err := db.New(context.Background(), m.DatabaseConnInfo.String())
if err != nil {
m.Err = err
return m, tea.Quit
}
m.Database = database
m.Status = StatusParsePrivateKey
return m, ParsePrivateKey(m.PrivateKeyPath)
case ParsePrivateKeyMsg:
m.Err = msg.Err
// If the private key can't be parsed we exit
if m.Err != nil {
return m, tea.Quit
}
m.PrivateKey = msg.PrivateKey
m.Status = StatusSetupFunctions
return m, SetupFunctions(m.ProjectDir, m.NodePackagesPath)
case SetupFunctionsMsg:
m.Err = msg.Err
// If something failed here (most likely npm install) we exit
if m.Err != nil {
return m, tea.Quit
}
m.Status = StatusLoadSchema
cmds := []tea.Cmd{
StartRuntimeServer(m.Port, m.runtimeRequestsCh),
NextMsgCommand(m.runtimeRequestsCh),
LoadSchema(m.ProjectDir, m.Environment),
}
if m.Mode == ModeRun {
cmds = append(
cmds,
StartWatcher(m.ProjectDir, m.watcherCh, nil),
NextMsgCommand(m.watcherCh),
)
}
return m, tea.Batch(cmds...)
case LoadSchemaMsg:
m.Schema = msg.Schema
m.SchemaFiles = msg.SchemaFiles
m.Config = msg.Config
m.Err = msg.Err
m.Secrets = msg.Secrets
if m.Err != nil {
return m, nil
}
cors := cors.New(cors.Options{
AllowOriginFunc: func(origin string) bool {
return true
},
AllowedMethods: []string{
http.MethodHead,
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
})
m.RuntimeHandler = cors.Handler(runtime.NewHttpHandler(m.Schema))
m.Status = StatusRunMigrations
return m, RunMigrations(m.Schema, m.Database)
case RunMigrationsMsg:
m.Err = msg.Err
m.MigrationChanges = msg.Changes
if m.Err != nil {
return m, nil
}
if m.Mode == ModeRun && !node.HasFunctions(m.Schema) {
m.Status = StatusRunning
return m, nil
}
m.Status = StatusUpdateFunctions
return m, UpdateFunctions(m.Schema, m.ProjectDir)
case UpdateFunctionsMsg:
m.Err = msg.Err
if m.Err != nil {
return m, nil
}
// If functions already running nothing to do
if m.FunctionsServer != nil {
_ = m.FunctionsServer.Rebuild()
m.Status = StatusRunning
return m, nil
}
// Start functions if needed
if node.HasFunctions(m.Schema) {
m.Status = StatusStartingFunctions
return m, tea.Batch(
StartFunctions(m),
NextMsgCommand(m.functionsLogCh),
)
}
return m, nil
case StartFunctionsMsg:
m.Err = msg.Err
m.FunctionsServer = msg.Server
if msg.Err == nil {
m.Status = StatusRunning
}
return m, nil
case FunctionsOutputMsg:
log := &FunctionLog{
Time: time.Now(),
Value: msg.Output,
}
m.FunctionsLog = append(m.FunctionsLog, log)
cmds := []tea.Cmd{
NextMsgCommand(m.functionsLogCh),
}
if m.Mode == ModeRun {
cmds = append(cmds, tea.Println(renderFunctionLog(log)))
}
return m, tea.Batch(cmds...)
case RuntimeRequestMsg:
r := msg.r
w := msg.w
request := &RuntimeRequest{
Time: time.Now(),
Method: r.Method,
Path: r.URL.Path,
}
cmds := []tea.Cmd{
NextMsgCommand(m.runtimeRequestsCh),
}
m.RuntimeRequests = append(m.RuntimeRequests, request)
// log runtime requests for the run cmd
if m.Mode == ModeRun && m.Err == nil && m.Status >= StatusLoadSchema {
cmds = append(cmds, tea.Println(renderRequestLog(request)))
}
if strings.HasSuffix(r.URL.Path, "/graphiql") {
handler := playground.Handler("GraphiQL", strings.TrimSuffix(r.URL.Path, "/graphiql")+"/graphql")
handler(w, r)
msg.done <- true
return m, NextMsgCommand(m.runtimeRequestsCh)
}
if m.RuntimeHandler == nil {
w.WriteHeader(500)
_, _ = w.Write([]byte("Cannot serve requests while there are schema errors. Please see the CLI output for more info."))
msg.done <- true
return m, NextMsgCommand(m.runtimeRequestsCh)
}
ctx := msg.r.Context()
if m.PrivateKey != nil {
ctx = runtimectx.WithPrivateKey(ctx, m.PrivateKey)
}
ctx = db.WithDatabase(ctx, m.Database)
ctx = runtimectx.WithSecrets(ctx, m.Secrets)
ctx = runtimectx.WithOAuthConfig(ctx, &m.Config.Auth)
mailClient := mail.NewSMTPClientFromEnv()
if mailClient != nil {
ctx = runtimectx.WithMailClient(ctx, mailClient)
} else {
ctx = runtimectx.WithMailClient(ctx, mail.NoOpClient())
}
if m.FunctionsServer != nil {
ctx = functions.WithFunctionsTransport(
ctx,
functions.NewHttpTransport(m.FunctionsServer.URL),
)
}
envVars := m.Config.GetEnvVars("development")
for k, v := range envVars {
os.Setenv(k, v)
}
// Synchronous event handling for keel run.
// TODO: make asynchronous
ctx, err := events.WithEventHandler(ctx, func(ctx context.Context, subscriber string, event *events.Event, traceparent string) error {
return runtime.NewSubscriberHandler(m.Schema).RunSubscriber(ctx, subscriber, event)
})
if err != nil {
m.Err = err
return m, tea.Quit
}
// In run mode we accept any external issuers but the tokens need to be signed correctly
ctx = runtimectx.WithAuthConfig(ctx, runtimectx.AuthConfig{
AllowAnyIssuers: true,
})
r = msg.r.WithContext(ctx)
m.RuntimeHandler.ServeHTTP(msg.w, r)
for k := range envVars {
os.Unsetenv(k)
}
msg.done <- true
return m, tea.Batch(cmds...)
case WatcherMsg:
m.Err = msg.Err
m.Status = StatusLoadSchema
// If the watcher errors then probably best to exit
if m.Err != nil {
return m, tea.Quit
}
return m, tea.Batch(
NextMsgCommand(m.watcherCh),
LoadSchema(m.ProjectDir, m.Environment),
)
}
return m, nil
}
func (m *Model) View() string {
b := strings.Builder{}
// lipgloss will automatically wrap any text based on the current dimensions of the user's term.
s := lipgloss.
NewStyle().
MaxWidth(m.width).
MaxHeight(m.height)
// Mode specific output
switch m.Mode {
case ModeRun:
b.WriteString(renderRun(m))
}
if m.Err != nil {
b.WriteString(renderError(m))
}
// The final "\n" is important as when Bubbletea exists it resets the last
// line of output, meaning without a new line we'd lose the final line
return s.Render(b.String() + "\n")
}