-
Notifications
You must be signed in to change notification settings - Fork 2
fix: add hello-haproxy #69
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5769cea
chore: add hello-haproxy example
cwaldren-ld c37eb73
update Dockerfile
cwaldren-ld 2d8c5ce
run hello-haproxy in CI job
cwaldren-ld dfb01f8
run workflow
cwaldren-ld a3d9d53
try adding -v to curl call
cwaldren-ld 722f3bd
add some validation
cwaldren-ld 4529272
try to make it fail intentionally
cwaldren-ld a147630
revert
cwaldren-ld 4038ab8
remove 'cw' from pull request trigger
cwaldren-ld 0872170
update docs in Dockerfile
cwaldren-ld 006876b
update README with examples
cwaldren-ld 39f3b9a
readme updates
cwaldren-ld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
FROM ubuntu:22.04 | ||
|
||
RUN apt-get update && apt-get install -y \ | ||
curl luarocks lua5.3 lua5.3-dev \ | ||
haproxy apt-transport-https ca-certificates \ | ||
software-properties-common | ||
|
||
RUN add-apt-repository ppa:mhier/libboost-latest && \ | ||
apt-get update && \ | ||
apt-get install -y boost1.81 | ||
|
||
RUN curl https://github.com/launchdarkly/cpp-sdks/releases/download/launchdarkly-cpp-server-v3.3.1/linux-gcc-x64-dynamic.zip -L -o /tmp/sdk.zip && \ | ||
mkdir ./cpp-sdk && \ | ||
unzip /tmp/sdk.zip -d ./cpp-sdk && \ | ||
rm /tmp/sdk.zip | ||
|
||
COPY . . | ||
|
||
RUN luarocks make launchdarkly-server-sdk-2.0.2-0.rockspec LD_DIR=./cpp-sdk/build-dynamic/release | ||
|
||
COPY ./examples/hello-haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg | ||
COPY ./examples/hello-haproxy/service.lua /service.lua | ||
|
||
# The strategy for this Docker example is to download the C++ SDK release artifacts and use those instead of compiling | ||
# from source. This is for example/CI purposes only; generally it's better to build from source to ensure all libraries | ||
# are compatible. | ||
# | ||
# Since we require a newer version of boost than is available in Ubuntu 22.04, we grab it from a PPA (mhier/libboost-latest). | ||
# | ||
# The SDK dynamic libs expect the boost libs to follow a specific naming convention, which isn't what | ||
# the libraries from the PPA follow ('-mt' suffix is added to indicate the libraries are built with multithreading support enabled.) | ||
# | ||
# It's not 100% clear if these libraries are multithread enabled (build logs in the PPA seem to indicate it), | ||
# but even so, the C++ SDK is single-threaded. | ||
# | ||
# To workaround, add symlinks with the expected names. | ||
RUN cd /usr/lib/x86_64-linux-gnu && \ | ||
ln -s libboost_json.so.1.81.0 libboost_json-mt-x64.so.1.81.0 && \ | ||
ln -s libboost_url.so.1.81.0 libboost_url-mt-x64.so.1.81.0 | ||
|
||
CMD haproxy -d -f /etc/haproxy/haproxy.cfg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# LaunchDarkly Lua server-side SDK HAProxy example | ||
|
||
We've built a minimal dockerized example of using the Lua SDK with [HAProxy](https://www.haproxy.org/). For more comprehensive instructions, you can visit the [Using the Lua SDK with HAProxy guide](https://docs.launchdarkly.com/guides/sdk/haproxy) or the [Lua reference guide](https://docs.launchdarkly.com/sdk/server-side/lua). | ||
|
||
## Build instructions | ||
|
||
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 .`. | ||
2. Run the demo with: | ||
``` | ||
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``` | ||
``` | ||
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. | ||
4. Open `localhost:8123` in your browser. Toggle the flag on to see a change in the page (refresh the page.) | ||
|
||
You should receive the message: | ||
> Feature flag is <true/false> for this user. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
global | ||
lua-load /service.lua | ||
|
||
defaults | ||
mode http | ||
timeout connect 10s | ||
timeout client 30s | ||
timeout server 30s | ||
|
||
frontend proxy | ||
bind 0.0.0.0:8123 | ||
use_backend default_backend | ||
|
||
backend default_backend | ||
http-request use-service lua.launchdarkly |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
print "loaded" | ||
|
||
local os = require("os") | ||
local ld = require("launchdarkly_server_sdk") | ||
|
||
|
||
-- Set YOUR_SDK_KEY to your LaunchDarkly SDK key. | ||
local YOUR_SDK_KEY = "" | ||
|
||
-- Set YOUR_FEATURE_KEY to the feature flag key you want to evaluate. | ||
local YOUR_FEATURE_KEY = "my-boolean-flag" | ||
|
||
|
||
-- Allows the LaunchDarkly SDK key to be specified as an environment variable (LD_SDK_KEY) | ||
-- or locally in this example code (YOUR_SDK_KEY). | ||
function get_key_from_env_or(name, existing_key) | ||
if existing_key ~= nil and existing_key ~= "" then | ||
core.Debug("Using LaunchDarkly SDK key from service.lua file") | ||
return existing_key | ||
end | ||
|
||
local env_key = os.getenv("LD_" .. name) | ||
if env_key ~= nil and env_key ~= "" then | ||
core.Debug("Using LaunchDarkly SDK key from LD_" .. name .. " environment variable") | ||
return env_key | ||
end | ||
|
||
core.log(core.crit, "LaunchDarkly SDK key not provided! SDK won't be initialized.") | ||
return "" | ||
end | ||
|
||
local config = {} | ||
local client = ld.clientInit(get_key_from_env_or("SDK_KEY", YOUR_SDK_KEY), 1000, config) | ||
|
||
core.register_service("launchdarkly", "http", function(applet) | ||
applet:start_response() | ||
|
||
local user = ld.makeContext({ | ||
user = { | ||
key = "example-user-key", | ||
name = "Sandy" | ||
} | ||
}) | ||
|
||
if client:boolVariation(user, get_key_from_env_or("FLAG_KEY", YOUR_FEATURE_KEY), false) then | ||
applet:send("<p>Feature flag is true for this user</p>") | ||
else | ||
applet:send("<p>Feature flag is false for this user</p>") | ||
end | ||
end) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably fine, but if it ends up being a problem you could instead just use curl with something like
--retry 10 --retry-connrefused --retry-delay 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's probably more robust.