diff --git a/README.md b/README.md index 4102eda7..014574b2 100644 --- a/README.md +++ b/README.md @@ -60,9 +60,9 @@ gender | A binary gender (eg. `Male`, `Woman`, `M`) | Formats to either `Male` o This tool is available via Docker by running `docker compose up`. Set the [Environment Variables](#environment-variables). ## Development -Create a `.env` file. Check out the `env.example` to get started. See [Environment Variables](#environment-variables). +Create an environment file by `cp env.example .env`. Change `INTERFACE` to `127.0.0.1` and otherwise see [Environment Variables](#environment-variables) for more info. -Then run +Then run: ``` npm run dev @@ -79,11 +79,15 @@ npm start The `env.example` file has example values. Here's what they mean: -Variable | Description +Variable | Description | Sample -- | -- -`CONFIG_NAME` | Name of the configuration to use (eg. "CHIS-KE") -`EXTERNAL_PORT` | Port to use when starting the web server -`COOKIE_PRIVATE_KEY` | A string used to two-way encryption of cookies. Production values need to be a secret. +`CONFIG_NAME` | Name of the configuration to use | `chis-ke` +`EXTERNAL_PORT` | Port to use in docker compose when starting the web server | `3000` +`PORT` | For localhost development environment | `3000` +`COOKIE_PRIVATE_KEY` | A string used to two-way encryption of cookies. Production values need to be a secret. Suggest `uuidgen` to generate | `589a7f23-5bb2-4b77-ac78-f202b9b6d5e3` +`INTERFACE` | Interface to bind to. Leave as '0.0.0.0' for prod, suggest '127.0.0.1' for development | `127.0.0.1` +`CHT_DEV_URL_PORT` | CHT instance when in `NODE_ENV===dev`. Needs URL and port | `192-168-1-26.local-ip.medicmobile.org:10463` +`CHT_DEV_HTTP` | 'false' for http 'true' for https | `false` ## Publishing new docker images diff --git a/env.example b/env.example index afc53619..9edb4c17 100644 --- a/env.example +++ b/env.example @@ -2,3 +2,6 @@ COOKIE_PRIVATE_KEY= CONFIG_NAME=chis-ke PORT=3000 # for development environment EXTERNAL_PORT=3000 # for docker +INTERFACE=0.0.0.0 # Leave as '0.0.0.0' for prod, suggest '127.0.0.1' for development +CHT_DEV_URL_PORT=localhost:5984 # where your dev CHT instance is, hostname:port +CHT_DEV_HTTP=true # 'false' for http 'true' for https diff --git a/src/config/index.ts b/src/config/index.ts index 14d3d623..ddcd70a2 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,6 +1,7 @@ import _ from "lodash"; import { ChtApi, PlacePayload } from "../lib/cht-api"; import getConfigByKey from "./config-factory"; +import {env} from "process"; export type ConfigSystem = { domains: AuthenticationInfo[]; @@ -55,6 +56,8 @@ export type AuthenticationInfo = { const { CONFIG_NAME, NODE_ENV, + CHT_DEV_URL_PORT, + CHT_DEV_HTTP } = process.env; const partnerConfig = getConfigByKey(CONFIG_NAME); @@ -139,11 +142,17 @@ export class Config { public static getDomains() : AuthenticationInfo[] { const domains = [...config.domains]; + // because all .env vars imported as strings, let's get the AuthenticationInfo object a boolean + let TMP_USE_HTTP = true; + if (CHT_DEV_HTTP === 'false') { + TMP_USE_HTTP = false + } + if (NODE_ENV !== 'production') { domains.push({ - friendly: '$localhost', - domain: 'localhost:5988', - useHttp: true, + friendly: '$Development Instance (' + CHT_DEV_URL_PORT + ')', + domain: CHT_DEV_URL_PORT as string, + useHttp: TMP_USE_HTTP, }); } diff --git a/src/index.ts b/src/index.ts index ea189354..b9e9fe4f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,10 @@ require('dotenv').config(); import build from "./server"; import { env } from 'process'; +const { + NODE_ENV, + INTERFACE +} = process.env; const port: number = env.PORT ? parseInt(env.PORT) : 3000; @@ -13,8 +17,15 @@ const port: number = env.PORT ? parseInt(env.PORT) : 3000; const server = build({ logger: loggerConfig, }); - server.listen({ host: '0.0.0.0', port }, (err, address) => { + + // in 1.1.0 we allowed INTERFACE to be declared in .env, but let's be + // backwards compatible to when it was undeclared and hard coded to + // be 0.0.0.0 + let calculated_interface = "0.0.0.0"; + if (INTERFACE) { + calculated_interface = INTERFACE; + } + server.listen({ host: calculated_interface, port }, (err, address) => { if (err) throw err; - console.log(`server is listening on ${address}`); }); })(); diff --git a/src/lib/cht-api.ts b/src/lib/cht-api.ts index d42a048e..42c2366d 100644 --- a/src/lib/cht-api.ts +++ b/src/lib/cht-api.ts @@ -3,6 +3,10 @@ import axios, { AxiosHeaders } from "axios"; import { UserPayload } from "../services/user-payload"; import { AuthenticationInfo, Config, ContactType } from "../config"; +const { + NODE_ENV +} = process.env; + export type ChtSession = { authInfo: AuthenticationInfo; sessionToken: string; @@ -64,6 +68,13 @@ export class ChtApi { const sessionToken = setCookieHeader?.[0].split(';') .find((header : string) => header.startsWith(COUCH_AUTH_COOKIE_NAME)); + if (NODE_ENV !== 'production') { + if (!sessionToken) { + console.log("failed to login to " + sessionUrl); + } else { + console.log("successfully logged in to " + sessionUrl); + } + } return { authInfo, username, @@ -224,4 +235,4 @@ function extractLineage(doc: any): string[] { } return []; -}; \ No newline at end of file +};