-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprout.go
77 lines (66 loc) · 1.92 KB
/
sprout.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
package sprout
import (
"log/slog"
"os"
"github.com/levelfourab/sprout-go/internal"
"github.com/levelfourab/sprout-go/internal/health"
"github.com/levelfourab/sprout-go/internal/logging"
"github.com/levelfourab/sprout-go/internal/otel"
"github.com/levelfourab/sprout-go/internal/runtime"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
"go.uber.org/zap"
"go.uber.org/zap/exp/zapslog"
)
type Sprout struct {
logger *zap.Logger
name string
version string
}
// New creates a new Sprout application. The name and version will be used to
// identify the application in logs, traces and metrics.
func New(name string, version string) *Sprout {
logger, err := logging.CreateRootLogger()
if err != nil {
os.Stderr.WriteString("Unable to bootstrap: " + err.Error() + "\n")
os.Exit(1)
}
zap.ReplaceGlobals(logger)
// Integrate with log/slog
slogLogger := slog.New(zapslog.NewHandler(logger.Core(), nil))
slog.SetDefault(slogLogger)
// Continue bootstrapping
logger.Info("Starting application", zap.String("name", name), zap.String("version", version))
runtime.Setup(logger)
return &Sprout{
logger: logger,
name: name,
version: version,
}
}
// With lets you specify Fx options to be used when creating the application.
func (s *Sprout) With(options ...fx.Option) *fx.App {
logger := s.logger
allOptions := []fx.Option{
fx.WithLogger(func() fxevent.Logger {
return &fxevent.ZapLogger{
Logger: logging.CreateLogger(logger, []string{"fx"}),
}
}),
fx.Supply(internal.ServiceInfo{
Name: s.name,
Version: s.version,
Development: internal.CheckIfDevelopment(),
Testing: false,
}),
logging.Module(logger),
otel.Module,
health.Module,
}
allOptions = append(allOptions, options...)
allOptions = append(allOptions, fx.Invoke(enableHealthServer))
return fx.New(allOptions...)
}
func enableHealthServer(checks Health) {
// Do nothing, only here to make health server always start
}