|
| 1 | +# Feature Flags with Flamingock + Spring Boot + PostgreSQL |
| 2 | + |
| 3 | +A working example that builds a feature-flag service from scratch using [Flamingock](https://www.flamingock.io) to manage database schema evolution, Spring Boot for the REST API, and PostgreSQL as the backing store. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Java 21 (use `sdk env` if you have [SDKMAN](https://sdkman.io/) installed) |
| 8 | +- Docker & Docker Compose |
| 9 | + |
| 10 | +## Quick Start |
| 11 | + |
| 12 | +```bash |
| 13 | +docker compose up --build |
| 14 | +``` |
| 15 | + |
| 16 | +That's it. Postgres starts first (health-checked), then the app boots, Flamingock runs the migrations, and the API is live at `http://localhost:8080`. |
| 17 | + |
| 18 | +### Running locally (without Docker for the app) |
| 19 | + |
| 20 | +```bash |
| 21 | +# Start only Postgres |
| 22 | +docker compose up db -d |
| 23 | + |
| 24 | +# Run the app |
| 25 | +./gradlew bootRun |
| 26 | +``` |
| 27 | + |
| 28 | +## API |
| 29 | + |
| 30 | +### Create a flag |
| 31 | + |
| 32 | +```bash |
| 33 | +curl -s -X POST localhost:8080/flags \ |
| 34 | + -H "Content-Type: application/json" \ |
| 35 | + -d '{"name":"dark-mode","description":"Dark mode UI"}' |
| 36 | +``` |
| 37 | + |
| 38 | +### List all flags |
| 39 | + |
| 40 | +```bash |
| 41 | +curl -s localhost:8080/flags |
| 42 | +``` |
| 43 | + |
| 44 | +### Update a flag (enable + set rollout %) |
| 45 | + |
| 46 | +```bash |
| 47 | +curl -s -X PUT localhost:8080/flags/dark-mode \ |
| 48 | + -H "Content-Type: application/json" \ |
| 49 | + -d '{"enabled":true,"rolloutPercentage":30}' |
| 50 | +``` |
| 51 | + |
| 52 | +### Evaluate a flag for a user |
| 53 | + |
| 54 | +```bash |
| 55 | +curl -s "localhost:8080/flags/evaluate/dark-mode?userId=user-42" |
| 56 | +``` |
| 57 | + |
| 58 | +Evaluation is deterministic — the same `userId` always lands in the same rollout bucket (SHA-256 hash). |
| 59 | + |
| 60 | +### Add a targeting rule |
| 61 | + |
| 62 | +```bash |
| 63 | +curl -s -X POST localhost:8080/flags/dark-mode/rules \ |
| 64 | + -H "Content-Type: application/json" \ |
| 65 | + -d '{"attribute":"plan","operator":"equals","value":"pro"}' |
| 66 | +``` |
| 67 | + |
| 68 | +### Evaluate with attributes |
| 69 | + |
| 70 | +```bash |
| 71 | +curl -s "localhost:8080/flags/evaluate/dark-mode?userId=user-999&plan=pro" |
| 72 | +``` |
| 73 | + |
| 74 | +When a targeting rule matches, the flag is enabled regardless of rollout percentage. |
| 75 | + |
| 76 | +### List rules for a flag |
| 77 | + |
| 78 | +```bash |
| 79 | +curl -s localhost:8080/flags/dark-mode/rules |
| 80 | +``` |
| 81 | + |
| 82 | +## How Flamingock manages the schema |
| 83 | + |
| 84 | +Instead of `ddl-auto` or hand-written SQL scripts, Flamingock applies versioned, auditable changes at startup: |
| 85 | + |
| 86 | +| Change | What it does | |
| 87 | +|--------|-------------| |
| 88 | +| `_0001__CreateFlagsTable` | Creates the `feature_flags` table | |
| 89 | +| `_0002__AddRolloutPercentage` | Adds the `rollout_percentage` column | |
| 90 | +| `_0003__CreateTargetingRules` | Creates the `targeting_rules` table + index | |
| 91 | + |
| 92 | +Each change targets the `postgres-flags` SQL target system and receives a `java.sql.Connection` automatically. Flamingock tracks execution in its audit store so changes run exactly once, even across restarts. |
| 93 | + |
| 94 | +## Targeting rule operators |
| 95 | + |
| 96 | +| Operator | Behaviour | |
| 97 | +|----------|-----------| |
| 98 | +| `equals` | Exact string match | |
| 99 | +| `contains` | Substring match | |
| 100 | +| `in` | Comma-separated list membership | |
| 101 | +| `starts_with` | Prefix match | |
| 102 | + |
| 103 | +## Project structure |
| 104 | + |
| 105 | +``` |
| 106 | +feature-flags/ |
| 107 | +├── docker-compose.yml |
| 108 | +├── Dockerfile |
| 109 | +├── build.gradle |
| 110 | +├── settings.gradle |
| 111 | +└── src/main/java/com/example/flags/ |
| 112 | + ├── FeatureFlagApplication.java # @EnableFlamingock entry point |
| 113 | + ├── config/FlamingockConfig.java # SqlTargetSystem + audit store beans |
| 114 | + ├── changes/ # Flamingock migrations |
| 115 | + ├── model/ # JPA entities |
| 116 | + ├── repository/ # Spring Data repositories |
| 117 | + ├── service/EvaluationService.java # Flag evaluation logic |
| 118 | + └── controller/FlagController.java # REST API |
| 119 | +``` |
0 commit comments