forked from rollmelette/rollmelette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
47 lines (41 loc) · 1.11 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
// Copyright (c) Gabriel de Quadros Ligneul
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
package rollmelette
import (
"context"
)
// RunOpts allows the application developer to pass some parameters to the run function.
type RunOpts struct {
AddressBook
// RollupURL is the URL of the Rollup API.
RollupURL string
}
// NewRunOpts creates a RunOpts struct with sensible default values.
func NewRunOpts() *RunOpts {
var opts RunOpts
opts.AddressBook = NewAddressBook()
opts.RollupURL = "http://127.0.0.1:5004"
return &opts
}
// Run connects to the Rollup API and calls the application.
// If opt is nil, this function creates it with the NewRunOpts function.
func Run(ctx context.Context, opts *RunOpts, app Application) (err error) {
if opts == nil {
opts = NewRunOpts()
}
rollup := newRollupHttp(opts.RollupURL)
env := newEnv(ctx, opts.AddressBook, rollup, app)
status := finishStatusAccept
for {
input, err := rollup.finishAndGetNext(ctx, status)
if err != nil {
return err
}
err = env.handle(input)
if err != nil {
status = finishStatusReject
} else {
status = finishStatusAccept
}
}
}