Skip to content

Commit 2387943

Browse files
authored
Fb/cds plugin (#21)
* wip 1 * wip 2 * wip 3 * wip 4 * wip 5 * wip 6 * wip 7 * wip 8 * wip 8.1 * wip 8.2 * wip 8.3 * wip 8.4 * wip 8.5 * wip 8.6 * wip 8.7 * wip 8.8 * wip 8.9 * wip 8.10 * wip 8.11 * wip 8.12
1 parent ee8aa12 commit 2387943

14 files changed

+66
-32
lines changed

cds-plugin.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// https://pages.github.tools.sap/cap/docs/node.js/cds-plugins
2+
"use strict";
3+
4+
const cds = require("@sap/cds");
5+
const { initializeFeatures } = require("./src/singleton");
6+
7+
const activate = async () => {
8+
if (cds.env.featureToggles?.config || cds.env.featureToggles?.configFile) {
9+
cds.env.requires = Object.assign(cds.env.requires, {
10+
FeatureService: { model: "@cap-js-community/feature-toggle-library" },
11+
});
12+
13+
if (Array.isArray(cds.env.featureToggles.serviceAccessRoles)) {
14+
cds.on("loaded", (csn) => {
15+
if (csn.definitions.FeatureService) {
16+
csn.definitions.FeatureService["@requires"] = cds.env.featureToggles.serviceAccessRoles;
17+
}
18+
});
19+
}
20+
21+
await initializeFeatures({
22+
config: cds.env.featureToggles.config,
23+
configFile: cds.env.featureToggles.configFile,
24+
});
25+
}
26+
};
27+
28+
module.exports = {
29+
activate,
30+
};

example-cap-server/.cdsrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
"auth": {
44
"kind": "mocked-auth",
55
"users": {
6+
"system": {
7+
"tenant": "system",
8+
"password": "system",
9+
"id": "system-user",
10+
"roles": ["system-user"]
11+
},
612
"alice": {
713
"tenant": "people",
814
"password": "alice",

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
### state
22
GET {{base_url}}/rest/feature/state
3-
Authorization: Basic {{username}} {{password}}
3+
Authorization: Basic system system
44

55
### redis_update | check 1
66
POST {{base_url}}/rest/feature/redisUpdate
7-
Authorization: Basic {{username}} {{password}}
7+
Authorization: Basic system system
88
Content-Type: application/json
99

1010
{
@@ -14,7 +14,7 @@ Content-Type: application/json
1414

1515
### redis_update | check 2
1616
POST {{base_url}}/rest/feature/redisUpdate
17-
Authorization: Basic {{username}} {{password}}
17+
Authorization: Basic system system
1818
Content-Type: application/json
1919

2020
{
@@ -25,7 +25,7 @@ Content-Type: application/json
2525

2626
### redis_update | check 3
2727
POST {{base_url}}/rest/feature/redisUpdate
28-
Authorization: Basic {{username}} {{password}}
28+
Authorization: Basic system system
2929
Content-Type: application/json
3030

3131
{
@@ -37,7 +37,7 @@ Content-Type: application/json
3737

3838
### redis_update | memory 1
3939
POST {{base_url}}/rest/feature/redisUpdate
40-
Authorization: Basic {{username}} {{password}}
40+
Authorization: Basic system system
4141
Content-Type: application/json
4242

4343
{
@@ -47,7 +47,7 @@ Content-Type: application/json
4747

4848
### redis_update | memory 2
4949
POST {{base_url}}/rest/feature/redisUpdate
50-
Authorization: Basic {{username}} {{password}}
50+
Authorization: Basic system system
5151
Content-Type: application/json
5252

5353
{
@@ -57,7 +57,7 @@ Content-Type: application/json
5757

5858
### redis_update | reset
5959
POST {{base_url}}/rest/feature/redisUpdate
60-
Authorization: Basic {{username}} {{password}}
60+
Authorization: Basic system system
6161
Content-Type: application/json
6262

6363
[
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
22
"local": {
3-
"base_url": "http://localhost:4004",
4-
"username": "alice",
5-
"password": "alice"
3+
"base_url": "http://localhost:4004"
64
}
75
}

example-cap-server/package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
"name": "example-cap-server",
44
"version": "1.0.0",
55
"scripts": {
6-
"start": "cds-serve",
7-
"cloc": "npx cloc --vcs=git --read-lang-def=cloc.def ."
6+
"start": "npm run copy-library && npm run serve",
7+
"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",
8+
"serve": "cds-serve",
9+
"cloc": "npx cloc --vcs=git --read-lang-def=cloc.def .",
10+
"upgrade": "npm up --save && npx shx rm -rf node_modules && npm i"
811
},
912
"dependencies": {
10-
"@cap-js-community/feature-toggle-library": "file:..",
11-
"@sap/cds": "^7.0.0",
13+
"@cap-js-community/feature-toggle-library": "*",
14+
"@sap/cds": "^7.1.2",
1215
"express": "^4.18.2"
16+
},
17+
"cds": {
18+
"featureToggles": {
19+
"configFile": "./srv/feature/features.yaml"
20+
}
1321
}
1422
}

example-cap-server/srv/index.cds

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
using from './service/check-service';
2-
using from './service/feature-service';

example-cap-server/srv/server.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
"use strict";
22

33
const cds = require("@sap/cds");
4-
const {
5-
singleton: { initializeFeatures },
6-
} = require("@cap-js-community/feature-toggle-library");
7-
8-
const { FEATURES_FILEPATH } = require("./feature");
94
const { initializeMemoryStatistics } = require("./memoryStatistics");
105

116
cds.on("bootstrap", async () => {
12-
await initializeFeatures({ configFile: FEATURES_FILEPATH });
137
await initializeMemoryStatistics();
148
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@protocol: 'rest'
2-
@impl: './../handler/check-service.js'
2+
@impl: './check-service.js'
33
service CheckService {
44
function priority() returns String;
55
}

index.cds

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
using from './src/service/feature-service';

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "SAP BTP feature toggle library enables Node.js applications using the SAP Cloud Application Programming Model to maintain live-updatable feature toggles via Redis.",
55
"main": "src/index.js",
66
"files": [
7-
"src"
7+
"src",
8+
"cds-plugin.js",
9+
"index.cds"
810
],
911
"scripts": {
1012
"test": "jest",

src/featureToggles.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
"use strict";
1313

1414
// TODO locale for validation messages
15-
// TODO investigate how to turn library into a cds plugin similar to https://github.com/cap-js-community/event-queue
16-
// this would incorporate a ready-made feature-service interface copied from example-cap-server
15+
// TODO custom validation should be configurable in yaml file
1716

1817
const { promisify } = require("util");
1918
const path = require("path");

example-cap-server/srv/service/feature-service.cds renamed to src/service/feature-service.cds

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
@protocol: 'rest'
2-
@impl: './../handler/feature-service.js'
3-
@(requires: 'authenticated-user')
4-
// NOTE: in practice this needs proper security restrictions
2+
@impl: './feature-service.js'
3+
@(requires: ['system-user'])
54
service FeatureService {
65
type JSON {};
76
function state() returns JSON;

example-cap-server/srv/handler/feature-service.js renamed to src/service/feature-service.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"use strict";
22

3-
const {
4-
redis,
5-
singleton: { getFeaturesInfos, refreshFeatureValues, changeFeatureValue },
6-
} = require("@cap-js-community/feature-toggle-library");
73
const cds = require("@sap/cds");
4+
const redis = require("../redisWrapper");
5+
const { getFeaturesInfos, refreshFeatureValues, changeFeatureValue } = require("../singleton");
86

97
const COMPONENT_NAME = "featureService";
108
const VALIDATION_ERROR_HTTP_CODE = 422;

0 commit comments

Comments
 (0)