RaythaHQ / raytha

Raytha is a powerful CMS with an easy-to-use interface and fast performance. It offers custom content types, a template engine, and various access controls. It supports multiple storage providers and an automatically generated REST API. Upgrade your development workflow with Raytha.
MIT License
143 stars 30 forks source link

Raytha Functions: Embeddable functions / scripting #177

Closed apexdodge closed 3 months ago

apexdodge commented 6 months ago

The next big feature will be to permit running server side scripting code directly within the platform, without having to deploy. This will open up a huge amount of capability, but does require coding knowledge.

We will call this feature Raytha Functions.

New left side navigation menu item, only viewable and accessible to those with Manage System permission, will say Functions.

Functions page should: 1) show a table of existing functions. 2) show a Create function button

Create function Form appears with the following fields:

/** The following classes are available:
 * API
 * CurrentOrganization
 * CurrentUser
 * Emailer
*/

/** 
 * Receives a get request at /raytha/functions/execute/{developerName}
 * @param {IQueryCollection} query Passed in from .NET's Request.Query
 * @returns {object} of type JsonResult, HtmlResult, or RedirectResult
 */
function get(query) {
    return new JsonResult({ success: true });
    //example 1: return new HtmlResult("<p>Hello World</p>");
    //example 2: return new RedirectResult("https://raytha.com");
}

/** 
 * Receives a post request at /raytha/functions/execute/{developerName}
 * @param {IFormCollection} payload Passed in from .NET's Request.Form
 * @param {IQueryCollection} query Passed in from .NET's Request.Query
 * @returns {object} of type JsonResult, HtmlResult, or RedirectResult
 */
function post(payload, query) {
    return new JsonResult({ success: true });
    //example 1: return new HtmlResult("<p>Hello World</p>");
    //example 2: return new RedirectResult("https://raytha.com");
}

We are going to use ClearScript for embedding javascript: https://github.com/microsoft/ClearScript?tab=readme-ov-file https://microsoft.github.io/ClearScript/Examples/Examples.html

Our ClearScript will attach some javascript classes that mimic IActionResult

class JsonResult {
  constructor(obj) {
    this.body = obj;
    this.contentType = "application/json";
  }
}

class HtmlResult {
  constructor(html) {
    this.body = html;
    this.contentType = "text/html";
  }
}

class RedirectResult {
  constructor(url) {
    this.body = url;
    this.contentType = "redirectToUrl";
  }
}

In order for Raytha Functions to be useful, we need to expose an interface that it can interact. The interface should represent functionality for:

New appsettings.json configuration: RAYTHA_FUNCTIONS_TIMEOUT and default to 10000 ms. RAYTHA_FUNCTIONS_MAX_ACTIVE and default to 5. If 0, disable the feature entirely.

Hitting the endpoints at /raytha/functions/execute/{developerName} will then trigger the correspondin ClearScript function for that DeveloperName if it exists. The running of the clearscript should crash out if it exceeds the timeout in appsettings for safety reasons.

We should be able to edit an existing function, disable / enable it, revert to previous revision, delete it.

bootleg224 commented 5 months ago

Functions should probably support current user context and admin context.

I can envision a function that should allow a user to update their own profile. Or an admin context that can list all users

fasteddys commented 4 months ago

@apexdodge has development stopped.. Was looking forward to adding custom modules or at least the way to add rapidly entities/crud with grid/tables from excel template

apexdodge commented 4 months ago

@fasteddys this feature is in development and release is scheduled for April.

apexdodge commented 3 months ago

This has been merged into dev and will now undergo testing and QA.