From 67b845c32e7b5541055bad92628e4b2cd7d0e289 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Jan 2021 17:03:58 +0100 Subject: [PATCH] simplified setup --- chinook/.eslintrc | 24 ----------- chinook/README.md | 12 ++---- chinook/package.json | 18 +++++--- chinook/server.js | 97 +++++++++----------------------------------- 4 files changed, 34 insertions(+), 117 deletions(-) delete mode 100644 chinook/.eslintrc diff --git a/chinook/.eslintrc b/chinook/.eslintrc deleted file mode 100644 index 639d13a0..00000000 --- a/chinook/.eslintrc +++ /dev/null @@ -1,24 +0,0 @@ -{ - "extends": "eslint:recommended", - "env": { - "node": true, - "es6": true, - "jest": true - }, - "parserOptions": { - "ecmaVersion": 2017 - }, - "globals": { - "SELECT": true, - "INSERT": true, - "UPDATE": true, - "DELETE": true, - "CREATE": true, - "DROP": true, - "cds": true - }, - "rules": { - "no-console": "off", - "require-atomic-updates": "off" - } -} diff --git a/chinook/README.md b/chinook/README.md index 55e1cadd..ca954f3f 100644 --- a/chinook/README.md +++ b/chinook/README.md @@ -8,7 +8,7 @@ It contains these folders and files, following our recommended project layout: | ---------------- | ------------------------------------ | | `app/` | will contain compiled front bundles | | `app/front/` | contains frontend app on react | -| `app/deployers/` | contains deployment staff | +| `app/deployers/` | contains deployment stuff | | `db/` | your domain models and data go here | | `srv/` | your service models and code go here | | `test/` | your services tests | @@ -19,13 +19,7 @@ It contains these folders and files, following our recommended project layout: ## Development -- At first open a new terminal and run below command. It should create new sqlite source and fill initial data from `db/data`. You can browse database in any sqlite client - -```json -npm run deploy -``` - -- Next, start cds service on 4004 port in watch mode: +- Start cds service on 4004 port in watch mode: ```json cds watch @@ -62,7 +56,7 @@ npm run test ## Deployment -- Make sure you already have hanatrial instance in your cockpit dashboard (SAP Cloud Platform). +- Make sure you already have hana trial instance in your cockpit dashboard (SAP Cloud Platform). Or if you are using hana instance - change it in mta.yaml config file from hanatrial to hana - Change package.json db section diff --git a/chinook/package.json b/chinook/package.json index d43a38d6..b60f50b4 100644 --- a/chinook/package.json +++ b/chinook/package.json @@ -1,13 +1,13 @@ { "name": "@capire/chinook", - "engines": { - "node": "12.18.3" - }, "version": "1.0.0", "description": "A simple CAP project.", "repository": "", "license": "UNLICENSED", "private": true, + "files": [ + "db", "srv", "server.js" + ], "dependencies": { "@sap/cds": "^4.2.8", "bcryptjs": "^2.4.3", @@ -22,7 +22,7 @@ }, "scripts": { "start": "npx cds run", - "deploy": "npx cds deploy --to sqlite:mychinook.db", + "deploy": "npx cds deploy", "test": "npx mocha ../test/chinook.test.js --verbose --timeout 10000" }, "cds": { @@ -31,11 +31,17 @@ }, "requires": { "db": { - "kind": "sql" + "kind": "sql", + "[development]": { + "model": "*", + "credentials": { + "database": "chinook.db" + } + } }, "auth": { "impl": "srv/auth.js" } } } -} +} \ No newline at end of file diff --git a/chinook/server.js b/chinook/server.js index a75d2a1f..6edfc357 100644 --- a/chinook/server.js +++ b/chinook/server.js @@ -1,80 +1,21 @@ -const cds = require("@sap/cds"); +const cds = require("@sap/cds") -const getDurationInMilliseconds = (start) => { - const NS_PER_SEC = 1e9; // convert to nanoseconds - const NS_TO_MS = 1e6; // convert to milliseconds - const diff = process.hrtime(start); - return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS; -}; +// Allow X-origin requests for React app during evelopment +if (cds.env.env === "development") { + cds.on("bootstrap", (app) => app.use((req, res, next) => { + res.header("Access-Control-Allow-Origin", "*") + res.header( + "Access-Control-Allow-Methods", + "GET, PUT, PATCH, POST, DELETE, OPTIONS" + ) + res.header( + "Access-Control-Allow-Headers", + "Origin, X-Requested-With, Content-Type, Accept, Authorization, Accept-Language" + ) + //intercept OPTIONS method + if (req.method === 'OPTIONS') res.sendStatus(200) + else next() + })) +} -const getFormattedDateTime = () => { - let currentDateTime = new Date(); - let formattedDateTime = - currentDateTime.getFullYear() + - "-" + - (currentDateTime.getMonth() + 1) + - "-" + - currentDateTime.getDate() + - " " + - currentDateTime.getHours() + - ":" + - currentDateTime.getMinutes() + - ":" + - currentDateTime.getSeconds(); - return formattedDateTime; -}; - -const corsMiddleware = (req, res, next) => { - res.header("Access-Control-Allow-Origin", "*"); - res.header( - "Access-Control-Allow-Methods", - "GET, PUT, PATCH, POST, DELETE, OPTIONS" - ); - res.header( - "Access-Control-Allow-Headers", - "Origin, X-Requested-With, Content-Type, Accept, Authorization, Accept-Language" - ); - - //intercepts OPTIONS method - if ("OPTIONS" === req.method) { - //respond with 200 - res.sendStatus(200); - } else { - //move on - next(); - } -}; - -// handle bootstrapping events... -cds.on("bootstrap", (app) => { - if (cds.env.env === "development") { - app.use(corsMiddleware); - } -}); -cds.on("served", async ({ db, messaging, ...servedServices }) => { - // add logging current user before any request - for (let i in servedServices) { - servedServices[i].prepend((srv) => { - srv.on("*", async (req, next) => { - const method = req._.req.method; - const url = req._.req.url; - const start = process.hrtime(); - - if (req.user) { - console.log("[USER]:", req.user.id, req.user.attr, req.user._roles); - } - const result = await next(); - - const status = req._.res.statusCode; - const currentDateTime = getFormattedDateTime(); - const durationInMilliseconds = getDurationInMilliseconds(start); - const log = `[${currentDateTime}] ${durationInMilliseconds.toLocaleString()} ms ${method}:${url} status: ${status} `; - console.log(log); - - return result; - }); - }); - } -}); - -module.exports = cds.server; +module.exports = cds.server