The solution enables you to evaluate JS code and also build out Go Templates via a REST API
- When you use an automation pipeline or tool and need to evaluate JS.
- When you use an automation pipeline or tool and need to build template.
- you will need to install go 1.8 https://go.dev/doc/install to run and install evaluate-js-templates-api
Below is a description evaluate-js-templates-api project folders and what they contain
Folder | Description |
---|---|
docs | Contains the swagger documents |
documents | Contains cli and api markdown files |
modules | Contains evaluate-js-templates-api modules and code |
go install github.com/Mrpye/evaluate-js-templates-api
1. Clone the repository
git clone https://github.com/Mrpye/evaluate-js-templates-api.git
2. Build the container as API endpoint
sudo docker build . -t evaluate-js-templates-api:v1.0.0 -f Dockerfile
3. Run the container
sudo docker run -d -p 8080:8080 --name=evaluate-js-templates-api --restart always --env=WEB_IP=0.0.0.0 --env=WEB_PORT=8080 -t evaluate-js-templates-api:1.0.0
- WEB_IP (set the listening ip address 0.0.0.0 allow from everywhere)
- WEB_PORT (set the port to listen on)
Check out the CLI documentation here
evaluate-js-templates-api
Add two numbers
curl --location 'localhost:8080/evaluate' \
--header 'Content-Type: application/json' \
--data '{
"code":"10+125",
"model":null
}'
Result:
135
Manipulate data model and return the model
curl --location 'localhost:8080/evaluate' \
--header 'Content-Type: application/json' \
--data '{
"code":"model.test=\"mydata\";model.test2=model.test2+10;model",
"model":{
"test":"test1",
"test2":5
}
}'
Result:
{
"test": "mydata",
"test2": 15
}
Build a template simple
curl --location 'localhost:8080/template' \
--header 'Content-Type: application/json' \
--data '{
"template":"Message1: {{.test1}} Message2: {{.test2}}",
"model":{
"test1":"This is Test1",
"test2":5
}
}'
Result:
Message1: This is Test1 Message2: 5
Build a template complex
curl --location 'localhost:8080/template' \
--header 'Content-Type: application/json' \
--data '{
"template":"Message: {{.test1}}\n A number: {{.test2}}\n list of numbers: {{range $val := .loop1}} {{$val}}{{end}}\n list of names and age: {{range $val := .loop2}} {{uc $val.name}}:{{$val.age}} {{end}}",
"model":{
"test1":"This is Test1",
"test2":5,
"loop1":[1,2,3,4,5],
"loop2":[
{
"name":"tony",
"age":22
},
{
"name":"fred",
"age":45
}
]
}
}'
Result:
Message: This is Test1
A number: 5
list of numbers: 1 2 3 4 5
list of names and age: TONY:22 FRED:45
Test is alive
curl --location --request GET 'localhost:8080/'
Result:
OK
This guide will show you how to build the helm chart package for evaluate-js-templates-api, you will need to have helm installed to build the package.
1. Build the helm chart package for evaluate-js-templates-api
# change into the chart directory
cd charts
# Package the evaluate-js-templates-api chart
helm package evaluate-js-templates-api
the helm chart package will be saved under the charts folder evaluate-js-templates-api-0.1.0.tgz
The code below shows you how to update the swagger API documents.
If you need more helm on using these tools please refer to the links below
- gin-swagge https://github.com/swaggo/gin-swagger
- swag https://github.com/swaggo/swag
1. Install swag
#Install swag
go install github.com/swaggo/swag/cmd/swag
2. Update APi document
#update the API document
swag init
3. Update the api.md
swagger generate markdown -f .\docs\swagger.json --output .\documents\api.md
- Nothing at the moment
https://github.com/dop251/goja
cimplex is Apache 2.0 licensed.