-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbedrock_example_test.go
51 lines (40 loc) · 1.15 KB
/
bedrock_example_test.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
// Copyright (c) 2023 Z5Labs and Contributors
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package bedrock
import (
"context"
"fmt"
"strings"
"github.com/z5labs/bedrock/config"
)
type appFunc func(context.Context) error
func (f appFunc) Run(ctx context.Context) error {
return f(ctx)
}
func ExampleRun() {
r := strings.NewReader(`hello: world`)
// Define a custom config struct which aligns with your
// the format of your config.Source(s).
type MyConfig struct {
Hello string `config:"hello"`
}
// Define a AppBuilder.
builder := AppBuilderFunc[MyConfig](func(ctx context.Context, cfg MyConfig) (App, error) {
// Inside your AppBuilder.Build is where you should be initializing
// all dependencies of your code e.g. backend API clients, DB connections, etc.
// Lastly, initialize something which implements the bedrock.App interface.
app := appFunc(func(c context.Context) error {
fmt.Println("hello,", cfg.Hello)
return nil
})
return app, nil
})
err := Run(context.Background(), builder, config.FromYaml(r))
if err != nil {
fmt.Println(err)
return
}
//Output: hello, world
}