This repository was archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathJsLambdaController.cs
80 lines (71 loc) · 3.16 KB
/
JsLambdaController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//******************************************************************************
// <copyright file="license.md" company="RawCMS project (https://github.com/arduosoft/RawCMS)">
// Copyright (c) 2019 RawCMS project (https://github.com/arduosoft/RawCMS)
// RawCMS project is released under GPL3 terms, see LICENSE file on repository root at https://github.com/arduosoft/RawCMS .
// </copyright>
// <author>Daniele Fontani, Emanuele Bucarelli, Francesco Mina'</author>
// <autogenerated>true</autogenerated>
//******************************************************************************
using Jint;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using RawCMS.Library.Core;
using RawCMS.Library.Core.Attributes;
using RawCMS.Library.JavascriptClient;
using RawCMS.Library.Service;
using System;
using System.Collections.Generic;
namespace RawCMS.Plugins.Core.Controllers
{
[AllowAnonymous]
[RawAuthentication]
[Route("api/js")]
public class JsLambdaController
{
private readonly AppEngine lambdaManager;
private readonly CRUDService crudService;
private readonly ILogger logger;
public JsLambdaController(AppEngine lambdaManager, CRUDService crudService, ILogger logger)
{
this.lambdaManager = lambdaManager;
this.crudService = crudService;
this.logger = logger;
}
[AllowAnonymous]
[RawAuthentication]
[HttpPost("{lambda}")]
public JObject Post(string lambda, [FromBody] JObject input)
{
Library.DataModel.ItemList result = crudService.Query("_js", new Library.DataModel.DataQuery()
{
PageNumber = 1,
PageSize = 1,
RawQuery = $"{{\"Path\":\"{lambda}\"}}"
});
JToken js = result.Items[0];
string code = js["Code"].ToString();
Dictionary<string, object> tmpIn = input.ToObject<Dictionary<string, object>>();
Dictionary<string, object> tmpOut = new Dictionary<string, object>();
Engine engine = new Engine((x) => { x.AllowClr(typeof(JavascriptRestClient).Assembly); x.AllowClr(typeof(JavascriptRestClientRequest).Assembly); });
engine.SetValue("input", tmpIn);
engine.SetValue("RAWCMSRestClient", Jint.Runtime.Interop.TypeReference.CreateTypeReference(engine, typeof(JavascriptRestClient)));
engine.SetValue("RAWCMSRestClientRequest", Jint.Runtime.Interop.TypeReference.CreateTypeReference(engine, typeof(JavascriptRestClientRequest)));
engine.SetValue("RAWCMSCrudService", crudService);
engine.SetValue("output", tmpOut);
try
{
logger.LogDebug($"calling lambda: {lambda}");
engine.Execute(code);
}
catch (Exception e)
{
logger.LogError($"Error on lambda javascript script: {e.Message} ");
tmpOut.Add("Error", e.Message);
}
logger.LogDebug($"Lambda response: {tmpOut}");
return JObject.FromObject(tmpOut);
}
}
}