mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-16 11:37:36 +02:00
957490efec
* #19 Some work on proxy * Fixed linter issue
83 lines
1.6 KiB
Go
83 lines
1.6 KiB
Go
package runtime
|
|
|
|
import (
|
|
"context"
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
"github.com/MontFerret/ferret/pkg/runtime/env"
|
|
"github.com/MontFerret/ferret/pkg/runtime/logging"
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
type (
|
|
Options struct {
|
|
proxy string
|
|
cdp string
|
|
params map[string]core.Value
|
|
logging *logging.Options
|
|
}
|
|
|
|
Option func(*Options)
|
|
)
|
|
|
|
func newOptions() *Options {
|
|
return &Options{
|
|
cdp: "http://0.0.0.0:9222",
|
|
params: make(map[string]core.Value),
|
|
logging: &logging.Options{
|
|
Writer: os.Stdout,
|
|
Level: logging.ErrorLevel,
|
|
},
|
|
}
|
|
}
|
|
|
|
func WithParam(name string, value interface{}) Option {
|
|
return func(options *Options) {
|
|
options.params[name] = values.Parse(value)
|
|
}
|
|
}
|
|
|
|
func WithParams(params map[string]interface{}) Option {
|
|
return func(options *Options) {
|
|
for name, value := range params {
|
|
options.params[name] = values.Parse(value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func WithBrowser(address string) Option {
|
|
return func(options *Options) {
|
|
options.cdp = address
|
|
}
|
|
}
|
|
|
|
func WithProxy(address string) Option {
|
|
return func(options *Options) {
|
|
options.proxy = address
|
|
}
|
|
}
|
|
|
|
func WithLog(writer io.Writer) Option {
|
|
return func(options *Options) {
|
|
options.logging.Writer = writer
|
|
}
|
|
}
|
|
|
|
func WithLogLevel(lvl logging.Level) Option {
|
|
return func(options *Options) {
|
|
options.logging.Level = lvl
|
|
}
|
|
}
|
|
|
|
func (opts *Options) withContext(parent context.Context) context.Context {
|
|
ctx := core.ParamsWith(parent, opts.params)
|
|
ctx = logging.WithContext(ctx, opts.logging)
|
|
ctx = env.WithContext(ctx, env.Environment{
|
|
CDPAddress: opts.cdp,
|
|
ProxyAddress: opts.proxy,
|
|
})
|
|
|
|
return ctx
|
|
}
|