Cloud Functions
Less allows you to easily create and deploy serverless functions with infinite scale.
Creating Cloud Functions
Simply create a folder named functions
in the less
directory and add your functions to it:
mkdir -p less/functions
Here's an example of a sum
function that adds 2 numbers:
mkdir less/functions/sum
- Node.js
- Python
touch less/functions/sum/index.js
exports.process = ({ a, b }) => {
return a + b;
}
touch less/functions/sum/__init__.py
def process(data):
return data['a'] + data['b']
Calling Cloud Functions
Less offers 2 ways to call cloud functions:
- Using the
functions
module in the Less SDK. - Using the Less Functions REST API.
Using the SDK
- Node.js
- Python
Import functions
from @chuva.io/less
to call the cloud function. The function payloads are JSON objects.
const { functions } = require('@chuva.io/less');
const sum_result = await functions.sum({ a: 3, b: 4 });
console.log('Result: ', sum_result);
// Result: 7
Import functions
from less
to call the function and process your payload in order to retrieve the response.
from less import functions
sum_result = functions.sum({ 'a': 3, 'b': 4 })
print('Result:', sum_result)
# Result: 7
Let's create a GET /sum
route that will return the sum of 2 numbers using our sum
cloud function.
mkdir less/apis/demo/sum
- Node.js
- Python
touch less/apis/demo/sum/get.js
const { functions } = require('@chuva.io/less');
exports.process = async (request, response) => {
// Get the values to add from the query parameters.
const { a, b } = request.query;
// Call the cloud function.
const sum_result = await functions.sum({ a, b });
response.body = `The sum is: ${sum_result}`
return response;
};
touch less/apis/demo/sum/get.py
from less import functions
def process(request, response):
a = request['query']['a']
b = request['query']['b']
response['body'] = functions.sum({
'a': int(a),
'b': int(b)
})
return response
Read the Less REST API documentation to learn more.
You can now deploy and call your GET /sum
route to test your sum
cloud function:
- Deploy your changes.
- npx
- npm
- yarn
npx @chuva.io/less-cli deploy my-less-project
less-cli deploy my-less-project
less-cli deploy my-less-project
- Execute your
GET /sum
request.
curl "[FUNCTIONS_URL]/sum?a=1&b=2"
# 3
Using the REST API
When using the Cloud Functions feature, Less automatically creates a Functions REST API for you. You can use the API to call cloud functions from anywhere, making it easier to integrate Less with your existing systems.
- Deploy your function.
- npx
- npm
- yarn
npx @chuva.io/less-cli deploy my-less-project
less-cli deploy my-less-project
less-cli deploy my-less-project
- Retrieve your Functions API URL from your deployment output:
[less-cli] Deployment complete ✅
[less-cli] Resources
[less-cli] - API URLs
[less-cli] - Functions: https://[PROJECT_NAME]-functions.api.eu-0.a83b464c9.less.chuva.cv
[less-cli] 🇨🇻
- Send a message to your
sum
function using thePOST /functions/{function_name}
route.
curl -X POST -d '{"a": 1, "b": 2}' [FUNCTIONS_BASE_URL]/functions/sum
# 3
Tips & Use-Cases
- E.g. Create a Cloud Function in Python and call it in Javascript.
- E.g. Get Python math precision in Javascript code.
- E.g. Wrap your ORMs in Cloud Functions to query your database from anywhere.
- E.g. Export Faker.js functions and use them in Python.
- E.g. Wrap your Shared Modules functions in Cloud Functions to make your code accessibile from anywhere.