# 1 - Add DATP to your Restify server
# Add DATP to your project
mv index.js app.js
npm install @tooltwist/datp
npm install esm
# Changes to your source code
In VSCode, right click on COURSE-FILES/3.datp/app-example1.js
and select Open to the Side. This will allow you to view your app.js in the left pain, and the updated version in the right pane.
These are the changes required to add DATP to your Restify server:
- Import DATP and the function addRoute() function.
- Surrounding our code with an asynchronous main() function. This allows us to use await and try/catch in our code.
- Use DATP.restifyMasterServer() instead of restify.createServer().
- Register our routes using addRoute() rather than server.get(). We'll ignore the server.head() in this tutorial.
- Start the server using DATP.goLive() rather than server.listen().
# Configuration files
When DATP is run, it's configuration is using JSON, and accessed using a library named Juice. This library allows the JSON file to be accessed from the file system during development, or from AWS Secrets Manager or another secure credentials store when running in a production environment.
Inside your project folder, create a folder named util-vsc
and copy these files into that folder.
COURSE_FILES/3.datp/run-master
COURSE_FILES/3.datp/dev-master
COURSE_FILES/3.datp/config-master.json
The location of the JSON configuration file is specified using an environment variable. The run-master
script sets that environment variable and then runs our server.
# Database and REDIS
Our devcontainer definitions cause Docker to start Docker containers for MySQL and also REDIS. It also starts a Docker container for a utility named phpMyadmin. This is a browser-based database administration tool, and we can use it to load our initial data into our database.
Open phpMyAdmin at http://localhost:33360. The pssword is root, and there is no password.
Have a look around this tool.
Create a database named
datp
, with encodingutf8_unicode_ci
.Use the Import tab to load data from
COURSE_FILES/3.datp/initial-data.sql
.
ZZZZ Need to simplify the data file
# Run our new DATP server
- In the terminal [within VSC], start the server with run-master.
util-vsc/run-master
- If the following URLs by either clicking on the following links, or test them using
curl
.
http://localhost:33370/datp/1.0/healthcheck
http://localhost:33370/datp/1.0/ping
http://localhost:33370/myProject/1.0/hello/fred
You might have noticed that our original Restify URL with the path /hello/fred is now /myProject/1.0/hello/fred. This is because the addRoute()
function promotes the best practices of namespaces and API versioning.
# Automatic Restart
If you modify your app.js
, you will find that the changes aren't deployed until you restart the server. To speed up development, we can use a utility named nodemon
, by using dev-master
instead of run-master
.
- Edit
package.json
and update the scripts section to include a vsc_dev entry:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"vsc-dev": "nodemon -w /workspace -x node -r esm app.js"
},
2
3
4
If your server is still running, kill it using <control>-C.
Start the server using dev-master:
util-vsc/dev-master
- If you make any change to
app.js
and save the file, you will see the server restart.
# Menu option to start the server
On the pane at the bottom of VSCode where it says PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL PORTS, on the right side there is a + and down arrow. If you click on that arrow you'll see an option 'MASTER', that will run the dev-master script in a new terminal.
# Next...
In the next exercise we will start calling DATP transactions from the administration tool, MONDAT.