From f5cbc66943a217f3830254d6b3400a2e36ad1d55 Mon Sep 17 00:00:00 2001 From: Key Networks <34238649+key-networks@users.noreply.github.com> Date: Thu, 14 Dec 2017 17:53:03 +0800 Subject: [PATCH] Added dotenv to easily add environment variables in .env file --- .gitignore | 1 + README.md | 35 ++++++++++++++++++++++++++++++----- app.js | 2 ++ package-lock.json | 5 +++++ package.json | 1 + 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 532fa27..ba48844 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ etc/passwd etc/storage/ node_modules/ *.swp +.env diff --git a/README.md b/README.md index d6193b7..6219d9f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ sudo npm install -g node-gyp * ztncui requires [ZeroTier One](https://www.zerotier.com/download.shtml) to be installed on the same machine. This will run as the network controller to establish ZeroTier networks. -* ztncui has been developed on a Linux platform and expects the ZT home directory to be in `/var/lib/zerotier-one`. It should be easy to modify for other platforms - please feed back if this is required. +* ztncui has been developed on a Linux platform and expects the ZT home directory to be in `/var/lib/zerotier-one`. It should be easy to modify for other platforms - please feed back if this is required. (Edit: it should be easier to run on any platform now using a `.env` file - see below). ### Installing ##### 1. Clone the repository on a machine running ZeroTier One: @@ -30,7 +30,11 @@ cd ztncui npm install ``` -##### 3. Allow access to /var/lib/zerotier-one/authtoken.secret +##### 3. authtoken.secret + +The app needs to know the zerotier-one authtoken.secret. Their are two options: + +###### A: Allow access to /var/lib/zerotier-one/authtoken.secret The user running the ztncui app needs read access to authtoken.secret. This can be achieved with: ```shell sudo usermod -aG zerotier-one username @@ -41,6 +45,27 @@ Where: Note that you need to log out and in again to apply the new group membership. +###### OR +###### B: Make a .env file +In the root of the ztncui directory, create a `.env` file with the content: +``` +ZT_TOKEN=######################## +``` +Where: +* ######################## is the token string. + +You can also specify in the `.env` file a different address for the zerotier-one API (which defaults to localhost:9993): +``` +ZT_ADDR=localhost:9995 +``` + +Make `.env` readable by the user running ztncui only: +```shell +chmod 600 .env +``` + +The `.env` file should make it easier to run ztncui on a non-Linux platform. + ##### 4. Copy the default passwd file To prevent git from over-writing your password file every time you pull updates from the repository, the etc/passwd file has been added to .gitignore. So you need to copy the default file after the first time you do a git clone. All these things ideally need to be done with a package installer script: ```shell @@ -51,9 +76,9 @@ cp -v etc/default.passwd etc/passwd ```shell npm start ``` -This will run the app on TCP port 3000 by default. If port 3000 is already in use, you can specify a different port, e.g.: -```shell -PORT=3456 npm start +This will run the app on TCP port 3000 by default. If port 3000 is already in use, you can specify a different port in the `.env` file (see 3B above), e.g.: +``` +PORT=3456 ``` ##### 6. Start the app automatically diff --git a/app.js b/app.js index ca07325..6f8f1e6 100644 --- a/app.js +++ b/app.js @@ -4,6 +4,8 @@ Licensed under GPLv3 - see LICENSE for details. */ +require('dotenv').config(); + const express = require('express'); const path = require('path'); const favicon = require('serve-favicon'); diff --git a/package-lock.json b/package-lock.json index d17ff6b..576f765 100644 --- a/package-lock.json +++ b/package-lock.json @@ -534,6 +534,11 @@ "is-obj": "1.0.1" } }, + "dotenv": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-4.0.0.tgz", + "integrity": "sha1-hk7xN5rO1Vzm+V3r7NzhefegzR0=" + }, "duplexer": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", diff --git a/package.json b/package.json index 5e6040f..5002888 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "bootstrap": "^3.3.7", "cookie-parser": "~1.4.3", "debug": "~3.1.0", + "dotenv": "^4.0.0", "express": "~4.16.2", "express-session": "^1.15.6", "express-validator": "^4.3.0",