RosaeNLG node.js Server
Server for RosaeNLG.
Install & Run the server
The server is provided without any security so you should only use it in a microservice architecture, and never make it publicly visible.
Using npm and node.js
mkdir my-rosaenlg-server
cd my-rosaenlg-server
npm init --yes
npm install rosaenlg-node-server
npx rosaenlg-server
Using Docker
Not saving templates:
docker run -p 5000:5000 -i registry.gitlab.com/rosaenlg-projects/rosaenlg/server:latest
Saving templates on the disk (here on an AWS EC2 server): you have to define the ROSAENLG_HOMEDIR
variable and map volumes:
mkdir templates
docker run -p 80:5000 --env "ROSAENLG_HOMEDIR=/templates" -v /home/ec2-user/templates:/templates -i registry.gitlab.com/rosaenlg-projects/rosaenlg/server:latest
You can also use S3, see below.
Configuration
Persistence
By default, templates are not saved permanently: the server is empty when it starts, you can upload templates and render them, but they are lost when the server is shut down. To save templates, you must provide a path to the disk or S3 credentials. Templates will be saved when uploaded (as json files), and reloaded when the server restarts.
when using direct rendering, which is rendering a template with both the template and the data in the same request, the template is cached but will never be stored permanently. |
Configuration:
-
Persistence on disk: Set the environment variable
ROSAENLG_HOMEDIR
. -
Persistence on S3:
-
set the 3 environment variables
AWS_S3_BUCKET
,AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
-
optionally use
AWS_S3_ENDPOINT
to indicate an S3-compatible object storage service (for instancehttps://mys3service.com
)
-
You can either save on disk or on S3, but not both. |
You can also push new templates directly on the disk (using CI or whatever) and ask the server to reload them (see reload
path), without having to restart the server. Follow these 2 constraints:
-
filename must be
$user_$templateId.json
: you can useDEFAULT_USER
(see User identification) -
a
user
field in the JSON (with the same value) must identify which user the template belongs to
Documentation, swagger, OpenAPI
Static version is here.
When running the server, the documentation is directly available: http://localhost:5000/api-docs
User identification
You can identify the user using the X-RapidAPI-User
header. Each user has his own separate space: user2
cannot see nor use user1
templates, etc.
If you do not identify users (which is a valid choice), user will default to DEFAULT_USER
.
Packaging the templates
RosaeNLG templates are typically developed on a node.js environment, as RosaeNLG is primarly a JavaScript library. Once the templates are developed, you can package them in a JSON package (instead of having multiple .pug
files, which is not practical), deploy them on RosaeNLG Java Server and render texts.
To package the templates, use the Gulp RosaeNLG plugin.
Use the API - Exemple using cURL
Register a template
curl -X PUT \
http://localhost:5000/templates \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/json' \
-d '{
"templateId": "chanson",
"entryTemplate": "chanson.pug",
"compileInfo": {
"activate": false,
"compileDebug": false,
"language": "fr_FR"
},
"templates": {
"chanson.pug": "p\n | il #[+verb(getAnonMS(), {verb: '\''chanter'\'', tense:'\''FUTUR'\''} )]\n | \"#{chanson.nom}\"\n | de #{chanson.auteur}\n"
}
}
'
You should get:
{
"templateId":"chanson",
"status":"CREATED",
"ms": ...
}
Render the template with some input data:
curl -X POST \
http://localhost:5000/templates/chanson/render \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/json' \
-d '{
"language": "fr_FR",
"chanson": {
"auteur": "Édith Piaf",
"nom": "Non, je ne regrette rien"
}
}'
You should get:
{
"templateId":"chanson",
"renderedText":"<p>Il chantera \"Non, je ne regrette rien\" d'Édith Piaf</p>",
"renderOptions":{
"language":"fr_FR"
},
"ms": ...
}