mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Move middleware context handler logic to service (#29605)
* middleware: Move context handler to own service Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> Co-authored-by: Emil Tullsted <sakjur@users.noreply.github.com> Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
This commit is contained in:
45
pkg/registry/di.go
Normal file
45
pkg/registry/di.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/facebookgo/inject"
|
||||
)
|
||||
|
||||
// BuildServiceGraph builds a graph of services and their dependencies.
|
||||
// The services are initialized after the graph is built.
|
||||
func BuildServiceGraph(objs []interface{}, services []*Descriptor) error {
|
||||
if services == nil {
|
||||
services = GetServices()
|
||||
}
|
||||
for _, service := range services {
|
||||
objs = append(objs, service.Instance)
|
||||
}
|
||||
|
||||
serviceGraph := inject.Graph{}
|
||||
|
||||
// Provide services and their dependencies to the graph.
|
||||
for _, obj := range objs {
|
||||
if err := serviceGraph.Provide(&inject.Object{Value: obj}); err != nil {
|
||||
return fmt.Errorf("failed to provide object to the graph: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve services and their dependencies.
|
||||
if err := serviceGraph.Populate(); err != nil {
|
||||
return fmt.Errorf("failed to populate service dependencies: %w", err)
|
||||
}
|
||||
|
||||
// Initialize services.
|
||||
for _, service := range services {
|
||||
if IsDisabled(service.Instance) {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := service.Instance.Init(); err != nil {
|
||||
return fmt.Errorf("service init failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user