Skip to content

Feature Flags ​

Use a feature flag to choose when the app shows a new photo layout. Keep the rollout decision in a flag definition so you can change it without rebuilding the app.

Enable Feature Flags ​

In the CLI Project from Quickstart, stop forj dev and add feature to the existing render.components list in .goforj.yml. From the Project root, render the feature manager and generate a command to check the decision.

bash
forj render &&
  forj make:command photos:layout --no-open

Define the Flag ​

Create storage/flags/photos.json with an initially disabled grid layout.

json
{
  "schema": 1,
  "flags": {
    "photo-grid": { "default": false }
  }
}

Select the file source in .env. The path is relative to the process working directory.

dotenv
FEATURE_DRIVER=file
FEATURE_PATH=storage/flags/photos.json

Read the Flag ​

Replace internal/photos/layout_cmd.go with this file. Use your module path from go.mod in place of your/module.

go
package photos

import (
	"context"
	"fmt"

	"your/module/internal/features"
)

// LayoutCmd prints the layout selected for photos.
type LayoutCmd struct {
	features *features.Manager
}

// NewLayoutCmd receives the feature manager from the App.
func NewLayoutCmd(manager *features.Manager) *LayoutCmd {
	return &LayoutCmd{features: manager}
}

// Signature exposes the layout check as a console command.
func (*LayoutCmd) Signature() string {
	return `name:"photos:layout" help:"Show the active photo layout"`
}

// Run prints the layout controlled by the photo-grid flag.
func (c *LayoutCmd) Run(ctx context.Context) error {
	layout := "list"
	if c.features.Default().Enabled(ctx, "photo-grid") {
		layout = "grid"
	}
	fmt.Println(layout)
	return nil
}

Inject the same manager into the service that chooses the layout. Use Default() to select the default evaluator. An undefined boolean flag returns false.

Verify the Decision ​

Build and run the command.

bash
forj build &&
  forj photos:layout
# list

Change photo-grid's default to true in storage/flags/photos.json and run the command again.

bash
forj photos:layout
# grid

Roll Out a Feature ​

Running Apps refresh file sources during their lifecycle. Startup requires a valid file. If a later update is invalid, the evaluator keeps the last valid definitions. Apply rollout rules to a stable user or tenant key. Capture a snapshot when several decisions in one request must use the same definition revision.

Use the Feature library for targeting, typed values, and snapshots. The Environment Reference covers memory sources, named evaluators, and refresh settings. Keep access checks in your authorization policy.