Skip to content

Commit c305a02

Browse files
authored
Fix/build ignore service (#26)
* add cds-dk * better way to say yes * require service in package json * add custom role to example cap server * take care of weird behavior where cds.env is modified between activate and loaded * cleanup features.yaml for example cap server * cleanup feature enum * better order for demo * better cloc * use implicit service impl path
1 parent 54448f5 commit c305a02

File tree

12 files changed

+64
-44
lines changed

12 files changed

+64
-44
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ node_modules/
88

99
# temp
1010
temp/
11+
12+
# cds
13+
gen/

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
package-lock=false
2+
yes=true

cds-plugin.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ const cds = require("@sap/cds");
55
const { initializeFeatures } = require("./src/singleton");
66

77
const activate = async () => {
8-
if (cds.env.featureToggles?.config || cds.env.featureToggles?.configFile) {
9-
cds.env.requires["FeatureService"] = { model: "@cap-js-community/feature-toggle-library" };
8+
const envFeatureToggles = cds.env.featureToggles;
9+
if (envFeatureToggles?.config || envFeatureToggles?.configFile) {
10+
// TODO this is currently done in package.json, because "cds build" ignores it otherwise. However, it should happen
11+
// dynamically.
12+
// cds.env.requires["FeatureService"] = { model: "@cap-js-community/feature-toggle-library" };
1013

11-
if (Array.isArray(cds.env.featureToggles.serviceAccessRoles)) {
14+
if (Array.isArray(envFeatureToggles.serviceAccessRoles)) {
1215
cds.on("loaded", (csn) => {
1316
if (csn.definitions.FeatureService) {
14-
csn.definitions.FeatureService["@requires"] = cds.env.featureToggles.serviceAccessRoles;
17+
csn.definitions.FeatureService["@requires"] = envFeatureToggles.serviceAccessRoles;
1518
}
1619
});
1720
}
1821

22+
// TODO for the "cds build" use case, this initialize makes no sense
1923
await initializeFeatures({
20-
config: cds.env.featureToggles.config,
21-
configFile: cds.env.featureToggles.configFile,
24+
config: envFeatureToggles.config,
25+
configFile: envFeatureToggles.configFile,
2226
});
2327
}
2428
};

example-cap-server/.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
package-lock=false
2+
yes=true

example-cap-server/http/feature-service.http

+27-17
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,69 @@
22
GET {{base_url}}/rest/feature/state
33
Authorization: Basic system system
44

5-
### redis_update | check 1
5+
### redis_update | memory 1
66
POST {{base_url}}/rest/feature/redisUpdate
77
Authorization: Basic system system
88
Content-Type: application/json
99

1010
{
11-
"key": "/check/priority",
12-
"value": 1
11+
"key": "/memory/logInterval",
12+
"value": 1000
1313
}
1414

15-
### redis_update | check 2
15+
### redis_update | memory 2
1616
POST {{base_url}}/rest/feature/redisUpdate
1717
Authorization: Basic system system
1818
Content-Type: application/json
1919

2020
{
21-
"key": "/check/priority",
22-
"value": 10,
23-
"scope": { "tenant": "people" }
21+
"key": "/memory/logInterval",
22+
"value": 100
2423
}
2524

26-
### redis_update | check 3
25+
### redis_update | memory off
2726
POST {{base_url}}/rest/feature/redisUpdate
2827
Authorization: Basic system system
2928
Content-Type: application/json
3029

3130
{
32-
"key": "/check/priority",
33-
"value": 100,
34-
"scope": { "user": "[email protected]", "tenant": "people" }
31+
"key": "/memory/logInterval",
32+
"value": null
3533
}
3634

35+
### redis_update | check 1
36+
POST {{base_url}}/rest/feature/redisUpdate
37+
Authorization: Basic system system
38+
Content-Type: application/json
39+
40+
{
41+
"key": "/check/priority",
42+
"value": 1
43+
}
3744

38-
### redis_update | memory 1
45+
### redis_update | check 2
3946
POST {{base_url}}/rest/feature/redisUpdate
4047
Authorization: Basic system system
4148
Content-Type: application/json
4249

4350
{
44-
"key": "/memory/logInterval",
45-
"value": 1000
51+
"key": "/check/priority",
52+
"value": 10,
53+
"scope": { "tenant": "people" }
4654
}
4755

48-
### redis_update | memory 2
56+
### redis_update | check 3
4957
POST {{base_url}}/rest/feature/redisUpdate
5058
Authorization: Basic system system
5159
Content-Type: application/json
5260

5361
{
54-
"key": "/memory/logInterval",
55-
"value": 100
62+
"key": "/check/priority",
63+
"value": 100,
64+
"scope": { "user": "[email protected]" }
5665
}
5766

67+
5868
### redis_update | reset
5969
POST {{base_url}}/rest/feature/redisUpdate
6070
Authorization: Basic system system

example-cap-server/package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@
66
"start": "npm run copy-library && npm run serve",
77
"copy-library": "npx shx rm -rf node_modules/@cap-js-community/feature-toggle-library && npx shx mkdir -p node_modules/@cap-js-community/feature-toggle-library && npx shx cp -R ../package.json ../index.cds ../cds-plugin.js ../src node_modules/@cap-js-community/feature-toggle-library",
88
"serve": "cds-serve",
9-
"cloc": "npx cloc --vcs=git --read-lang-def=cloc.def .",
9+
"build": "cds build --production",
10+
"cloc": "npx cloc --vcs=git --exclude-ext=def --read-lang-def=cloc.def .",
1011
"upgrade": "npm up --save && npx shx rm -rf node_modules && npm i"
1112
},
1213
"dependencies": {
13-
"@cap-js-community/feature-toggle-library": "^0.6.9",
14+
"@cap-js-community/feature-toggle-library": "*",
1415
"@sap/cds": "^7.2.0",
16+
"@sap/cds-dk": "^7.2.0",
1517
"express": "^4.18.2"
1618
},
1719
"cds": {
1820
"featureToggles": {
19-
"configFile": "./srv/feature/features.yaml"
21+
"configFile": "./srv/feature/features.yaml",
22+
"serviceAccessRoles": [
23+
"system-user",
24+
"custom-role"
25+
]
2026
}
2127
}
2228
}

example-cap-server/srv/feature/features.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@
1414
fallbackValue: 0
1515
validations:
1616
- regex: '^\d+$'
17-
- { module: "./srv/feature/validators.js", call: validateTenantScope }
+1-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
"use strict";
22

3-
const pathlib = require("path");
4-
5-
const FEATURES_FILEPATH = pathlib.join(__dirname, "features.yaml");
6-
7-
const FEATURE = Object.freeze({
3+
module.exports = Object.freeze({
84
CHECK_API_PRIORITY: "/check/priority",
95

106
MEM_STAT_LOG_INTERVAL: "/memory/logInterval",
117
});
12-
13-
module.exports = {
14-
FEATURES_FILEPATH,
15-
FEATURE,
16-
};

example-cap-server/srv/memoryStatistics.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ const {
77
singleton: { registerFeatureValueChangeHandler, getFeatureValue },
88
DynamicIntervalController,
99
} = require("@cap-js-community/feature-toggle-library");
10-
const {
11-
FEATURE: { MEM_STAT_LOG_INTERVAL },
12-
} = require("./feature");
10+
const { MEM_STAT_LOG_INTERVAL } = require("./feature");
1311

1412
const logger = cds.log("memoryStatistics");
1513

example-cap-server/srv/service/check-service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
singleton: { getFeatureValue },
55
} = require("@cap-js-community/feature-toggle-library");
66

7-
const { FEATURE } = require("../feature");
7+
const { CHECK_API_PRIORITY } = require("../feature");
88

99
const LOW_VALUE_RESPONSES = ["hello", "barely made it"];
1010

@@ -15,7 +15,7 @@ const HIGH_VALUE_RESPONSES = ["well done", "full success", "huzzah", "celebratio
1515
const HIGH_BOUNDARY = 100;
1616

1717
const priorityHandler = async (context) => {
18-
const value = getFeatureValue(FEATURE.CHECK_API_PRIORITY, { user: context.user.id, tenant: context.tenant });
18+
const value = getFeatureValue(CHECK_API_PRIORITY, { user: context.user.id, tenant: context.tenant });
1919
const messages =
2020
value >= HIGH_BOUNDARY
2121
? HIGH_VALUE_RESPONSES

package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,12 @@
5353
"cloud-foundry"
5454
],
5555
"author": "Richard Lindner <[email protected]>",
56-
"license": "Apache-2.0"
56+
"license": "Apache-2.0",
57+
"cds": {
58+
"requires": {
59+
"FeatureService": {
60+
"model": "@cap-js-community/feature-toggle-library"
61+
}
62+
}
63+
}
5764
}

src/service/feature-service.cds

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@protocol: 'rest'
2-
@impl: './feature-service.js'
2+
33
@(requires: ['system-user'])
44
service FeatureService {
55
type JSON {};

0 commit comments

Comments
 (0)