Skip to content

Commit

Permalink
feat!: Updated service to use connect (#163)
Browse files Browse the repository at this point in the history
Connect is a family of libraries for building browser and
gRPC-compatible HTTP APIs. It is backwards compatible with gRPC and
allows for all currently listed service providers and their 'variations'
(gRPC, http(s), unix / tcp) to be managed by a single service provider
with a single set of handlers.

Using connect introduces the `connect protocol`, directly compatible
with the `connect-go` andf `connect-web` client libraries. This provides
the benefit avoiding the use of heavy libraries such as `grpc-web` in
the front end applications. It is also fully compatible with gRPC
meaning that any existing clients that match the interface will still
work, thus, providers can still be writen for other languages using pure
grpc clients, without requiring a language specifc conenct library.

Breaking changes: 

The http routes are changed, there is currently no support for providing
'custom' routes. A request which previously will have looked like this:
```
$ curl -X POST "localhost:8013/flags/headerColor/resolve/string" -d '{"email": "[email protected]"}'
{"value":"#0000FF","reason":"TARGETING_MATCH","variant":"blue"}
```
will not look like this: 
```
$ curl -X POST "localhost:8013/schema.v1.Service/ResolveString" -d ''{"flagKey":"headerColor","context":{"email": "[email protected]"}}'' -H "Content-Type: application/json"
{"value":"#0000FF","reason":"TARGETING_MATCH","variant":"blue"}
``` 
This may not be an issue going forwards as these URLs are internal, and
will only requre an update to the provider.
route prefixes can be added to allow for /api/ to be appended to the
front of each route.

There is no longer a `--service-provider` option, this is because all
variations of this flagkey are now available through the single
`connect` service. (there may be argument for keeping this flag and
setting the default to `'connect'`)

Signed-off-by: James-Milligan <[email protected]>
Co-authored-by: Alex Jones <[email protected]>
  • Loading branch information
james-milligan and AlexsJones authored Sep 26, 2022
1 parent 50fe46f commit 828d5c4
Show file tree
Hide file tree
Showing 16 changed files with 402 additions and 875 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ This now provides an accessible http or [https](#https) endpoint for flag evalua
Command:

```sh
curl -X POST "localhost:8013/flags/myBoolFlag/resolve/boolean"
curl -X POST "localhost:8013/schema.v1.Service/ResolveBoolean" -d '{"flagKey":"myBoolFlag","context":{}}' -H "Content-Type: application/json"
```

Result:
Expand All @@ -52,7 +52,7 @@ Result:
Command:

```sh
curl -X POST "localhost:8013/flags/myStringFlag/resolve/string"
curl -X POST "localhost:8013/schema.v1.Service/ResolveString" -d '{"flagKey":"myStringFlag","context":{}}' -H "Content-Type: application/json"
```

Result:
Expand All @@ -68,7 +68,7 @@ Result:
Command:

```sh
curl -X POST "localhost:8013/flags/myIntFlag/resolve/int"
curl -X POST "localhost:8013/schema.v1.Service/ResolveInt" -d '{"flagKey":"myIntFlag","context":{}}' -H "Content-Type: application/json"
```

Result:
Expand All @@ -86,7 +86,7 @@ Result:
Command:

```sh
curl -X POST "localhost:8013/flags/myFloatFlag/resolve/float"
curl -X POST "localhost:8013/schema.v1.Service/ResolveFloat" -d '{"flagKey":"myFloatFlag","context":{}}' -H "Content-Type: application/json"
```

Result:
Expand All @@ -102,7 +102,7 @@ Result:
Command:

```sh
curl -X POST "localhost:8013/flags/myObjectFlag/resolve/object"
curl -X POST "localhost:8013/schema.v1.Service/ResolveObject" -d '{"flagKey":"myObjectFlag","context":{}}' -H "Content-Type: application/json"
```

Result:
Expand All @@ -118,7 +118,7 @@ Result:
Command:

```sh
curl -X POST "localhost:8013/flags/isColorYellow/resolve/boolean" -d '{"color": "yellow"}'
curl -X POST "localhost:8013/schema.v1.Service/ResolveBoolean" -d '{"flagKey":"isColorYellow","context":{"color":"yellow"}}' -H "Content-Type: application/json"
```

Result:
Expand All @@ -136,13 +136,13 @@ A type mismatch error is returned when the resolved value of a flag does not mat
Command:

```sh
curl -X POST "localhost:8013/flags/myBoolFlag/resolve/string"
curl -X POST "localhost:8013/schema.v1.Service/ResolveString" -d '{"flagKey":"myBoolFlag","context":{}}' -H "Content-Type: application/json"
```

Result:

```sh
{"error_code":"TYPE_MISMATCH","reason":"ERROR"}
{"code":"invalid_argument","message":"TYPE_MISMATCH"}
```

<br />
Expand All @@ -154,13 +154,13 @@ The flag not found error is returned when flag key in the request doesn't match
Command:

```sh
curl -X POST "localhost:8013/flags/aMissingFlag/resolve/string"
curl -X POST "localhost:8013/schema.v1.Service/ResolveBoolean" -d '{"flagKey":"aMissingFlag","context":{}}' -H "Content-Type: application/json"
```

Result:

```sh
{"error_code":"FLAG_NOT_FOUND","reason":"ERROR"}
{"code":"not_found","message":"FLAG_NOT_FOUND"}
```

### https
Expand All @@ -172,7 +172,7 @@ When it is desired to use TLS for increased security, flagD can be started with
This enables you to use an upgraded connection for the previous example requests, such as the following:

```
curl -X POST "https://localhost:8013/flags/myBoolFlag/resolve/boolean"
curl -X POST "localhost:8013/schema.v1.Service/ResolveBoolean" -d '{"flagKey":"myBoolFlag","context":{}}' -H "Content-Type: application/json"
// {"value":true,"reason":"DEFAULT","variant":"on"}
```

Expand Down Expand Up @@ -231,7 +231,7 @@ A flag is defined as such:
The rule provided returns `"on"` if `var color == "yellow"` and `"off"` otherwise:

```shell
curl -X POST "localhost:8013/flags/isColorYellow/resolve/boolean" -d '{"color": "yellow"}'
curl -X POST "localhost:8013/schema.v1.Service/ResolveBoolean" -d '{"flagKey":"isColorYellow","context":{"color":"yellow"}}' -H "Content-Type: application/json"
```

returns
Expand All @@ -243,7 +243,7 @@ returns
whereas

```shell
curl -X POST "localhost:8013/flags/isColorYellow/resolve/boolean" -d '{"color": "white"}'
curl -X POST "localhost:8013/schema.v1.Service/ResolveBoolean" -d '{"flagKey":"isColorYellow","context":{"color":"white"}}' -H "Content-Type: application/json"
```

returns
Expand Down
27 changes: 14 additions & 13 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (
)

const (
portFlagName = "port"
serviceProviderFlagName = "service-provider"
socketPathFlagName = "socket-path"
syncProviderFlagName = "sync-provider"
providerArgsFlagName = "sync-provider-args"
evaluatorFlagName = "evaluator"
serverCertPathFlagName = "server-cert-path"
serverKeyPathFlagName = "server-key-path"
uriFlagName = "uri"
bearerTokenFlagName = "bearer-token"
portFlagName = "port"
socketPathFlagName = "socket-path"
syncProviderFlagName = "sync-provider"
providerArgsFlagName = "sync-provider-args"
evaluatorFlagName = "evaluator"
serverCertPathFlagName = "server-cert-path"
serverKeyPathFlagName = "server-key-path"
uriFlagName = "uri"
bearerTokenFlagName = "bearer-token"
corsFlagName = "cors-origin"
)

func init() {
Expand All @@ -34,7 +34,6 @@ func init() {
flags.StringP(socketPathFlagName, "d", "", "Flagd socket path. "+
"With grpc the service will become available on this address. "+
"With http(s) the grpc-gateway proxy will use this address internally.")
flags.StringP(serviceProviderFlagName, "s", "http", "Set a service provider e.g. http or grpc")
flags.StringP(
syncProviderFlagName, "y", "filepath", "Set a sync provider e.g. filepath or remote",
)
Expand All @@ -49,17 +48,18 @@ func init() {
"flags with the same key, the later will be used.")
flags.StringP(
bearerTokenFlagName, "b", "", "Set a bearer token to use for remote sync")
flags.StringSliceP(corsFlagName, "C", []string{}, "CORS allowed origins, * will allow all origins")

_ = viper.BindPFlag(portFlagName, flags.Lookup(portFlagName))
_ = viper.BindPFlag(socketPathFlagName, flags.Lookup(socketPathFlagName))
_ = viper.BindPFlag(serviceProviderFlagName, flags.Lookup(serviceProviderFlagName))
_ = viper.BindPFlag(syncProviderFlagName, flags.Lookup(syncProviderFlagName))
_ = viper.BindPFlag(providerArgsFlagName, flags.Lookup(providerArgsFlagName))
_ = viper.BindPFlag(evaluatorFlagName, flags.Lookup(evaluatorFlagName))
_ = viper.BindPFlag(serverCertPathFlagName, flags.Lookup(serverCertPathFlagName))
_ = viper.BindPFlag(serverKeyPathFlagName, flags.Lookup(serverKeyPathFlagName))
_ = viper.BindPFlag(uriFlagName, flags.Lookup(uriFlagName))
_ = viper.BindPFlag(bearerTokenFlagName, flags.Lookup(bearerTokenFlagName))
_ = viper.BindPFlag(corsFlagName, flags.Lookup(corsFlagName))
}

// startCmd represents the start command
Expand All @@ -78,7 +78,6 @@ var startCmd = &cobra.Command{
}
// Build Runtime -----------------------------------------------------------
rt, err := runtime.FromConfig(runtime.Config{
ServiceProvider: viper.GetString(serviceProviderFlagName),
ServicePort: viper.GetInt32(portFlagName),
ServiceSocketPath: viper.GetString(socketPathFlagName),
ServiceCertPath: viper.GetString(serverCertPathFlagName),
Expand All @@ -90,6 +89,8 @@ var startCmd = &cobra.Command{
SyncBearerToken: viper.GetString(bearerTokenFlagName),

Evaluator: viper.GetString(evaluatorFlagName),

CORS: viper.GetStringSlice(corsFlagName),
})
if err != nil {
log.Error(err)
Expand Down
4 changes: 2 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Supported flags are as follows (result of running `./flagd start --help`):
-p, --port int32 Port to listen on (default 8013)
-c, --server-cert-path string Server side tls certificate path
-k, --server-key-path string Server side tls key path
-s, --service-provider string Set a service provider e.g. http or grpc (default "http")
-a, --sync-provider-args Sync provider arguments as key values separated by =
-d, --socket-path string Set the flagd socket path. With grpc the service will become available on this address. With http(s) the grpc-gateway proxy will use this address internally
-d, --socket-path string Set the flagd socket path.
-y, --sync-provider string Set a sync provider e.g. filepath or remote (default "filepath")
-f, --uri strings Set a sync provider uri to read data from this can be a filepath or url. Using multiple providers is supported where collisions between flags with the same key, the later will be used.
-C, --cors-origin strings Set a CORS allow origin header, setting "*" will allow all origins (by default CORS headers are not set)
```

Environment variable keys are uppercased, prefixed with `FLAGD_` and all `-` are replaced with `_`. For example,
Expand Down
4 changes: 2 additions & 2 deletions docs/fractional_evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ Flags defined as such:
will return variant `red` 50% of the time, `blue` 20% of the time & `green` 30% of the time.

```shell
$ curl -X POST "localhost:8013/flags/headerColor/resolve/string" -d '{"email": "[email protected]"}'
$ curl -X POST "localhost:8013/schema.v1.Service/ResolveString" -d ''{"flagKey":"headerColor","context":{"email": "[email protected]"}}'' -H "Content-Type: application/json"
{"value":"#0000FF","reason":"TARGETING_MATCH","variant":"blue"}%

$ curl -X POST "localhost:8013/flags/headerColor/resolve/string" -d '{"email": "[email protected]"}'
$ curl -X POST "localhost:8013/schema.v1.Service/ResolveString" -d ''{"flagKey":"headerColor","context":{"email": "[email protected]"}}'' -H "Content-Type: application/json"
{"value":"#00FF00","reason":"TARGETING_MATCH","variant":"green"}%
```
4 changes: 2 additions & 2 deletions docs/http_int_response.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Why is my `int` response a `string`?
Command:
```sh
curl -X POST "localhost:8013/flags/myIntFlag/resolve/int"
curl -X POST "localhost:8013/schema.v1.Service/ResolveInt" -d '{"flagKey":"myIntFlag","context":{}}' -H "Content-Type: application/json"
```
Result:
```sh
Expand All @@ -14,7 +14,7 @@ When interacting directly with the flagD http(s) api and requesting an `int` the
<br />
Command:
```sh
curl -X POST "localhost:8013/flags/myIntFlag/resolve/float"
curl -X POST "localhost:8013/schema.v1.Service/ResolveFloat" -d '{"flagKey":"myIntFlag","context":{}}' -H "Content-Type: application/json"
```
Result:
```sh
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ module github.com/open-feature/flagd
go 1.18

require (
github.com/bufbuild/connect-go v0.4.0
github.com/deepmap/oapi-codegen v1.11.0
github.com/diegoholiveira/jsonlogic/v3 v3.2.3
github.com/dimiro1/banner v1.1.0
github.com/fsnotify/fsnotify v1.5.4
github.com/go-chi/chi/v5 v5.0.7
github.com/golang/mock v1.6.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.1
github.com/mattn/go-colorable v0.1.12
github.com/open-feature/open-feature-operator v0.0.10-0.20220826061622-a6421d66936a
github.com/open-feature/schemas v0.0.0-20220809125333-185e3bd77775
github.com/robfig/cron v1.2.0
github.com/rs/cors v1.8.2
github.com/sirupsen/logrus v1.8.1
github.com/soheilhy/cmux v0.1.5
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.7.4
github.com/xeipuuv/gojsonschema v1.2.0
github.com/zeebo/xxh3 v1.0.2
go.buf.build/open-feature/flagd-server/open-feature/flagd v1.1.2
go.buf.build/open-feature/flagd-connect/open-feature/flagd v1.1.3
golang.org/x/net v0.0.0-20220909164309-bea034e7d591
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.28.1
Expand Down Expand Up @@ -73,7 +74,6 @@ require (
github.com/subosito/gotenv v1.3.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect
golang.org/x/oauth2 v0.0.0-20220722155238-128564f6959c // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
Expand Down
Loading

0 comments on commit 828d5c4

Please sign in to comment.