| 1 | package options |
| 2 | |
| 3 | type ApiSettings struct { |
| 4 | Offline bool |
| 5 | FetchBlocks bool |
| 6 | } |
| 7 | |
| 8 | type ApiOption func(*ApiSettings) error |
| 9 | |
| 10 | func ApiOptions(opts ...ApiOption) (*ApiSettings, error) { |
| 11 | options := &ApiSettings{ |
| 12 | Offline: false, |
| 13 | FetchBlocks: true, |
| 14 | } |
| 15 | |
| 16 | return ApiOptionsTo(options, opts...) |
| 17 | } |
| 18 | |
| 19 | func ApiOptionsTo(options *ApiSettings, opts ...ApiOption) (*ApiSettings, error) { |
| 20 | for _, opt := range opts { |
| 21 | err := opt(options) |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | } |
| 26 | return options, nil |
| 27 | } |
| 28 | |
| 29 | type apiOpts struct{} |
| 30 | |
| 31 | var Api apiOpts |
| 32 | |
| 33 | func (apiOpts) Offline(offline bool) ApiOption { |
| 34 | return func(settings *ApiSettings) error { |
| 35 | settings.Offline = offline |
| 36 | return nil |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // FetchBlocks when set to false prevents api from fetching blocks from the |
| 41 | // network while allowing other services such as IPNS to still be online |
| 42 | func (apiOpts) FetchBlocks(fetch bool) ApiOption { |
| 43 | return func(settings *ApiSettings) error { |
| 44 | settings.FetchBlocks = fetch |
| 45 | return nil |
| 46 | } |
| 47 | } |