Skip to content

Commit

Permalink
Merge pull request #123 from suborbital/connor/capability-config
Browse files Browse the repository at this point in the history
Add ability to configure individual capabilities
  • Loading branch information
cohix committed Aug 12, 2021
2 parents bc9f175 + 7fe7ff4 commit 89eb2f8
Show file tree
Hide file tree
Showing 18 changed files with 335 additions and 75 deletions.
2 changes: 1 addition & 1 deletion api/rust/suborbital/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -5,8 +5,8 @@ go 1.16
require (
github.com/google/uuid v1.3.0
github.com/pkg/errors v0.9.1
github.com/suborbital/atmo v0.2.4-0.20210703165749-14c9326b2f7f
github.com/suborbital/grav v0.4.0
github.com/suborbital/atmo v0.3.1-0.20210811161300-cf9b7d3fbb19
github.com/suborbital/grav v0.4.1
github.com/suborbital/vektor v0.4.1
github.com/wasmerio/wasmer-go v1.0.3
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
Expand Down
21 changes: 21 additions & 0 deletions go.sum
Expand Up @@ -81,6 +81,7 @@ github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand All @@ -105,6 +106,7 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down Expand Up @@ -143,9 +145,12 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/nats-io/jwt v1.2.2/go.mod h1:/xX356yQA6LuXI9xWW7mZNpxgF2mBmGecH+Fj34sP5Q=
github.com/nats-io/jwt/v2 v2.0.2/go.mod h1:VRP+deawSXyhNjXmxPCHskrR6Mq50BqpEI5SEcNiGlY=
github.com/nats-io/nats-server/v2 v2.3.2/go.mod h1:dUf7Cm5z5LbciFVwWx54owyCKm8x4/hL6p7rrljhLFY=
github.com/nats-io/nats.go v1.11.1-0.20210623165838-4b75fc59ae30 h1:9GqilBhZaR3xYis0JgMlJjNw933WIobdjKhilXm+Vls=
github.com/nats-io/nats.go v1.11.1-0.20210623165838-4b75fc59ae30/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
github.com/nats-io/nkeys v0.2.0/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s=
github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8=
github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
Expand All @@ -170,6 +175,7 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/schollz/peerdiscovery v1.6.1 h1:2V/Dw+GZY18W6e3yAeUzJouHmIcr9UlWtqsQtpfObGw=
github.com/schollz/peerdiscovery v1.6.1/go.mod h1:bq5/NB9o9/jyEwiW4ubehfToBa2LwdQQMoNiy/vSdYg=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sethvargo/go-envconfig v0.3.0/go.mod h1:XZ2JRR7vhlBEO5zMmOpLgUhgYltqYqq4d4tKagtPUv0=
Expand All @@ -183,9 +189,11 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.1.3 h1:xghbfqPkxzxP3C/f3n5DdpAbdKLj4ZE4BWQI362l53M=
github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -198,24 +206,36 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/suborbital/atmo v0.1.1-0.20210313181308-bc6c9bd4aa04/go.mod h1:JY4mFIOLMi50+5dxTcjuDGcmyEjgnNX6cxKJfX0gWHY=
github.com/suborbital/atmo v0.1.1-0.20210315231600-21e248dacd0f/go.mod h1:pUmfGo+TNrKO1r+LG4njMmrB+FHTyUsqB99Ow64xZ08=
github.com/suborbital/atmo v0.2.3-0.20210521151945-bc9b42cd29c6/go.mod h1:d6OMJ1Lng8vWM1XUvYhGKpW5HbIgOrcbcvyBsw/5gVw=
github.com/suborbital/atmo v0.2.3-0.20210612160203-f809a67f53ea/go.mod h1:pgirs2kVjwkWK0HvrfTyOFEHy/E+XvM5x4w4ENCVWYw=
github.com/suborbital/atmo v0.2.4-0.20210703023811-c300771a336d/go.mod h1:01soR3EqyzdWlyu+zGMsSrkZfKe0mkZCOyCKqDNf78M=
github.com/suborbital/atmo v0.2.4-0.20210703165749-14c9326b2f7f h1:LD8oxWsqD5zP6YRBwmK4lMLTy+ukrijmjhQevBOpr7M=
github.com/suborbital/atmo v0.2.4-0.20210703165749-14c9326b2f7f/go.mod h1:Wt+gmDF17zxf1162QP5V9GhI2Y6x0+7spyPNWCAq63U=
github.com/suborbital/atmo v0.3.1-0.20210810195347-325b9aa81ca7 h1:fJI8E0/KyfuAyitm01u80I/JTHgWo7Ok1Ekz37BEMIQ=
github.com/suborbital/atmo v0.3.1-0.20210810195347-325b9aa81ca7/go.mod h1:fqKzibuuvELT05PdPQhiKPdYuSkQ01ThBYRwhwS/UkQ=
github.com/suborbital/atmo v0.3.1-0.20210811161300-cf9b7d3fbb19 h1:7RlGtf/w0VHKYhYuh2B4YgnbhvXvO1NsXEQoi0ydAR8=
github.com/suborbital/atmo v0.3.1-0.20210811161300-cf9b7d3fbb19/go.mod h1:p8Myr6lgyUiGcGczf9XEhhW1RVFgwnCYGEtZJ+tMSIg=
github.com/suborbital/grav v0.3.0/go.mod h1:PapJ62PtT9dPmW37WaCD+UMhoZiNPp0N9E3nUfEujC4=
github.com/suborbital/grav v0.3.2/go.mod h1:PapJ62PtT9dPmW37WaCD+UMhoZiNPp0N9E3nUfEujC4=
github.com/suborbital/grav v0.3.3/go.mod h1:PapJ62PtT9dPmW37WaCD+UMhoZiNPp0N9E3nUfEujC4=
github.com/suborbital/grav v0.4.0 h1:4znpeqO0x2R1kRJAIE9zdm3H5yiM2DeTtRKQDubJSOM=
github.com/suborbital/grav v0.4.0/go.mod h1:jN+zB9O6ztW2GqruEU46EMOFjvc8K2UDLyofFJWdI8o=
github.com/suborbital/grav v0.4.1 h1:g70gG4EVqNcy5LMII05ayLtxMD8v5M9kBW1BcJFYsC0=
github.com/suborbital/grav v0.4.1/go.mod h1:jN+zB9O6ztW2GqruEU46EMOFjvc8K2UDLyofFJWdI8o=
github.com/suborbital/reactr v0.8.1/go.mod h1:5arEv/OCrzC08bb1x5nbkQ2qnKyU6C9nziSZu/nmi9w=
github.com/suborbital/reactr v0.8.2-0.20210313181650-a56d26f1fabf/go.mod h1:A8nCPLH8didNgORV5z3LII37fwLE6W2Xq2UMOEtGI30=
github.com/suborbital/reactr v0.9.3-0.20210521151323-0a06b6e64358/go.mod h1:64Ui4q6d/NSUS0vJfoqCGXmjW5Q0D5RUY2HcobaPBW0=
github.com/suborbital/reactr v0.9.3-0.20210529212058-25492f1ccb63/go.mod h1:9MS38IUb2AuPHVCb/c+wp2B/Uhbr1bsMEQDDNxRBVyo=
github.com/suborbital/reactr v0.10.1-0.20210703023052-41aca888467c/go.mod h1:eLcALsA1+LKqud1/5LhEX3ltx1r37ZJsXc5NgD5alUU=
github.com/suborbital/reactr v0.10.1-0.20210703165139-b58e147cf44c/go.mod h1:9+nF032WyZX5p+sdeyVctWyaokSDYHDe1tpuItk9Tgc=
github.com/suborbital/reactr v0.11.1-0.20210806212346-26bc1d469301/go.mod h1:ZhWGNCH5boA+jJD9Jf/rthZ7OKb7+zRqiDI7c8lU078=
github.com/suborbital/reactr v0.11.1-0.20210811160758-b715bc22e760/go.mod h1:WKIsmoKHawZoJd7X34xXH7P7i5s+33ln3gCZZIvtwAo=
github.com/suborbital/subo v0.0.18/go.mod h1:bCfpNhqLwemsHK21F9r1BwCNS6A6AeIuwVAlGymSf/8=
github.com/suborbital/vektor v0.2.2/go.mod h1:6YQE7r6t1JcVs3twpqjXDftsLUaTNUk5YorRKHcDamI=
github.com/suborbital/vektor v0.2.3/go.mod h1:6YQE7r6t1JcVs3twpqjXDftsLUaTNUk5YorRKHcDamI=
github.com/suborbital/vektor v0.2.4/go.mod h1:uzSf3LuTDt7omkVgevQLGsGm6PuhWE7JF6zr+kw2PQs=
github.com/suborbital/vektor v0.3.0/go.mod h1:3xIK+UsDed8llTgfMs8aw7GvghYhmaQnCAC3b4Oslog=
github.com/suborbital/vektor v0.4.0/go.mod h1:3xIK+UsDed8llTgfMs8aw7GvghYhmaQnCAC3b4Oslog=
github.com/suborbital/vektor v0.4.1-0.20210528122244-4c6599246636/go.mod h1:3xIK+UsDed8llTgfMs8aw7GvghYhmaQnCAC3b4Oslog=
github.com/suborbital/vektor v0.4.1 h1:WHmIxAp0Jepusg+p2z6yLFnlhyJ+FfopQ7MI1Z9GwKw=
github.com/suborbital/vektor v0.4.1/go.mod h1:3xIK+UsDed8llTgfMs8aw7GvghYhmaQnCAC3b4Oslog=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down Expand Up @@ -326,6 +346,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
32 changes: 19 additions & 13 deletions rcap/authentication.go
Expand Up @@ -5,31 +5,33 @@ import (
"strings"
)

// AuthProvider is a provider for various kinds of auth
type AuthProvider interface {
// AuthCapability is a provider for various kinds of auth
type AuthCapability interface {
HeaderForDomain(string) *AuthHeader
}

// AuthHeader is an HTTP header designed to authenticate requests
type AuthHeader struct {
HeaderType string `json:"headerType"`
Value string `json:"value"`
}
// AuthConfig is a config for the default auth provider
type AuthConfig struct {
Enabled bool `json:"enabled" yaml:"enabled"`

// AuthProviderConfig is a config for the default auth provider
type AuthProviderConfig struct {
// Headers is a map between domains and auth header that should be added to requests to those domains
Headers map[string]AuthHeader `json:"headers"`
Headers map[string]AuthHeader `json:"headers" yaml:"headers"`
}

// AuthHeader is an HTTP header designed to authenticate requests
type AuthHeader struct {
HeaderType string `json:"headerType" yaml:"headerType"`
Value string `json:"value" yaml:"value"`
}

type defaultAuthProvider struct {
config *AuthProviderConfig
config AuthConfig

augmentedHeaders map[string]AuthHeader
}

// DefaultAuthProvider creates the default static auth provider
func DefaultAuthProvider(config *AuthProviderConfig) AuthProvider {
func DefaultAuthProvider(config AuthConfig) AuthCapability {
ap := &defaultAuthProvider{
config: config,
augmentedHeaders: map[string]AuthHeader{},
Expand All @@ -40,9 +42,13 @@ func DefaultAuthProvider(config *AuthProviderConfig) AuthProvider {

// HeadersForDomain returns the appropriate auth headers for the given domain
func (ap *defaultAuthProvider) HeaderForDomain(domain string) *AuthHeader {
if !ap.config.Enabled {
return nil
}

header, ok := ap.augmentedHeaders[domain]
if !ok {
if ap.config == nil {
if ap.config.Headers == nil {
return nil
}

Expand Down
25 changes: 22 additions & 3 deletions rcap/cache.go
Expand Up @@ -10,15 +10,21 @@ import (
// ErrCacheKeyNotFound is returned when a non-existent cache key is requested
var ErrCacheKeyNotFound = errors.New("key not found")

// Cache gives Runnables access to a key/value cache
type Cache interface {
// CacheConfig is configuration for the cache capability
type CacheConfig struct {
Enabled bool `json:"enabled" yaml:"enabled"`
}

// CacheCapability gives Runnables access to a key/value cache
type CacheCapability interface {
Set(key string, val []byte, ttl int) error
Get(key string) ([]byte, error)
Delete(key string) error
}

// memoryCache is a "default" cache implementation for Reactr
type memoryCache struct {
config CacheConfig
values map[string]*uniqueVal

lock sync.RWMutex
Expand All @@ -29,8 +35,9 @@ type uniqueVal struct {
val []byte
}

func DefaultCache() Cache {
func DefaultCache(config CacheConfig) CacheCapability {
m := &memoryCache{
config: config,
values: make(map[string]*uniqueVal),
lock: sync.RWMutex{},
}
Expand All @@ -39,6 +46,10 @@ func DefaultCache() Cache {
}

func (m *memoryCache) Set(key string, val []byte, ttl int) error {
if !m.config.Enabled {
return ErrCapabilityNotEnabled
}

m.lock.Lock()
defer m.lock.Unlock()

Expand Down Expand Up @@ -66,6 +77,10 @@ func (m *memoryCache) Set(key string, val []byte, ttl int) error {
}

func (m *memoryCache) Get(key string) ([]byte, error) {
if !m.config.Enabled {
return nil, ErrCapabilityNotEnabled
}

m.lock.RLock()
defer m.lock.RUnlock()

Expand All @@ -78,6 +93,10 @@ func (m *memoryCache) Get(key string) ([]byte, error) {
}

func (m *memoryCache) Delete(key string) error {
if !m.config.Enabled {
return ErrCapabilityNotEnabled
}

m.lock.Lock()
defer m.lock.Unlock()

Expand Down
56 changes: 56 additions & 0 deletions rcap/config.go
@@ -0,0 +1,56 @@
package rcap

import (
"github.com/pkg/errors"
"github.com/suborbital/vektor/vlog"
)

var ErrCapabilityNotEnabled = errors.New("capability is not enabled")

// CapabilityConfig is configuration for a Runnable's capabilities
// NOTE: if any of the individual configs are nil, it will cause a crash,
// but we need to be able to determine if they're set or not, hence the pointers
// we are going to leave capabilities undocumented until we come up with a more elegant solution
type CapabilityConfig struct {
Logger *LoggerConfig `json:"logger,omitempty" yaml:"logger,omitempty"`
HTTP *HTTPConfig `json:"http,omitempty" yaml:"http,omitempty"`
GraphQL *GraphQLConfig `json:"graphql,omitempty" yaml:"graphql,omitempty"`
Auth *AuthConfig `json:"auth,omitempty" yaml:"auth,omitempty"`
Cache *CacheConfig `json:"cache,omitempty" yaml:"cache,omitempty"`
File *FileConfig `json:"file,omitempty" yaml:"file,omitempty"`
RequestHandler *RequestHandlerConfig `json:"requestHandler,omitempty" yaml:"requestHandler,omitempty"`
}

// DefaultCapabilityConfig returns the default all-enabled config (with a default logger)
func DefaultCapabilityConfig() CapabilityConfig {
return DefaultConfigWithLogger(vlog.Default())
}

func DefaultConfigWithLogger(logger *vlog.Logger) CapabilityConfig {
c := CapabilityConfig{
Logger: &LoggerConfig{
Enabled: true,
Logger: logger,
},
HTTP: &HTTPConfig{
Enabled: true,
},
GraphQL: &GraphQLConfig{
Enabled: true,
},
Auth: &AuthConfig{
Enabled: true,
},
Cache: &CacheConfig{
Enabled: true,
},
File: &FileConfig{
Enabled: true,
},
RequestHandler: &RequestHandlerConfig{
Enabled: true,
},
}

return c
}
21 changes: 17 additions & 4 deletions rcap/filesource.go
Expand Up @@ -9,26 +9,39 @@ var (
// StaticFileFunc is a function that returns the contents of a requested file
type StaticFileFunc func(string) ([]byte, error)

// FileSource gives runnables access to various kinds of files
type FileSource interface {
// FileConfig is configuration for the File capability
type FileConfig struct {
Enabled bool `json:"enabled" yaml:"enabled"`

FileFunc StaticFileFunc `json:"-" yaml:"-"`
}

// FileCapability gives runnables access to various kinds of files
type FileCapability interface {
GetStatic(filename string) ([]byte, error)
}

// defaultFileSource grants access to files
type defaultFileSource struct {
config FileConfig
staticFileFunc StaticFileFunc
}

func DefaultFileSource(staticFileFunc StaticFileFunc) FileSource {
func DefaultFileSource(config FileConfig) FileCapability {
d := &defaultFileSource{
staticFileFunc: staticFileFunc,
config: config,
staticFileFunc: config.FileFunc,
}

return d
}

// GetStatic returns a static file
func (d *defaultFileSource) GetStatic(filename string) ([]byte, error) {
if !d.config.Enabled {
return nil, ErrCapabilityNotEnabled
}

if d.staticFileFunc == nil {
return nil, ErrFileFuncNotSet
}
Expand Down
21 changes: 16 additions & 5 deletions rcap/graphql.go
Expand Up @@ -11,19 +11,26 @@ import (
"github.com/pkg/errors"
)

// GraphQLClient is a GraphQL capability for Reactr Modules
type GraphQLClient interface {
Do(auth AuthProvider, endpoint, query string) (*GraphQLResponse, error)
// GraphQLConfig is configuration for the GraphQL capability
type GraphQLConfig struct {
Enabled bool `json:"enabled" yaml:"enabled"`
}

// GraphQLCapability is a GraphQL capability for Reactr Modules
type GraphQLCapability interface {
Do(auth AuthCapability, endpoint, query string) (*GraphQLResponse, error)
}

// defaultGraphQLClient is the default implementation of the GraphQL capability
type defaultGraphQLClient struct {
config GraphQLConfig
client *http.Client
}

// DefaultGraphQLClient creates a GraphQLClient object
func DefaultGraphQLClient() GraphQLClient {
func DefaultGraphQLClient(config GraphQLConfig) GraphQLCapability {
g := &defaultGraphQLClient{
config: config,
client: http.DefaultClient,
}

Expand All @@ -49,7 +56,11 @@ type GraphQLError struct {
Path string `json:"path"`
}

func (g *defaultGraphQLClient) Do(auth AuthProvider, endpoint, query string) (*GraphQLResponse, error) {
func (g *defaultGraphQLClient) Do(auth AuthCapability, endpoint, query string) (*GraphQLResponse, error) {
if !g.config.Enabled {
return nil, ErrCapabilityNotEnabled
}

r := &GraphQLRequest{
Query: query,
Variables: map[string]string{},
Expand Down

0 comments on commit 89eb2f8

Please sign in to comment.