Functions Directory

Introduction

As discussed earlier, Catalyst supports functions written in Java, Node.js and Python programming environments. The functions folder in the project directory contains all the server-side functions of your microservice or application in individual folders.

An individual function’s folder in the Functions directory contains the main function file (the .java class file or the .js file or .py file), and all the other necessary dependencies and configuration files of the function. You can also create additional secondary functions in the function’s folder that closely work with the main function.

However, you cannot create secondary functions in a function folder from the CLI. You can either manually import the files in the folder from an external development environment, or create them in the Catalyst console and pull them into the directory using the CLI.

Note: You can add any number of functions of Java, Node.js or Python stacks in the functions directory using the CLI or set up the functions component at any time using the CLI. To learn more, refer to the Working with Functions help page .

When you initialize functions from the CLI in the project directory, a folder named ‘functions’ will be created inside the project directory, along with the necessary dependencies based on the programming environment that you select.


A Java function’s folder is set up with the following components when it is created:

  1. The main .java Class File
  2. catalyst-config.json
  3. The .jar Library Files

A Node.js function’s folder is set up with the following components when it is created:

  1. The main function file (index.js)
  2. catalyst-config.json
  3. Node modules and Node.js function dependencies

A Python function’s folder is set up with the following components when it is created:

  1. The main.py file
  2. catalyst-config.json
  3. requirements.txt file

Let us first discuss about the main function file and the catalyst-config.json file that are common in Java, Node.js and Python programming environments.

The Main Function File

The main function file is the file of the type .java or .js or .py , depending on the language that you select while initializing the function. The main function file is created with a sample code structure for Java, Node.js and Python.

For a Java function, you will need to provide the class name of the main function, and the reference name of the function group while initializing it from the CLI. The name of the function folder will be the same as the reference name of the function.

For a Node.js and Python function, you will need to provide the package name of the function. You will also need to provide the name of the entry point file or the main function file, which will be named index.js and main.py by default, respectively.

The catalyst.json file will specify the reference or the package names of all the functions.


catalyst-config json file

The catalyst-config.json file specifies the configuration of the function, such as its deployment and execution specifications.

    
copy
{ "deployment":{ "name": "fn", "stack": "node16", "type": "advancedio", "env_variables": {} }, "execution": { "main": "index.js" } }

deployment information includes the following parameters:

  • name: The reference name of the function group that you provided while initializing it.
    If you modify the reference name of the function, ensure that you update the changes in catalyst-config.json and rename the function folder with the same name. You must reflect the changes in catalyst.json as well.
  • stack: Indicates whether it is a Java, Node.js or Python function and the version information. Changing this value will result in errors.
  • type: Indicates the type of function. To learn more about function types, refer to the Functions help page. Changing this value will result in errors.

execution refers to the main function file which is the entry point, i.e., the name of the main .java, .js or .py function file.

You must ensure that catalyst-config.json file is present in the root of the function folder and contains the right configurations, when you deploy the function.

Let’s now look at the Java library files, Node and Python function dependencies in detail :


The Java Library Folder

The library folder named lib contains the dependent JAR files for the function. The lib folder is added to the function directory automatically when you initialize the function for the Java environment from the CLI.

Project Directory Structure- Java Function Directory

These JAR files are part of the Java SDK package. During the runtime, the Java function calls these library files as they contain packages and features that are required by the function to execute.


Node Modules and Node JS Function Dependencies

A Node.js function’s folder essentially includes the following:

  1. The main function file
  2. catalyst-config.json
  3. package-lock.json
  4. package.json
  5. The Node modules folder

We have previously discussed the main function file and the catalyst-config.json file. The node modules and node function dependencies are installed in your system when you install the Node.js SDK package.

Project Dir Structure- Node.js Directory


The package.json and package-lock.json files are dependency files that contain the configuration of the function. The package.json files specifies information like the name of the function, main function file, version, author and more, whereas the package-lock.json specifies other dependency information in detail.

Note: If you rename the function, you must update the changes in these configuration files.

The node modules folder contains the sub-folders of the modules. Each module contains individual library files, license files, configuration files, index files and more, that are essential for the execution of your Node.js functions.

One of the node modules, zcatalyst-sdk-node, contains the Node.js SDK definitions of all Catalyst components grouped in a folder named src, and other dependency files.

Note:
  • When you create a function from the console, the function group will not contain all the required dependencies. For example, the node modules will not be added to a Node.js function group, although a folder will be created for it. In such cases, you can pull the functions from the console to the project directory using the CLI. This will create a directory for those functions in the required structure. If the function group created in the console contains secondary functions, they will be included in the function’s folder when you pull it from the console.

  • When you create a function from an external editor and import it into Catalyst, you must ensure that all the dependencies are included, and all the configuration files are created in the specified format. If there are any discrepancies, it could result in errors when the function is executed.

Python Modules and Function Dependencies

A Python function’s folder essentially includes the following:

  1. The main.py file
  2. catalyst-config.json
  3. requirements.txt file

We have already discussed about the main function file and the catalyst-config.json file in the previous sections.

requirements.txt file

This requirements.txt file contains the list of installed dependencies that are needed to implement the Python function. By default, it contains the entry for Catalyst’s Python SDK package (zcatalyst-sdk), when you create the Python function from the CLI. When you install external dependencies or libraries for your function, you will have to configure the names and values of the dependencies in the form of key-value pairs manually in the requirements.txt file and save it.

You can also upgrade the version of the Python SDK package or any of the external dependencies by simply configuring the appropriate version values in the requirements.txt file and save it. When you serve or deploy your Python function from the CLI, for the updated configurations to be reflected.

Note: You must configure all the external dependencies that you add in the function's directory in the requirements.txt file mandatorily.

The Python function directory structure is as follows :

Project Dir Structure- Python Directory

Last Updated 2023-06-16 19:42:28 +0530 +0530