Catalyst Configurations for AppSail Services

Although AppSail does not include many Catalyst-specific configurations or format restrictions, there are certain definitions that are bundled with your app containing information pertaining to hosting and executing the app. These are discussed below.

The app-config.json File

When you initialize an AppSail service from the CLI during project initialization or by adding it, Catalyst creates a configuration file app-config.json based on the input you provide. This file is created in the directory you initialize the app in, otherwise called the source directory.

Note: The app-config.json file is not created when you do a standalone deploy to deploy an app without initializing it. The information that this file carries is fetched as input from you in the CLI during the deployment.

An example of the default code of app-config.json created for a Java app is given below:

    
copy
{ "command": "java -jar demo-0.0.1-SNAPSHOT.jar", "buildPath": "/Users/emma-426/CatalystDirectories/DemoApp", "stack": "java8", "env_variables": { "clientid":xxxxxxx, "clientsecret":xxxxxx}, "memory": 256, "platform": "javase" }

The parameters defined in this configuration file are explained in the next few sections.

Note: Catalyst enables you to select one of the two standard formats of the Java environment: Java SE and Java WAR. These are explained in this section. Therefore, the app-config.json file or configuration definitions for a Java app alone includes the additional parameter platform, which specifies the template. This definition will not be available for Python or Node.js apps.

Source Directory

  • By default, the source directory of your application is the directory that you execute the app initialization or app addition command from.

  • You can also select another directory besides the current directory to be the source during the initialization.

  • You must add the source files of your app in this directory.

  • The app-config.json file gets created in the source directory of your app.

  • The source directory can also be your Catalyst project directory. That is, you can initialize a project in your AppSail service’s root folder.


Build Path

  • The target directory is the directory that will contain the build files of your deployable application.

  • You can specify the target directory of your app through the parameter build-path when you initialize or add the app.

  • When you do a standalone deploy, the current directory will be the build directory.

  • Depending on the programming environment, the frameworks, and dependencies you build your app with, your deployable app could be a single file or multiple files. Some example file formats are discussed in this section.

  • You need not bundle or zip all the build files together, as AppSail will automatically bundle them during the application serve or deployment from the build-path specified.

  • You can name the main file of your app’s build anything you prefer. However, when you build your app as a Java WAR file, you will need to name the main file root.war. This is to ensure further configurations are prevented from your end when the endpoint URL is generated for your application. If you name your WAR file anything else, you will need to add some necessary controllers in your code accordingly.


Catalyst SDK for AppSail

Catalyst enables you to implement the SDK packages of the supported development environments in your AppSail applications. This enables you to avail other Catalyst services and components in your app’s functionality. You can implement the development SDKs of various programming environments in your app as specified below.

Implementing Catalyst Java SDK in AppSail

You can download the Catalyst Java SDK package from the Developer Tools settings in your Catalyst console and include it in your app’s source code. You can then implement the Catalyst Java SDK in your application’s code and initialize it. Refer Catalyst Java SDK help for details about the various functionalities of the SDK toolkit and sample code snippets.

The steps to implement and initialize the Catalyst SDK for different API versions of Java servlets are demonstrated with sample codes below.

In all cases, Catalyst requires you to implement the AuthHeaderProvider interface from the Catalyst Java SDK package. The implementation defines the getHeader() method that returns the value of the request header. You can then pass an object of the implementation class to the init() method, to initialize the SDK.

Java Servlet API versions <=4

Sample code for Java applications that use Java servlets of API versions lesser than or equal to 4.0 (javax.servlet):

Implementation Class:

    
copy
package com.zoho.catalyst.appsail.demo.utils; import javax.servlet.http.HttpServletRequest; import com.zc.auth.AuthHeaderProvider; public class AuthProviderImpl implements AuthHeaderProvider { HttpServletRequest request; public AuthProviderImpl(HttpServletRequest request) { this.request = request; } @Override public String getHeaderValue(String key) { return request.getHeader(key); } }

Initialization:

    
copy
AuthProviderImpl authProviderImpl = new AuthProviderImpl(req); CatalystSDK.init(authProviderImpl);

Java Servlet API versions >=5

Sample code for Java applications that use Java servlets of API versions greater than or equal to 5.0 (jakarta.servlet):

Implementation Class:

    
copy
import com.zc.auth.AuthHeaderProvider; import jakarta.servlet.http.HttpServletRequest; public class AuthProviderImpl implements AuthHeaderProvider { private HttpServletRequest request; AuthProviderImpl(HttpServletRequest request) { this.request = request; } @Override public String getHeaderValue(String s) { return request.getHeader(s); } }

Initialization:

    
copy
CatalystSDK.init(new AuthProviderImpl((HttpServletRequest) servletRequest));

If you are developing a Java application with the Maven build tool, you can include the Catalyst Java SDK as a dependency in the Maven configuration file (pom.xml), instead of downloading and adding the SDK in your source code manually.

To add the Catalyst SDK in a Maven project, simply add the Zoho repository (published in MvnRepository) in the pom.xml file as shown below:

    
copy
<repositories> <repository> <id>java-sdk</id> <url>https://maven.zohodl.com</url> </repository> </repositories>

You can then add the Java SDK as a dependency in pom.xml as shown below:

    
copy
<dependencies> <dependency> <groupId>com.zoho.catalyst</groupId> <artifactId>java-sdk</artifactId> <version>1.15.0</version> </dependency> </dependencies>

Implementing Catalyst Node.js SDK in AppSail

You can install the Catalyst Node.js SDK package by executing the following command in your terminal and including it in your app’s source code:

    
copy
npm install zcatalyst-sdk-node --save

This will install the latest supported version of the Node.js SDK. You can also install a specific supported version in this way:

    
copy
npm install zcatalyst-sdk-node@2.1.1 --save

You can then initialize the Node.js SDK in your application’s code as shown in the sample code below. This passes the request object to the initialize() method.

    
copy
const catalyst = require('zcatalyst-sdk-node') const express = require('express') const app = express() app.get((req, res) => { let catalystApp = catalyst.initialize(req); //Your code goes here }) app.listen(process.env("X_ZOHO_CATALYST_LISTEN_PORT") || 9000)

Refer Catalyst Node.js SDK help for details. The SDK documentation also provides sample code snippets for all supported functionalities.


Implementing Catalyst Python SDK in AppSail

You can install Catalyst Python SDK for your AppSail solution by executing the following command in your terminal and including it in your app’s source code:

    
copy
pip install zcatalyst-sdk -t .

You can then import the Python SDK in your code for your Catalyst app. The SDK will need to be initialized with the request object before each request.

An example code snippet for importing and initializing Python SDK in a Flask web app is shown below:

    
copy
from flask import Flask, request, g import os import zcatalyst_sdk from zcatalyst_sdk.catalyst_app import CatalystApp app = Flask(__name__) @app.before_request def before_request(): if request.path.startswith('/admin'): return 'Unauthorized', 401 # if authorized user g.zc_app = zcatalyst_sdk.initialize(req=request) @app.route('/') def index(): return 'Web App with Python Flask!' @app.route('/cache') def cache(): app: CatalystApp = g.zc_app resp = app.cache().segment().put('key', 'value') return resp, 200 listen_port = os.getenv('X_ZOHO_CATALYST_LISTEN_PORT', 9000) app.run(host='0.0.0.0', port = listen_port)

Refer to Catalyst Python SDK help for details about the various functionalities of the SDK toolkit and sample code snippets.

Last Updated 2024-04-15 12:43:38 +0530 +0530