Skip to content

Commit 8053770

Browse files
committed
Initial commit
Signed-off-by: kvmw <mshamsi@broadcom.com>
0 parents  commit 8053770

File tree

17 files changed

+2395
-0
lines changed

17 files changed

+2395
-0
lines changed

.github/workflows/ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: ci
2+
3+
on:
4+
push:
5+
branches: ['main']
6+
pull_request:
7+
branches: ['main']
8+
9+
jobs:
10+
check:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: Setup Node.js
18+
uses: actions/setup-node@v4
19+
with:
20+
node-version: '20'
21+
cache: 'npm'
22+
cache-dependency-path: 'package-lock.json'
23+
24+
- name: Install dependencies
25+
run: npm ci
26+
27+
- name: Check Lint
28+
run: npm run check

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Dependency directories
2+
node_modules/
3+
4+
# dotenv environment variable files
5+
.env
6+
.env.*
7+
!.env.development
8+
9+
.idea/

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
dist/

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"semi": true,
3+
"tabWidth": 2,
4+
"singleQuote": true,
5+
"trailingComma": "all",
6+
"printWidth": 80
7+
}

LICENSE

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
SOFTWARE LICENSE AGREEMENT
3+
4+
Copyright (c) VMware LLC. All rights reserved.
5+
6+
You are hereby granted a non-exclusive, worldwide, royalty-free license under VMware LLC’s copyrights to use, copy, modify, and distribute this software in source code or binary form for use in connection with VMware LLC products.
7+
8+
This copyright notice shall be included in all copies or substantial portions of the software.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Config Server Node.js sample
2+
3+
![CI](https://github.com/spring-cloud-services-samples/cook-nodejs/actions/workflows/ci.yml/badge.svg)
4+
5+
Sample Node.js application demonstrating the use of Config Server in Tanzu Platform for Cloud Foundry.
6+
7+
For information on the Config Server product in Tanzu Platform for Cloud Foundry, please [see the documentation](https://techdocs.broadcom.com/us/en/vmware-tanzu/spring/spring-cloud-services-for-cloud-foundry/3-3/scs-tanzu/config-server-index.html).
8+
9+
## Building and Deploying
10+
11+
- Create a Config Server instance:
12+
13+
```
14+
cf create-service p.config-server standard cook-config-server -c '{ "git": { "uri": "https://github.com/spring-cloud-services-samples/cook-config" } }'
15+
```
16+
17+
- Install dependencies:
18+
19+
```
20+
npm install
21+
```
22+
23+
- Push the application:
24+
25+
```
26+
cf push
27+
```
28+
29+
## Trying It Out
30+
31+
Visit `[ROUTE]/restaurant`, where `[ROUTE]` is the route bound to the application. The "special" of the day will be taken from the configuration repository and the value of `cook.special`.
32+
33+
```
34+
$ curl https://cook.apps.example.cf-app.com/restaurant
35+
36+
Today's special is: Cake a la mode
37+
```

cook.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import express from 'express';
2+
import config from './src/config-server.js';
3+
import env from './environment/index.js';
4+
5+
const cook = express();
6+
7+
let properties = {};
8+
config
9+
.load()
10+
.then((data) => {
11+
properties = data;
12+
console.log('Properties loaded successfully');
13+
cook.listen(env.port, '0.0.0.0', () => {
14+
console.log(`Server listening on port ${env.port}`);
15+
});
16+
})
17+
.catch((e) => {
18+
properties = {};
19+
console.error('Error loading properties:', e);
20+
});
21+
22+
cook.get('/restaurant', async (_req, res) => {
23+
res.send(`Today's special is: ${properties['cook.special'] || 'none'}`);
24+
});
25+
26+
cook.get('/restaurant/secret-menu', async (_req, res) => {
27+
res.send(`${properties['secretMenu'] || 'none'}`);
28+
});
29+
30+
cook.get('/restaurant/dessert-menu', async (_req, res) => {
31+
res.send((await config.loadTextResource('dessert.json')) || 'none');
32+
});

environment/.env.development

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
APPLICATION_NAME=cook
2+
PROFILES=development
3+
LABEL=''

environment/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import path from 'path';
2+
import { config } from 'dotenv';
3+
import { fileURLToPath } from 'url';
4+
5+
// Get __dirname equivalent in ESM
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = path.dirname(__filename);
8+
9+
// Load environment variables from .env file based on NODE_ENV
10+
config({
11+
path: path.resolve(
12+
__dirname,
13+
`.env.${process.env.NODE_ENV || 'development'}`,
14+
),
15+
});
16+
17+
export default {
18+
name: process.env.APPLICATION_NAME,
19+
profiles: process.env.PROFILES || 'default',
20+
label: process.env.LABEL || '',
21+
port: process.env.PORT,
22+
};

eslint.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig, globalIgnores } from 'eslint/config';
2+
import globals from 'globals';
3+
import js from '@eslint/js';
4+
import prettier from 'eslint-plugin-prettier/recommended';
5+
6+
export default defineConfig([
7+
prettier,
8+
{ files: ['**/*.js'], languageOptions: { globals: globals.node } },
9+
{ files: ['**/*.js'], plugins: { js }, extends: ['js/recommended'] },
10+
globalIgnores(['node_modules/', 'dist/']),
11+
]);

0 commit comments

Comments
 (0)