Skip to content

Commit

Permalink
Fix/build ignore service (#26)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rlindner81 authored Sep 13, 2023
1 parent 54448f5 commit c305a02
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ node_modules/

# temp
temp/

# cds
gen/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock=false
yes=true
16 changes: 10 additions & 6 deletions cds-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ const cds = require("@sap/cds");
const { initializeFeatures } = require("./src/singleton");

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

if (Array.isArray(cds.env.featureToggles.serviceAccessRoles)) {
if (Array.isArray(envFeatureToggles.serviceAccessRoles)) {
cds.on("loaded", (csn) => {
if (csn.definitions.FeatureService) {
csn.definitions.FeatureService["@requires"] = cds.env.featureToggles.serviceAccessRoles;
csn.definitions.FeatureService["@requires"] = envFeatureToggles.serviceAccessRoles;
}
});
}

// TODO for the "cds build" use case, this initialize makes no sense
await initializeFeatures({
config: cds.env.featureToggles.config,
configFile: cds.env.featureToggles.configFile,
config: envFeatureToggles.config,
configFile: envFeatureToggles.configFile,
});
}
};
Expand Down
1 change: 1 addition & 0 deletions example-cap-server/.npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock=false
yes=true
44 changes: 27 additions & 17 deletions example-cap-server/http/feature-service.http
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,69 @@
GET {{base_url}}/rest/feature/state
Authorization: Basic system system

### redis_update | check 1
### redis_update | memory 1
POST {{base_url}}/rest/feature/redisUpdate
Authorization: Basic system system
Content-Type: application/json

{
"key": "/check/priority",
"value": 1
"key": "/memory/logInterval",
"value": 1000
}

### redis_update | check 2
### redis_update | memory 2
POST {{base_url}}/rest/feature/redisUpdate
Authorization: Basic system system
Content-Type: application/json

{
"key": "/check/priority",
"value": 10,
"scope": { "tenant": "people" }
"key": "/memory/logInterval",
"value": 100
}

### redis_update | check 3
### redis_update | memory off
POST {{base_url}}/rest/feature/redisUpdate
Authorization: Basic system system
Content-Type: application/json

{
"key": "/check/priority",
"value": 100,
"scope": { "user": "[email protected]", "tenant": "people" }
"key": "/memory/logInterval",
"value": null
}

### redis_update | check 1
POST {{base_url}}/rest/feature/redisUpdate
Authorization: Basic system system
Content-Type: application/json

{
"key": "/check/priority",
"value": 1
}

### redis_update | memory 1
### redis_update | check 2
POST {{base_url}}/rest/feature/redisUpdate
Authorization: Basic system system
Content-Type: application/json

{
"key": "/memory/logInterval",
"value": 1000
"key": "/check/priority",
"value": 10,
"scope": { "tenant": "people" }
}

### redis_update | memory 2
### redis_update | check 3
POST {{base_url}}/rest/feature/redisUpdate
Authorization: Basic system system
Content-Type: application/json

{
"key": "/memory/logInterval",
"value": 100
"key": "/check/priority",
"value": 100,
"scope": { "user": "[email protected]" }
}


### redis_update | reset
POST {{base_url}}/rest/feature/redisUpdate
Authorization: Basic system system
Expand Down
12 changes: 9 additions & 3 deletions example-cap-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
"start": "npm run copy-library && npm run serve",
"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",
"serve": "cds-serve",
"cloc": "npx cloc --vcs=git --read-lang-def=cloc.def .",
"build": "cds build --production",
"cloc": "npx cloc --vcs=git --exclude-ext=def --read-lang-def=cloc.def .",
"upgrade": "npm up --save && npx shx rm -rf node_modules && npm i"
},
"dependencies": {
"@cap-js-community/feature-toggle-library": "^0.6.9",
"@cap-js-community/feature-toggle-library": "*",
"@sap/cds": "^7.2.0",
"@sap/cds-dk": "^7.2.0",
"express": "^4.18.2"
},
"cds": {
"featureToggles": {
"configFile": "./srv/feature/features.yaml"
"configFile": "./srv/feature/features.yaml",
"serviceAccessRoles": [
"system-user",
"custom-role"
]
}
}
}
1 change: 0 additions & 1 deletion example-cap-server/srv/feature/features.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@
fallbackValue: 0
validations:
- regex: '^\d+$'
- { module: "./srv/feature/validators.js", call: validateTenantScope }
11 changes: 1 addition & 10 deletions example-cap-server/srv/feature/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
"use strict";

const pathlib = require("path");

const FEATURES_FILEPATH = pathlib.join(__dirname, "features.yaml");

const FEATURE = Object.freeze({
module.exports = Object.freeze({
CHECK_API_PRIORITY: "/check/priority",

MEM_STAT_LOG_INTERVAL: "/memory/logInterval",
});

module.exports = {
FEATURES_FILEPATH,
FEATURE,
};
4 changes: 1 addition & 3 deletions example-cap-server/srv/memoryStatistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const {
singleton: { registerFeatureValueChangeHandler, getFeatureValue },
DynamicIntervalController,
} = require("@cap-js-community/feature-toggle-library");
const {
FEATURE: { MEM_STAT_LOG_INTERVAL },
} = require("./feature");
const { MEM_STAT_LOG_INTERVAL } = require("./feature");

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

Expand Down
4 changes: 2 additions & 2 deletions example-cap-server/srv/service/check-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
singleton: { getFeatureValue },
} = require("@cap-js-community/feature-toggle-library");

const { FEATURE } = require("../feature");
const { CHECK_API_PRIORITY } = require("../feature");

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

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

const priorityHandler = async (context) => {
const value = getFeatureValue(FEATURE.CHECK_API_PRIORITY, { user: context.user.id, tenant: context.tenant });
const value = getFeatureValue(CHECK_API_PRIORITY, { user: context.user.id, tenant: context.tenant });
const messages =
value >= HIGH_BOUNDARY
? HIGH_VALUE_RESPONSES
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,12 @@
"cloud-foundry"
],
"author": "Richard Lindner <[email protected]>",
"license": "Apache-2.0"
"license": "Apache-2.0",
"cds": {
"requires": {
"FeatureService": {
"model": "@cap-js-community/feature-toggle-library"
}
}
}
}
2 changes: 1 addition & 1 deletion src/service/feature-service.cds
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@protocol: 'rest'
@impl: './feature-service.js'

@(requires: ['system-user'])
service FeatureService {
type JSON {};
Expand Down

0 comments on commit c305a02

Please sign in to comment.