-
Notifications
You must be signed in to change notification settings - Fork 9
/
entrypoint.go
40 lines (31 loc) · 1.13 KB
/
entrypoint.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package servercore
import (
"context"
"namespacelabs.dev/foundation/std/go/core"
)
type RunOpts struct {
PackageName string
RegisterInitializers func(*core.DependencyGraph)
WireServices func(context.Context, Server, core.Dependencies) []error
}
func Run(ctx context.Context, opts RunOpts, listenOpts ListenOpts) {
resources := core.PrepareEnv(opts.PackageName)
defer resources.Close(ctx)
ctx = core.WithResources(ctx, resources)
depgraph := core.NewDependencyGraph()
opts.RegisterInitializers(depgraph)
if err := depgraph.RunInitializers(ctx); err != nil {
core.ZLog.Fatal().Err(err).Send()
}
core.InitializationDone()
if err := Listen(ctx, listenOpts, func(srv Server) {
if errs := opts.WireServices(ctx, srv, depgraph); len(errs) > 0 {
core.ZLog.Fatal().Errs("errors", errs).Msgf("%d services failed to initialize.", len(errs))
}
}); err != nil {
core.ZLog.Fatal().Err(err).Msgf("failed to listen.")
}
}