Skip to content

vtfk/dust-webservice

Repository files navigation

JavaScript Style Guide

dust-webservice

Local web service for calling scripts for DUST (amongst others if you wish).

IIS setup

  1. Add IIS to a Windows 2012/2016/2019 server
  2. Install nodejs
  3. Install URL Rewrite to IIS
  4. Install Application Request Routing to IIS
  5. Configure Reverse Proxy in IIS

Setup repo

git clone https://github.com/vtfk/dust-webservice.git
cd dust-webservice
npm i

Setup environment variables

Create a .env file in the cloned repo:

EXPRESS_PORT=3000
MAX_BUFFER=10240000
ACCEPTED_PATH_ROOT=<local-full-path-to-folder>
SERVICE_PATH=<local-full-path-to-folder-containing-the-scripts>
JWT_SECRET=<SuperDuperSecretSecretKey>
PAPERTRAIL_HOST=logs.example.com
PAPERTRAIL_PORT=port
PAPERTRAIL_HOSTNAME=<coolname>
NODE_ENV=production

SERVICE_PATH can and should contain a %service% part to support a variaty of service endpoints.

Example: D:\Scripts\%service%\scripts

MAX_BUFFER is total bytes to accept in stdout

Set ACCEPTED_PATH_ROOT to the root path of where all scripts are stored. All scripts to be invoked must be directly inside the root path or in any number of sub folders.

Set JWT_SECRET to a super duper secret 🗝   to enable JWT token auth. To disable, remove this configuration

Add PAPERTRAIL_* and NODE_ENV to enable papertrails logging

Start webservice on Windows startup

The user that will be running the webservice needs to have local administrative privileges to setup the Windows Service.

After the service is setup, the user no longer needs administrative privileges

  1. Install PM2 (A Process Manager for node.js) (Must be run with the user that will be running the webservice)
    1. npm install -g pm2
  2. Move PM2 home to D:\PM2
    1. Create a new folder D:\PM2
    2. Create a new System level environment variable PM2_HOME and set it to D:\PM2
    3. Open a new cmd and verify that PM2_HOME is set correctly by running echo %PM2_HOME%
  3. Start the webservice in pm2 and save the current process list
    1. Open a new cmd
    2. Navigate to the folder containing the webservice you cloned
    3. Start the webservice in pm2 by running pm2 start index.js
    4. If all goes well, you should see a table containing your service and it should have it's status set to online
    5. To save the current process list, run pm2 save
    6. You should see Successfully saved in D:\PM2\dump.pm2
  4. Install pm2-windows-service (Must be run with the user that will be running the webservice)
    1. npm install -g pm2-windows-service
    2. Open a new cmd as administrator (this must be with the same user that will be running the webservice!)
      1. pm2-service-install -n DUST
        1. ? Perform environment setup (recommended): Yes
        2. ? Set PM2_HOME: Yes
        3. ? PM2_HOME value...: D:\PM2
        4. ? Set PM2_SERVICE_SCRIPTS...: No
        5. ? Set PM2_SERVICE_PM2_DIR...: Yes
        6. ? Specify the directory containing the pm2 version to be used by the service: C:\Users%username-of-user-to-run-the-webservice%\AppData\Roaming\npm\node_modules\pm2\index.js
        7. PM2 service installed and started
  5. Set Windows Service to log on as the correct user account
    1. Open services.msc
    2. Open Properties on the Windows Service
    3. Go to the Log On tab
    4. Select This account
    5. Click browse and choose the correct user
    6. Put in users password
    7. Stop and Start the Windows Service to switch to the new user

If you remove administrative privileges from the user running the Windows Service, the user doesn't have access to start the service on Windows startup. To go around this, create a scheduled task that will start the service on Windows Startup

  1. Open Task Scheduler as administrator
    1. Create a new task
    2. Set SYSTEM as the running user for the task
    3. Activate Run with highest privileges
    4. Add a trigger for At startup and set a delay for 1 minute to allow other required Windows services to launch first
    5. Add a action:
      1. Program: powershell
      2. Add arguments: -nologo -noninteractive -command Start-Service -Name "DUST" -Confirm:$False

Usage

👉🏼 Head over here to see usage