Technical Articles
3 Developer’s hacks – Auto Start CF Login, Custom APIs in CDS, CAPM in GCP Deployment
Intro
Hello there. Hope you are doing well.
In this blog, I am going to share some methods which might be easen up deployments and developments in CAPM – “The SAP Cloud Application Programming Model is a framework of languages, libraries, and tools for building enterprise-grade services and applications”
Readers, please make a note that, this is not related to the actual CAPM tutorial. You must have some prior understanding of these – VS Code, SCP-CF, NodeJS, and CAPM.
What you are getting here:
- Auto CF Login from your Windows PC.
- how to create a custom API in SAP CDS – Build Custom API parallel to CDS Services
- Deploy your CAPM in GCP ie Deploy your CAP in the GCP APP Engine. | SAP CDS on GCP
Let’s do it
Here is –
Git-Link.
Linked-In
1. Auto Start CF Login for Windows – Git-Rep
.bat file is a special text file where you can run multiple commands sequentially. This helps to automate several processes. Now its turn to change your life a lot…????
Just kidding. It’s not – you still have to code.??
CF-CLI is an easy way to push your code in SCP-CF.
Create a .bat file as – (https://github.com/sabarna17/sample-capm-gcp-vm/blob/main/cf%20bat/cflogin.bat)
To make the login as an auto-start need to create a shortcut of the bat file and save the shortcut in the following path:
C:\Users{user}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Vola.
Restart and Check now. While switching on your Windows machine, CF will be automatically logged in every time.
You can also use these .bat files to automate and run your multiple and complex deployments in a go. So start batting.??
2.Custom NodeJS API with CDS Service? – Git Rep
While creating a CAP project have you tried to write a freehand NodeJS and deploy parallelly to the CDS Services? If the answer is yes, please mention your strategy commenting about it.
What you are getting here is :
You can create a CDS Service and write some NodeJS scripts and enhance the existing modules.
What I thought about CDS:
- The custom output structure is hard to expose.
- Free custom SQLite syntax can not be executed
- Full Control along with the API
Solution:
Create a CDS Project and deploy your CDS Tables to Sqlite.
The below technique is just a way to enhance the way we can write code in NodeJS
- Create a myCustAPI.js file as given here myCustAPI.js
module.exports = { custAPI: function (app) { const express = require('express'); const bodyParser = require('body-parser'); const { urlencoded } = require('body-parser'); var fullpath = __dirname.replace("/\\", '/'); app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static("public")); app.use(bodyParser.json()); app.get("/API_without_CDS", function (request, response) { function_API_without_CDS(request, response); }); return app; } } function function_API_without_CDS(request, response){ var query = request.query; if (query.VAL1 && query.VAL2) { response.json({ 'query': query, 'sample_query': query.VAL1 + query.VAL2 }); } }
- Change the existing CDS ‘server.js’ file – node_modules>@sap>cds>server.js
- To change the file use reference code from server.js file
- Check the changes in the existing server.js – line no – 29,57
Testing from Postman:
You can now execute two below URLs from the postman and test it:
- CDS service – http://localhost:4004/catalog/Books
I know you are a bit confused about the language in “descr” field value for “ID”: “101”. FYI that’s Bengali.
2. Your API – http://localhost:4004/API_without_CDS .
Note:
http://localhost:4004/API_without_CDSThis API accepts two query parameters – VAL1 and VAL2 and returns the concatenation of both.
Moving on to the Last section:
3. Deploy your CAP Project in GCP. Git Rep
Here I have tried to deploy a CDS Project to GCP.
Prerequisite
- GCP account is active. If no, click here and see details – GCP
- You have tested your CAPM-NodeJS code Locally. You can run your CDS services – (http://localhost:4004/) and it works fine.
Deployment Steps
- Install GCP Cli from the below link – GoogleCloudSDKInstaller
- Authenticate to Google Cloud by the below command. While authenticating it might open a browser and ask for Authentication to your GCP
gcloud auth login
- Set your Project for GCP for GCP-Cli
gcloud config set project tidal-analogy-XXXXXXX
- Now we need to change the package(my-bookshop) a bit.
- Create a .yaml file name app.yaml inside the package my-bookshop
runtime: nodejs12 service: my-bookshop
- Your package will look like –
my-bookshop/ app db srv app.yaml package.json package-lock.json myCustAPI.js README.md
- Now let’s deploy with the below command:
gcloud app deploy
- Your API is not being hosted in GCP:
Ta da it got deployed
- You can test now through postman:
Read more
Create a GCP account (https://cloud.google.com/appengine/docs/standard/nodejs/building-app)
Common Error
I tried to deploy several times but getting a strange error – “Internal server error” whenever I was executing Catalog URLs. Then I found a resolution to this. I added a “prestart” command as given in the file: package.json file
Conclusion
I have started to work on CAP and NodeJS recently. So trying out different things about the CAP and here is what I thought might be interesting to you. I try to explore more about the SAP offerings.
Note:
1. If you have any substitutable solution for ‘Hack no 2’, please comment about it.
2. I tried to Mix – ‘Hack no 2’ to ‘Hack no 3’. It didn’t work. Feel free to comment if you have any solution for this.
Thanks for Reading.. ???
Please comment on your thoughts..
Great stuff.
I would like to connect in person or over a call to understand it better.
Would you be kind to share your contact via SAP Community DM [Moderator deleted personal information]
Great stuff.
I would like to connect in person or over a call to understand it better.
Would you be kind to share your contact over SAP Community DM [Moderator deleted personal information]
Would love to know if you've found a better way to add to CDS services without writing to the CDS package file.
Thanks for reading out this blog.
The answer to your question is yes. And there was a very easy way to do that.
Step1 -
you need to create some files under "srv". It has to look like below -
Step2-
Server.js will have some code ->
this above piece of code will help to load whatever you write inside ."/lib.js"
Step3 -
Now you magical code -
You can add a new uri here as similar to - "app.get".
Awesome thank you for that! I figured out that you can override the CDS package server.js by copying it to your project root from the CDS package, then correct linked resources.
But, this is much better. Thank you. I'll refactor.
For anyone else that needs further info into extending server.js, it can be found here: https://cap.cloud.sap/docs/node.js/cds-server#custom-server-js