Skip to content

Commit ca5ec32

Browse files
committed
std/go/core: added ability to register package-scoped DebugHandlers.
1 parent d290663 commit ca5ec32

File tree

9 files changed

+163
-41
lines changed

9 files changed

+163
-41
lines changed

std/go/core/checker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func ManualChecker(check CheckerFunc) Checker {
3131
return manualChecker{check}
3232
}
3333

34-
func ProvideLivenessCheck(ctx context.Context, _ *LivenessCheck) (Check, error) {
34+
func ProvideLivenessCheck(ctx context.Context, _ *LivenessCheckArgs) (Check, error) {
3535
return Check{registerLiveness, InstantiationPathFromContext(ctx)}, nil
3636
}
3737

38-
func ProvideReadinessCheck(ctx context.Context, _ *ReadinessCheck) (Check, error) {
38+
func ProvideReadinessCheck(ctx context.Context, _ *ReadinessCheckArgs) (Check, error) {
3939
return Check{registerReadiness, InstantiationPathFromContext(ctx)}, nil
4040
}
4141

std/go/core/coretypes.pb.go

Lines changed: 81 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

std/go/core/coretypes.proto

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ package foundation.std.go.core;
88

99
option go_package = "namespacelabs.dev/foundation/std/go/core";
1010

11-
message LivenessCheck {}
12-
message ReadinessCheck {}
11+
message LivenessCheckArgs {}
12+
message ReadinessCheckArgs {}
13+
message DebugHandlerArgs {}

std/go/core/debughandler.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package core
2+
3+
import (
4+
"context"
5+
"sync"
6+
7+
"github.com/gorilla/mux"
8+
"namespacelabs.dev/foundation/schema"
9+
)
10+
11+
var debugHandlers struct {
12+
mu sync.RWMutex
13+
handlers map[string]*mux.Router
14+
}
15+
16+
type DebugHandler struct {
17+
owner schema.PackageName
18+
}
19+
20+
func (h DebugHandler) Mux() *mux.Router {
21+
debugHandlers.mu.Lock()
22+
defer debugHandlers.mu.Unlock()
23+
24+
if debugHandlers.handlers == nil {
25+
debugHandlers.handlers = map[string]*mux.Router{}
26+
}
27+
28+
if r, ok := debugHandlers.handlers[h.owner.String()]; ok {
29+
return r
30+
}
31+
32+
r := mux.NewRouter()
33+
debugHandlers.handlers[h.owner.String()] = r
34+
return r
35+
}
36+
37+
func ProvideDebugHandler(ctx context.Context, _ *DebugHandlerArgs) (DebugHandler, error) {
38+
return DebugHandler{InstantiationPathFromContext(ctx).Last()}, nil
39+
}

std/go/core/deps.fn.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import (
55
"context"
66
)
77

8-
type _checkProvideLivenessCheck func(context.Context, *LivenessCheck) (Check, error)
8+
type _checkProvideDebugHandler func(context.Context, *DebugHandlerArgs) (DebugHandler, error)
9+
10+
var _ _checkProvideDebugHandler = ProvideDebugHandler
11+
12+
type _checkProvideLivenessCheck func(context.Context, *LivenessCheckArgs) (Check, error)
913

1014
var _ _checkProvideLivenessCheck = ProvideLivenessCheck
1115

12-
type _checkProvideReadinessCheck func(context.Context, *ReadinessCheck) (Check, error)
16+
type _checkProvideReadinessCheck func(context.Context, *ReadinessCheckArgs) (Check, error)
1317

1418
var _ _checkProvideReadinessCheck = ProvideReadinessCheck

std/go/core/endpoints.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ func RegisterDebugEndpoints(mux *mux.Router) {
1515
mux.Handle("/livez", livezEndpoint())
1616
mux.Handle("/readyz", readyzEndpoint())
1717
mux.Handle("/", StatusHandler())
18-
}
18+
19+
debugHandlers.mu.RLock()
20+
defer debugHandlers.mu.RUnlock()
21+
for pkg, handlers := range debugHandlers.handlers {
22+
mux.Handle("/debug/"+pkg+"/", handlers)
23+
}
24+
}

std/go/core/exports.fn.cue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22
package core
33

44
#Exports: {
5+
DebugHandler: {
6+
#Definition: {
7+
packageName: "namespacelabs.dev/foundation/std/go/core"
8+
type: "DebugHandler"
9+
typeDefinition: {
10+
"typename": "foundation.std.go.core.DebugHandlerArgs"
11+
"source": [
12+
"coretypes.proto",
13+
]
14+
}
15+
}
16+
}
517
LivenessCheck: {
618
#Definition: {
719
packageName: "namespacelabs.dev/foundation/std/go/core"
820
type: "LivenessCheck"
921
typeDefinition: {
10-
"typename": "foundation.std.go.core.LivenessCheck"
22+
"typename": "foundation.std.go.core.LivenessCheckArgs"
1123
"source": [
1224
"coretypes.proto",
1325
]
@@ -19,7 +31,7 @@ package core
1931
packageName: "namespacelabs.dev/foundation/std/go/core"
2032
type: "ReadinessCheck"
2133
typeDefinition: {
22-
"typename": "foundation.std.go.core.ReadinessCheck"
34+
"typename": "foundation.std.go.core.ReadinessCheckArgs"
2335
"source": [
2436
"coretypes.proto",
2537
]

std/go/core/extension.cue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ $coreTypesProto: inputs.#Proto & {
1111
extension: fn.#Extension & {
1212
provides: {
1313
LivenessCheck: {
14-
input: $coreTypesProto.types.LivenessCheck
14+
input: $coreTypesProto.types.LivenessCheckArgs
1515
availableIn: {
1616
go: type: "Check"
1717
}
1818
}
1919

2020
ReadinessCheck: {
21-
input: $coreTypesProto.types.ReadinessCheck
21+
input: $coreTypesProto.types.ReadinessCheckArgs
2222
availableIn: {
2323
go: type: "Check"
2424
}
2525
}
26+
27+
DebugHandler: {
28+
input: $coreTypesProto.types.DebugHandlerArgs
29+
availableIn: {
30+
go: type: "DebugHandler"
31+
}
32+
}
2633
}
2734
}
2835

std/go/core/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ func (di *depInitializer) Instantiate(ctx context.Context, ref Reference, f func
7171
isSingleton := ref.Scope == ""
7272
if !ok {
7373
if isSingleton {
74-
return fmt.Errorf("No singleton provider found for package %s.", ref.Package)
74+
return fmt.Errorf("no singleton provider found for package %s", ref.Package)
7575
}
76-
return fmt.Errorf("No provider found for type %s in package %s.", ref.Scope, ref.Package)
76+
return fmt.Errorf("no provider found for type %s in package %s", ref.Scope, ref.Package)
7777
}
7878

7979
var path *InstantiationPath

0 commit comments

Comments
 (0)