42 lines
752 B
Go
42 lines
752 B
Go
package diego
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type Router struct {
|
|
mux http.ServeMux
|
|
}
|
|
|
|
type InjectionContext struct{}
|
|
|
|
type InjectionFunction[T any] func(InjectionContext) T
|
|
|
|
type InjectionToken[T any] struct {
|
|
Name string
|
|
InjectionFunc InjectionFunction[T]
|
|
}
|
|
|
|
var injectionTokens map[string]any = make(map[string]any)
|
|
|
|
func Inject[T any](token InjectionToken[T]) T {
|
|
var value = injectionTokens[token.Name]
|
|
|
|
if value == nil {
|
|
value = token.InjectionFunc(InjectionContext{})
|
|
injectionTokens[token.Name] = value
|
|
}
|
|
|
|
var injection, ok = value.(T)
|
|
|
|
if !ok {
|
|
panic(fmt.Sprintf("FATAL ERROR: Tried to inject a value with the wrong type, expected = %T, got = %T", *new(T), value))
|
|
}
|
|
return injection
|
|
|
|
}
|
|
|
|
func (r *Router) Start() {
|
|
}
|