Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: automate client installs; update readmes #25

Merged
merged 12 commits into from
Dec 8, 2023
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ HEADER = flipt_engine.h
.DEFAULT_GOAL := help
.PHONY: build go node python ruby help clean

# run cargo build if anything in engine/ changes
build: ## Build the shared engine library
cargo build --release

go: build ## Prepare the go client for building
cp target/release/$(LIB).* target/release/$(HEADER) flipt-client-go/

node: build ## Prepare the node client for building
cp target/release/$(LIB).* flipt-client-node/
node: build ## Build the node client package
cp target/release/$(LIB).* flipt-client-node/ext/
cd flipt-client-node && npm install && npm run build && npm pack

python: build ## Prepare the python client for building
cp target/release/$(LIB).* flipt-client-python/

ruby: build ## Prepare the ruby client for building
ruby: build ## Build the ruby client gem
cp target/release/$(LIB).* flipt-client-ruby/lib/ext/
cd flipt-client-ruby && rake install

clean: ## Clean up build artifacts
rm -rf target
rm -rf flipt-client-go/$(LIB).* flipt-client-go/$(HEADER)
rm -rf flipt-client-node/$(LIB).*
rm -rf flipt-client-node/ext/$(LIB).*
rm -rf flipt-client-node/*.tgz
rm -rf flipt-client-python/$(LIB).*
rm -rf flipt-client-ruby/lib/ext/$(LIB).*
rm -rf flipt-client-ruby/pkg/
Expand Down
3 changes: 1 addition & 2 deletions build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,10 @@ func nodeTests(ctx context.Context, client *dagger.Client, flipt *dagger.Contain
WithDirectory("/app", hostDirectory.Directory("flipt-client-node"), dagger.ContainerWithDirectoryOpts{
Exclude: []string{"./node_modules/"},
}).
WithFile("/app/libfliptengine.so", dynamicLibrary).
WithFile("/app/ext/libfliptengine.so", dynamicLibrary).
WithServiceBinding("flipt", flipt.WithExec(nil).AsService()).
WithEnvVariable("FLIPT_URL", "http://flipt:8080").
WithEnvVariable("FLIPT_AUTH_TOKEN", "secret").
WithEnvVariable("FLIPT_ENGINE_LIB_PATH", "/app/libfliptengine.so").
WithExec([]string{"npm", "install"}).
WithExec([]string{"npm", "test"}).
Sync(ctx)
Expand Down
130 changes: 130 additions & 0 deletions flipt-client-node/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
55 changes: 48 additions & 7 deletions flipt-client-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,64 @@

The `flipt-client-node` directory contains the TypeScript source code for a Flipt evaluation client.

## Instructions
## Installation

To use this client, you can run the following command from the root of the repository:
Currently, to use this client, you'll need to build the dynamic library and the Node package locally and install it. This is a temporary solution until we can figure out a better way to package and distribute the libraries.

```bash
cargo build --release
```
The dynamic library will contain the functionality necessary for the client to make calls to the Flipt engine via FFI. See [flipt-engine](../flipt-engine) for more information on the Flipt engine and FFI.

### Prerequisites

- [Rust](https://www.rust-lang.org/tools/install)
- [Node](https://nodejs.org/en/download/)
- [Make](https://www.gnu.org/software/make/)

### Automated Build

1. Build and copy the dynamic library to the `flipt-client-node` directory. You can do this by running the following command from the root of the repository:

```bash

```bash
make node
```

2. Install the package locally. You can do this by running the following command from the `flipt-client-node` directory:

This should generate a `target/` directory in the root of this repository, which contains the dynamic linking library built for your platform. This dynamic library will contain the functionality necessary for the Node client to make FFI calls. You'll need to set the `FLIPT_ENGINE_LIB_PATH` environment variable depending on your platform::
```bash
npm install -g flipt-client-node-{version}.tgz
```

### Manual Build

1. Build the Rust dynamic library

```bash
cargo build --release
```

This should generate a `target/` directory in the root of this repository, which contains the dynamically linked library built for your platform.

2. You'll need to copy the dynamic library to the `flipt-client-node/ext` directory. This is a temporary solution until we can figure out a better way to package the libraries with the package.

The `path/to/lib` will be the path to the dynamic library which will have the following paths depending on your platform.

- **Linux**: `{REPO_ROOT}/target/release/libfliptengine.so`
- **Windows**: `{REPO_ROOT}/target/release/libfliptengine.dll`
- **MacOS**: `{REPO_ROOT}/target/release/libfliptengine.dylib`

You can then use the client like so:
3. You can then build the package and install it locally. You can do this by running the following command from the `flipt-client-node` directory:

```bash
npm install
npm run build
npm pack
npm install -g flipt-client-node-{version}.tgz
```

## Usage

In your Node code you can import this client and use it as so:

```typescript
import { FliptEvaluationClient } from 'flipt-client-node';
Expand Down
Empty file.
7 changes: 6 additions & 1 deletion flipt-client-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
"types": "dist/index.d.ts",
"author": "",
"license": "MIT",
"files": [
"dist",
"ext"
],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this packages the shared lib in the node module instead of us relying on the env var and for it to exist elsewhere on the host

"scripts": {
"fmt": "prettier --config .prettierrc 'src/**/*.ts' --write",
"lint": "prettier --check src/**/*.ts",
"test": "jest"
"test": "jest",
"build": "tsc"
},
"dependencies": {
"ffi-napi": "^4.0.3",
Expand Down
38 changes: 29 additions & 9 deletions flipt-client-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
import * as ffi from 'ffi-napi';
import { alloc, allocCString, types } from 'ref-napi';
import * as os from 'os';
import {
BooleanResult,
EngineOpts,
EvaluationRequest,
VariantResult
} from './models';

const engineLib = ffi.Library(
process.env.FLIPT_ENGINE_LIB_PATH || 'libfliptengine.so',
{
initialize_engine: ['void *', ['char **', 'string']],
evaluate_variant: ['string', ['void *', 'string']],
evaluate_boolean: ['string', ['void *', 'string']],
destroy_engine: ['void', ['void *']]
}
);
let libfile = '';

switch (os.platform()) {
case 'darwin':
libfile = 'libfliptengine.dylib';
break;
case 'win32':
libfile = 'libfliptengine.dll';
break;
default:
libfile = 'libfliptengine.so';
break;
}

// get absolute path to libfliptengine
if (process.env.FLIPT_ENGINE_LIB_PATH) {
libfile = process.env.FLIPT_ENGINE_LIB_PATH;
} else {
const path = require('path');
libfile = path.join(__dirname, '..', 'ext', libfile);
}

const engineLib = ffi.Library(libfile, {
initialize_engine: ['void *', ['char **', 'string']],
evaluate_variant: ['string', ['void *', 'string']],
evaluate_boolean: ['string', ['void *', 'string']],
destroy_engine: ['void', ['void *']]
});

export class FliptEvaluationClient {
private namespace: string;
Expand Down
52 changes: 43 additions & 9 deletions flipt-client-ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,71 @@

The `flipt-client-ruby` directory contains the Ruby source code for a Flipt evaluation client.

## Instructions
## Installation

To use this client, you can run the following command from the root of the repository:
Currently, to use this client, you'll need to build the dynamic library and the gem locally and install it. This is a temporary solution until we can figure out a better way to package and distribute the libraries.

```bash
cargo build --release
```
The dynamic library will contain the functionality necessary for the client to make calls to the Flipt engine via FFI. See [flipt-engine](../flipt-engine) for more information on the Flipt engine and FFI.

### Prerequisites

- [Rust](https://www.rust-lang.org/tools/install)
- [Ruby](https://www.ruby-lang.org/en/documentation/installation/)
- [Make](https://www.gnu.org/software/make/)

### Automated Build

1. Build and copy the dynamic library to the `flipt-client-ruby/lib/ext` directory. You can do this by running the following command from the root of the repository:

This should generate a `target/` directory in the root of this repository, which contains the dynamically linked library built for your platform. This dynamic library will contain the functionality necessary for the Ruby client to make FFI calls.
```bash

TODO: Currently, you'll need copy the dynamic library to the `flipt-client-ruby/lib/ext` directory. This is a temporary solution until we can figure out a better way to package the libraries with the gem.
```bash
make ruby
```

### Manual Build

1. Build the Rust dynamic library

```bash
cargo build --release
```

This should generate a `target/` directory in the root of this repository, which contains the dynamically linked library built for your platform.

2. You'll need to copy the dynamic library to the `flipt-client-ruby/lib/ext` directory. This is a temporary solution until we can figure out a better way to package the libraries with the gem.

The `path/to/lib` will be the path to the dynamic library which will have the following paths depending on your platform.

- **Linux**: `{REPO_ROOT}/target/release/libfliptengine.so`
- **Windows**: `{REPO_ROOT}/target/release/libfliptengine.dll`
- **MacOS**: `{REPO_ROOT}/target/release/libfliptengine.dylib`

You can then build the gem and install it locally:
3. You can then build the gem and install it locally. You can do this by running the following command from the `flipt-client-ruby` directory:

```bash
rake build
gem install pkg/flipt_client-{version}.gem
```

## Usage

In your Ruby code you can import this client and use it as so:

```ruby
require 'flipt_client'

client = Flipt::EvaluationClient.new() # uses 'default' namespace
# namespace is the first positional argument and is optional here and will have a value of "default" if not specified.
# opts is the second positional argument and is also optional, the structure is:
# {
# "url": "http://localhost:8080",
# "update_interval": 120,
# "auth_token": "secret"
# }
#
# You can replace the url with where your upstream Flipt instance points to, the update interval for how long you are willing
# to wait for updated flag state, and the auth token if your Flipt instance requires it.
client = Flipt::EvaluationClient.new()
resp = client.evaluate_variant({ flag_key: 'buzz', entity_id: 'someentity', context: { fizz: 'buzz' } })

puts resp
Expand Down
Loading