1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-19 21:28:32 +02:00
ferret/pkg/drivers/driver.go
Tim Voronov d55bce325c
Bugfix/#597 headers panic (#598)
* Remodeled HTTPHeaders

* Remodeled HTTPCookies
2021-03-26 12:01:00 -04:00

75 lines
1.3 KiB
Go

package drivers
import (
"context"
"io"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
type (
ctxKey struct{}
ctxValue struct {
opts *globalOptions
drivers map[string]Driver
}
Driver interface {
io.Closer
Name() string
Open(ctx context.Context, params Params) (HTMLPage, error)
Parse(ctx context.Context, params ParseParams) (HTMLPage, error)
}
)
func WithContext(ctx context.Context, drv Driver, opts ...GlobalOption) context.Context {
ctx, value := resolveValue(ctx)
value.drivers[drv.Name()] = drv
for _, opt := range opts {
opt(drv, value.opts)
}
// 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: &globalOptions{},
drivers: make(map[string]Driver),
}
return context.WithValue(ctx, key, value), value
}
return ctx, value
}