⚠️ Warning: This is an example implementation meant to serve as a starting point for integrating LaunchDarkly with Fastly Compute. The code is incomplete and should be used at your own risk. Make sure to:
- Add proper error handling
- Implement security best practices
- Add appropriate logging
- Test thoroughly in your environment
- Follow LaunchDarkly and Fastly's latest best practices
This project demonstrates how to integrate the LaunchDarkly Go SDK with Fastly's Compute Runtime. It showcases a practical implementation of feature flagging in a serverless environment, specifically using Fastly's KV Store for persistent data storage.
This demo application shows how to:
- Initialize the LaunchDarkly Go SDK in Fastly's Compute Runtime
- Use Fastly's KV Store as a persistent data store for LaunchDarkly
- Handle feature flag evaluations in a serverless context
- Maintain feature flag state across requests
The kvdatastore
package provides a custom implementation of LaunchDarkly's data store interface using Fastly's KV Store. This allows feature flag data to persist between requests, improving performance and reducing API calls to LaunchDarkly's servers.
The main application (main.go
) demonstrates:
- Initialization of the LaunchDarkly client with custom configuration
- Context creation with Fastly-specific attributes
- Feature flag evaluation
- JSON response formatting
- Go 1.21 or later
- Fastly CLI
- LaunchDarkly account and SDK key
- Fastly account with Compute@Edge enabled
- Set up your LaunchDarkly SDK key in the environment or update the constant in
main.go
:
const LD_SDK_KEY = "your-sdk-key"
- Configure your Fastly service using the
fastly.toml
file.
- Install dependencies:
go mod download
- Run the application locally:
fastly compute serve
Deploy to Fastly using:
fastly compute publish
- When a request comes in, the application initializes the LaunchDarkly client with custom configuration using Fastly's KV Store.
- The client creates a context with Fastly-specific attributes (POP, region, service version, etc.).
- Feature flags are evaluated using this context.
- Results are returned as JSON responses.
The application returns JSON responses in the following format:
{
"animal": "feature-flag-value",
"context": {
"kind": "multi",
"contexts": [
{
"kind": "user",
"key": "user-123"
},
{
"kind": "fastly-request",
"key": "request-id",
"fastly_service_version": "version",
"fastly_pop": "pop",
"fastly_region": "region",
"fastly_service_id": "service-id"
}
]
},
"reason": {
"kind": "RULE_MATCH",
"ruleIndex": 0,
"ruleId": "rule-id"
},
"serviceVersion": "version"
}
This project is licensed under the MIT License - see the LICENSE file for details.