-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from Flagsmith/release/5.0.0
Release version 5.0.0
- Loading branch information
Showing
97 changed files
with
5,106 additions
and
3,518 deletions.
There are no files selected for viewing
This file contains 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 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,3 @@ | ||
[submodule "src/test/java/com/flagsmith/flagengine/enginetestdata"] | ||
path = src/test/java/com/flagsmith/flagengine/enginetestdata | ||
url = git@github.com:Flagsmith/engine-test-data.git |
This file contains 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 |
---|---|---|
@@ -1,3 +1,23 @@ | ||
# Example Spring Boot Application | ||
# Flagsmith Java Example | ||
|
||
This is just a really simple Spring Boot application that demonstrates a super basic implementation of Flagsmith. | ||
This directory contains a basic Spring Boot application which utilises Flagsmith. To run the example application, you'll | ||
need to go through the following steps: | ||
|
||
1. Create an account, organisation and project on [Flagsmith](https://flagsmith.com) | ||
2. Create a feature in the project called "secret_button" | ||
3. Give the feature a value using the json editor as follows: | ||
|
||
```json | ||
{"colour": "#ababab"} | ||
``` | ||
|
||
|
||
4. Set environment variable `FLAGSMITH_API_KEY` with your Flagsmith API key. | ||
5. If you are facing issues with maven dependency for flagsmith, you may run | ||
`mvn clean install` on the flagsmith-java-client root directory (also ../). | ||
6. Run this project using mvn spring-boot:run. | ||
7. The project is accessible at http://localhost:8080/ | ||
|
||
Now you can play around with the 'secret_button' feature in flagsmith, turn it on to show it and edit the colour in the | ||
json value to edit the colour of the button. You can also identify as a given user and then update the settings for the | ||
secret button feature for that user in the flagsmith interface to see the affect that has too. |
This file contains 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 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 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 @@ | ||
package com.flagsmith; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class FontColour { | ||
@JsonProperty("colour") | ||
private String colour; | ||
|
||
public String getColour() { | ||
return colour; | ||
} | ||
|
||
public void setColour(String colour) { | ||
this.colour = colour; | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,19 +1,98 @@ | ||
package com.flagsmith; | ||
|
||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.MapperFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.databind.node.TextNode; | ||
import com.flagsmith.exceptions.FlagsmithApiError; | ||
import com.flagsmith.exceptions.FlagsmithClientError; | ||
import com.flagsmith.models.DefaultFlag; | ||
import com.flagsmith.models.Flags; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
@RestController | ||
@Controller | ||
public class HelloController { | ||
private static volatile ObjectMapper mapper = null; | ||
|
||
private static FlagsmithClient flagsmith = FlagsmithClient.newBuilder() | ||
.setApiKey("NowEDzKzNJXZVTVanLVdMQ") | ||
private static FlagsmithClient flagsmith = FlagsmithClient | ||
.newBuilder() | ||
.setDefaultFlagValueFunction(HelloController::defaultFlagHandler) | ||
.setApiKey(System.getenv("FLAGSMITH_API_KEY")) | ||
.build(); | ||
|
||
@RequestMapping("/") | ||
public String index() { | ||
String fontSize = flagsmith.getFeatureFlagValue("font_size"); | ||
@GetMapping("") | ||
@ResponseBody | ||
public ModelAndView index( | ||
@RequestParam(name = "identifier", defaultValue = "") String identifier, | ||
@RequestParam(name = "trait-key", defaultValue = "") String traitKey, | ||
@RequestParam(name = "trait-value", defaultValue = "") String traitValue | ||
) throws FlagsmithApiError, FlagsmithClientError { | ||
String featureName = "secret_button"; | ||
Flags flags; | ||
|
||
return "Greetings from Spring Boot! Font size is " + fontSize; | ||
if (!StringUtils.isBlank(identifier)) { | ||
Map<String, Object> traits = new HashMap<String, Object>(); | ||
if (!StringUtils.isBlank(traitKey) && !StringUtils.isBlank(traitValue)) { | ||
traits.put(traitKey, traitValue); | ||
} | ||
flags = flagsmith.getIdentityFlags(identifier, traits); | ||
} else { | ||
flags = flagsmith.getEnvironmentFlags(); | ||
} | ||
|
||
Boolean showButton = flags.isFeatureEnabled(featureName); | ||
Object value = flags.getFeatureValue(featureName); | ||
String buttonValue = value instanceof String ? (String) value : ((TextNode) value).textValue(); | ||
|
||
FontColour fontColor = parse(buttonValue, FontColour.class); | ||
|
||
ModelAndView view = new ModelAndView(); | ||
view.setViewName("index"); | ||
view.addObject("show_button", showButton); | ||
view.addObject("font_colour", fontColor.getColour()); | ||
return view; | ||
} | ||
|
||
private static DefaultFlag defaultFlagHandler(String featureName) { | ||
DefaultFlag flag = new DefaultFlag(); | ||
flag.setEnabled(Boolean.FALSE); | ||
|
||
if (featureName.equals("secret_button")) { | ||
flag.setValue("{\"colour\": \"#ababab\"}"); | ||
} else { | ||
flag.setValue(null); | ||
} | ||
|
||
return flag; | ||
} | ||
|
||
private static ObjectMapper getMapper() { | ||
if (null == mapper) { | ||
mapper = new ObjectMapper(); | ||
mapper.configure(MapperFeature.USE_ANNOTATIONS, true); | ||
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false); | ||
} | ||
|
||
return mapper; | ||
} | ||
|
||
private <T> T parse(String data, Class<T> clazz) { | ||
try { | ||
JsonNode json = getMapper().readTree(data); | ||
return getMapper().treeToValue(json, clazz); | ||
} catch (JsonProcessingException e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains 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,36 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org" lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Flagsmith Test</title> | ||
<script src="https://code.jquery.com/jquery-3.6.0.min.js" | ||
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" | ||
crossorigin="anonymous"></script> | ||
</head> | ||
<body> | ||
<p> | ||
Hello <span id="identifier-name" th:text="${identifier} ? identifier : 'World'">World!</span>, | ||
</p> | ||
<button | ||
id="secret-button" | ||
th:if="${show_button}" | ||
th:style="'color: ' + @{${font_colour}} + ';'" | ||
>Secret Button</button> | ||
<p> | ||
<form id="form" method="get"> | ||
<h3>Identify as a user</h3> | ||
<label for="identifier">Identifier: </label> | ||
<input name="identifier" id="identifier"/> | ||
<br /> | ||
<p>... with an optional user trait</p> | ||
<label for="trait-key">Trait Key: </label> | ||
<input name="trait-key" id="trait-key"/><br /> | ||
|
||
<label for="trait-value">Trait Value: </label> | ||
<input name="trait-value" id="trait-value"/><br /> | ||
|
||
<button type="submit">Identify!</button> | ||
</form> | ||
</p> | ||
</body> | ||
</html> |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.