Skip to content

Commit 6d1259f

Browse files
authored
chore: add hello-nginx (#91)
Closes #87 . As part of this work, I've also aligned the docs/README style for `hello-haproxy` and `hello-nginx`.
1 parent 6e0f6d0 commit 6d1259f

File tree

14 files changed

+214
-69
lines changed

14 files changed

+214
-69
lines changed

.github/actions/ci/action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ runs:
5555
- name: Run hello-lua-server example
5656
if: ${{ !contains(inputs.lua-version, 'jit') }}
5757
shell: bash
58-
run: ./scripts/interpreter.sh ./examples/hello-lua-server/hello.lua
58+
run: |
59+
cd ./examples/hello-lua-server
60+
lua hello.lua | tee output.txt
61+
grep -F "is false for this user" output.txt || (echo "Expected false evaluation!" && exit 1)
5962
env:
6063
LD_SDK_KEY: "fake-sdk-key"
6164
# Needed because boost isn't installed in default system paths, which is

.github/workflows/ci.yml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,26 @@ jobs:
4242
rockspec: ${{ matrix.package }}
4343

4444

45-
hello-haproxy:
45+
examples:
4646
runs-on: ubuntu-latest
47+
strategy:
48+
fail-fast: false
49+
matrix:
50+
name: ["hello-haproxy", "hello-nginx"]
4751
env:
48-
LD_SDK_KEY: "foo"
52+
LD_SDK_KEY: foo
4953
steps:
5054
- uses: actions/checkout@v4
51-
- name: Build hello-haproxy image
55+
- name: Build ${{ matrix.name }} image
5256
run: |
53-
docker build -t launchdarkly:hello-haproxy -f ./examples/hello-haproxy/Dockerfile .
54-
- name: Run hello-haproxy container in background
57+
docker build -t launchdarkly:${{ matrix.name }} -f ./examples/${{ matrix.name }}/Dockerfile .
58+
- name: Run ${{ matrix.name }} container in background
5559
run: |
56-
docker run -dit --rm --name hello-haproxy -p 8123:8123 --env LD_SDK_KEY="$LD_SDK_KEY" launchdarkly:hello-haproxy
60+
docker run -dit --rm --name ${{ matrix.name }} -p 8123:80 --env LD_SDK_KEY="$LD_SDK_KEY" launchdarkly:${{ matrix.name }}
5761
- name: Evaluate feature flag
5862
run: |
5963
curl --retry 5 --retry-all-errors --retry-delay 1 -s -v http://localhost:8123 | tee response.txt
60-
grep -F "Feature flag is false for this user" response.txt || (echo "Expected false evaluation!" && exit 1)
61-
- name: Stop hello-haproxy container
64+
grep -F "is false for this user" response.txt || (echo "Expected false evaluation!" && exit 1)
65+
- name: Stop ${{ matrix.name }} container
6266
run: |
63-
docker stop hello-haproxy
67+
docker stop ${{ matrix.name }}

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ getting started with using the SDK.
5252
The following examples are available in addition to the usage guide linked above:
5353

5454

55-
| Example | Purpose |
56-
|-------------------------------------------------|-----------------------------------------------------------------------------------------------|
57-
| [hello-lua-server](./examples/hello-lua-server) | Demonstrates basic example of Lua SDK usage from the command line. |
58-
| [hello-haproxy](./examples/hello-haproxy) | Demonstrates usage of the Lua SDK as a [HAproxy](https://www.haproxy.org/) module via Docker. |
55+
| Example | Purpose |
56+
|-------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|
57+
| [hello-lua-server](./examples/hello-lua-server) | Demonstrates basic example of Lua SDK usage from the command line. |
58+
| [hello-haproxy](./examples/hello-haproxy) | Demonstrates usage of the Lua SDK as a [HAproxy](https://www.haproxy.org/) module via Docker. |
59+
| [hello-nginx](./examples/hello-nginx) | Demonstrates usage of the Lua SDK as a [nginx OpenResty](https://github.com/openresty/openresty) module via Docker. |
5960

6061

6162
Before you start using the SDK, you'll need to install it.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- This is a helper function to get a value from an existing variable, if not empty or nil, or otherwise
2+
-- from an environment variable.
3+
function get_from_env_or_default(env_variable_name, local_variable)
4+
if local_variable ~= nil and local_variable ~= "" then
5+
return local_variable
6+
end
7+
8+
local env_var = os.getenv(env_variable_name)
9+
if env_var ~= nil and env_var ~= "" then
10+
return env_var
11+
end
12+
13+
return ""
14+
end
15+
16+
17+
return get_from_env_or_default

examples/hello-haproxy/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ RUN curl https://github.com/launchdarkly/cpp-sdks/releases/download/launchdarkly
2121
COPY . .
2222
COPY ./examples/hello-haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg
2323
COPY ./examples/hello-haproxy/service.lua /service.lua
24+
COPY ./examples/env-helper/get_from_env_or_default.lua /get_from_env_or_default.lua
2425

2526
RUN luarocks make launchdarkly-server-sdk-"${VERSION}"-0.rockspec LD_DIR=./cpp-sdk/build-dynamic/release
2627

examples/hello-haproxy/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ We've built a minimal dockerized example of using the Lua SDK with [HAProxy](htt
44

55
## Build instructions
66

7-
1. On the command line from the root of the repo, build the image from this directory with `docker build -t hello-haproxy -f ./examples/hello-haproxy/Dockerfile .`.
7+
1. On the command line from the **root** of the repo, build the image from this directory with `docker build -t hello-haproxy -f ./examples/hello-haproxy/Dockerfile .`.
88
2. Run the demo with:
9-
```
10-
docker run -it --rm --name hello-haproxy -p 8123:8123 --env LD_SDK_KEY="your-sdk-key" --env LD_FLAG_KEY="my-boolean-flag" hello-haproxy```
9+
```bash
10+
docker run -it --rm --name hello-haproxy -p 8123:8123 --env LD_SDK_KEY="my-sdk-key" --env LD_FLAG_KEY="my-boolean-flag" hello-haproxy
1111
```
1212
3. **Note:** the SDK key and flag key are passed with environment variables into the container. The `LD_FLAG_KEY` should be a boolean-type flag in your environment.
1313
4. Open `localhost:8123` in your browser. Toggle the flag on to see a change in the page (refresh the page.)
1414

1515
You should receive the message:
16-
> Feature flag is <true/false> for this user.
16+
> Feature flag is <true/false> for this user context

examples/hello-haproxy/haproxy.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defaults
88
timeout server 30s
99

1010
frontend proxy
11-
bind 0.0.0.0:8123
11+
bind 0.0.0.0:80
1212
use_backend default_backend
1313

1414
backend default_backend

examples/hello-haproxy/service.lua

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,19 @@ print "loaded"
22

33
local os = require("os")
44
local ld = require("launchdarkly_server_sdk")
5+
local get_from_env_or_default = require("get_from_env_or_default")
56

7+
-- Set MY_SDK_KEY to your LaunchDarkly SDK key.
8+
local MY_SDK_KEY = ""
69

7-
-- Set YOUR_SDK_KEY to your LaunchDarkly SDK key.
8-
local YOUR_SDK_KEY = ""
9-
10-
-- Set YOUR_FEATURE_KEY to the feature flag key you want to evaluate.
11-
local YOUR_FEATURE_KEY = "my-boolean-flag"
12-
13-
14-
-- Allows the LaunchDarkly SDK key to be specified as an environment variable (LD_SDK_KEY)
15-
-- or locally in this example code (YOUR_SDK_KEY).
16-
function get_key_from_env_or(name, existing_key)
17-
if existing_key ~= nil and existing_key ~= "" then
18-
core.Debug("Using LaunchDarkly SDK key from service.lua file")
19-
return existing_key
20-
end
21-
22-
local env_key = os.getenv("LD_" .. name)
23-
if env_key ~= nil and env_key ~= "" then
24-
core.Debug("Using LaunchDarkly SDK key from LD_" .. name .. " environment variable")
25-
return env_key
26-
end
27-
28-
core.log(core.crit, "LaunchDarkly SDK key not provided! SDK won't be initialized.")
29-
return ""
30-
end
10+
-- Set MY_FLAG_KEY to the boolean-type feature flag key you want to evaluate.
11+
local MY_FLAG_KEY = "my-boolean-flag"
3112

3213
local config = {}
33-
local client = ld.clientInit(get_key_from_env_or("SDK_KEY", YOUR_SDK_KEY), 1000, config)
14+
local sdk_key = get_from_env_or_default("LD_SDK_KEY", MY_SDK_KEY)
15+
local client = ld.clientInit(sdk_key, 1000, config)
16+
17+
local flag_key = get_from_env_or_default("LD_FLAG_KEY", MY_FLAG_KEY)
3418

3519
core.register_service("launchdarkly", "http", function(applet)
3620
applet:start_response()
@@ -42,9 +26,9 @@ core.register_service("launchdarkly", "http", function(applet)
4226
}
4327
})
4428

45-
if client:boolVariation(user, get_key_from_env_or("FLAG_KEY", YOUR_FEATURE_KEY), false) then
46-
applet:send("<p>Feature flag is true for this user</p>")
29+
if client:boolVariation(user, flag_key, false) then
30+
applet:send("<p>Feature flag " .. flag_key .. " is true for this user context</p>")
4731
else
48-
applet:send("<p>Feature flag is false for this user</p>")
32+
applet:send("<p>Feature flag " .. flag_key .. " is false for this user context</p>")
4933
end
5034
end)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# LaunchDarkly Lua server-side SDK basic example
2+
3+
First, modify `hello.lua` to inject your SDK key and boolean-type feature flag key.
4+
5+
Then, use the Lua interpreter to run `hello.lua`:
6+
```bash
7+
lua hello.lua
8+
```
9+
10+
If you'd rather use environment variables to specify the SDK key or flag key, set `LD_SDK_KEY` or `LD_FLAG_KEY`.
11+
```bash
12+
LD_SDK_KEY=my-sdk-key LD_FLAG_KEY=my-boolean-flag lua hello.lua
13+
```
14+
15+
The program should output:
16+
> Feature flag is <true/false> for this user context
Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
11
local ld = require("launchdarkly_server_sdk")
2+
local get_from_env_or_default = dofile("../env-helper/get_from_env_or_default.lua")
23

3-
-- Allows the LaunchDarkly SDK key to be specified as an environment variable (LD_SDK_KEY)
4-
-- or locally in this example code (YOUR_SDK_KEY).
5-
function get_key_from_env_or(existing_key)
6-
if existing_key ~= "" then
7-
return existing_key
8-
end
4+
-- Set MY_SDK_KEY to your LaunchDarkly SDK key.
5+
local MY_SDK_KEY = ""
96

10-
local env_key = os.getenv("LD_SDK_KEY")
11-
if env_key ~= "" then
12-
return env_key
13-
end
14-
15-
error("No SDK key specified (use LD_SDK_KEY environment variable or set local YOUR_SDK_KEY)")
16-
end
17-
18-
-- Set YOUR_SDK_KEY to your LaunchDarkly SDK key.
19-
local YOUR_SDK_KEY = ""
20-
21-
-- Set YOUR_FEATURE_KEY to the feature flag key you want to evaluate.
22-
local YOUR_FEATURE_KEY = "my-boolean-flag"
7+
-- Set MY_FLAG_KEY to the boolean-type feature flag key you want to evaluate.
8+
local MY_FLAG_KEY = "my-boolean-flag"
239

2410

2511
local config = {}
2612

27-
local client = ld.clientInit(get_key_from_env_or(YOUR_SDK_KEY), 1000, config)
13+
local sdk_key = get_from_env_or_default("LD_SDK_KEY", MY_SDK_KEY)
14+
local client = ld.clientInit(sdk_key, 1000, config)
2815

2916
local user = ld.makeContext({
3017
user = {
@@ -33,5 +20,6 @@ local user = ld.makeContext({
3320
}
3421
})
3522

36-
local value = client:boolVariation(user, YOUR_FEATURE_KEY, false)
37-
print("feature flag "..YOUR_FEATURE_KEY.." is "..tostring(value).." for this user")
23+
local flag_key = get_from_env_or_default("LD_FLAG_KEY", MY_FLAG_KEY)
24+
local value = client:boolVariation(user, flag_key, false)
25+
print("Feature flag ".. flag_key .." is "..tostring(value).." for this user context")

0 commit comments

Comments
 (0)