1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/drivers/driver.go

77 lines
1.3 KiB
Go
Raw Normal View History

package drivers
import (
"context"
"io"
"time"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
const DefaultTimeout = time.Second * 30
type (
ctxKey struct{}
ctxValue struct {
opts *options
drivers map[string]Driver
}
Driver interface {
io.Closer
Name() string
Open(ctx context.Context, params Params) (HTMLPage, error)
}
)
func WithContext(ctx context.Context, drv Driver, opts ...Option) context.Context {
ctx, value := resolveValue(ctx)
value.drivers[drv.Name()] = drv
for _, opt := range opts {
opt(drv, value.opts)
}
2019-02-21 20:04:38 +02:00
// set first registered driver as a default one
if value.opts.defaultDriver == "" {
value.opts.defaultDriver = drv.Name()
}
return ctx
}
func FromContext(ctx context.Context, name string) (Driver, error) {
_, value := resolveValue(ctx)
if name == "" {
name = value.opts.defaultDriver
}
drv, exists := value.drivers[name]
if !exists {
return nil, core.Error(core.ErrNotFound, name)
}
return drv, nil
}
func resolveValue(ctx context.Context) (context.Context, *ctxValue) {
key := ctxKey{}
v := ctx.Value(key)
value, ok := v.(*ctxValue)
if !ok {
value = &ctxValue{
opts: &options{},
drivers: make(map[string]Driver),
}
return context.WithValue(ctx, key, value), value
}
return ctx, value
}