An API endpoint to evaluate js and build Go templates
Description
The solution enables you to evaluate JS code and also build out Go Templates via a REST API
When to use evaluate-js-templates-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.
Requirements
Project folders
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 |
|
|
Installation
go install github.com/Mrpye/evaluate-js-templates-api
Run as a container
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
Environment variables
- WEB_IP (set the listening ip address 0.0.0.0 allow from everywhere)
- WEB_PORT (set the port to listen on)
How to use evaluate-js-templates-api CLI
Check out the CLI documentation here
Using the API
Run web server
evaluate-js-templates-api
Examples
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
evaluate-js-templates-api Helm chart
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
Update the swagger document
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
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
To Do
3rd party Libraries
https://github.com/dop251/goja
license
cimplex is Apache 2.0 licensed.