These instructions are for developers who want to contribute to the Core Framework (this repository). If you only need to run the Framework (ie with a Reference App configuration), or you are a developer building configurations, you can follow the easy deployment instructions instead.
Before getting started, read about our development workflow and the architecture overview. With the setup instructions below the tools will run directly on your machine, rather than via Docker.
Developers are actively using both Linux and MacOS, so both of those platforms are well supported for development. We don't support Windows out of the box. However, you can try using the Windows Subsystem for Linux. See the Windows Subsystem for Linux notes for how the installation instructions differ.
You will need to install the following:
- Node.js 8.11.x and above
- npm 6.x.x and above (to support npm ci)
- grunt cli
- CouchDB 2.x (installation instructions). For simplicity we recommend installing via docker. If on a Mac, please note that installation via homebrew is not supported.
- xsltproc
- python 2.7
To run end-to-end tests you will also need:
- Java JDK
- Docker
Installation instructions for these tools differ heavily based on your operating system and aren't covered here.
We recommend using Docker to install and use CouchDB. This ensures you are getting a compatible version and are not relying on OS packages that may be more or less up to date than needed.
After installing docker, you can create a docker container like so:
docker run -d -p 5984:5984 -p 5986:5986 --name medic-couchdb -v <path>:/opt/couchdb/data apache/couchdb:2
Notes before copy pasting:
--name
creates a container calledmedic-couchdb
. You can name it whatever you want, but this is how you refer to it later-v
maps where couchdb stores data to your local file system for performance, using the path before the:
(the path after the colon is the internal path inside the docker image). This should be somewhere in your home directory you have write access to, and want this data to be stored.apache/couchdb:2
will install the latest package for CouchDB 2.x
Once this downloads and starts, you will need to initialise CouchDB as noted in their install instructions.
You can use docker stop medic-couchdb
to stop it and docker start medic-couchdb
to start it again. Remember that you'll need to start it whenever you restart your OS, which might not be the case if you use a normal OS package.
Medic recommends you familiarise yourself with other Docker commands to make docker image and container management clearer.
Medic needs the following environment variables to be declared:
COUCH_URL
: the full authenticated url to themedic
DB. Locally this would behttp://myAdminUser:myAdminPass@localhost:5984/medic
COUCH_NODE_NAME
: the name of your CouchDB's node. The Docker image default isnonode@nohost
. Other installations may use[email protected],1
. You can find out by querying CouchDB's membership API- (optionally)
API_PORT
: the port API will run on. If not defined we use5988
- (optionally)
CHROME_BIN
: only required ifgrunt unit
orgrunt e2e
complain that they can't find Chrome.
How to permanently define environment variables depends on your OS and shell (e.g. for bash you can put them ~/.bashrc
). You can temporarily define them with export
:
export COUCH_NODE_NAME=nonode@nohost
export COUCH_URL=http://myAdminUser:myAdminPass@localhost:5984/medic
git clone https://github.com/medic/cht-core
cd cht-core
npm ci
By default CouchDB runs in admin party mode, which means you do not need users to read or edit any data. This is great for some, but to use your application safely we're going to disable this feature.
First, add an admin user. When prompted to create an admin during installation, use a strong username and password. Passwords can be changed via Fauxton. For more information see the CouchDB install doc.
Once you have an admin user you can proceed with securing CouchDB:
COUCH_URL=http://myAdminUser:myAdminPass@localhost:5984/medic COUCH_NODE_NAME=nonode@nohost grunt secure-couchdb
After following these steps CouchDB should no longer allow unauthorised access:
curl http://myAdminUser:myAdminPass@localhost:5984 # should work
{"couchdb":"Welcome","version":"2.0.0","vendor":{"name":"The Apache Software Foundation"}}
curl http://localhost:5984 # should fail
{"error":"unauthorized","reason":"Authentication required."}
To be able to use Fauxton with authenticated users:
curl -X PUT "http://myAdminUser:myAdminPass@localhost:5984/_node/$COUCH_NODE_NAME/_config/httpd/WWW-Authenticate" \
-d '"Basic realm=\"administrator\""' -H "Content-Type: application/json"
There are three steps to getting cht-core up and running.
Webapp code is stored in CouchDB. To compile and deploy the current code, use grunt
:
grunt
This will also watch for changes and redeploy as neccessary.
API is needed to access the application.
Either start it directly with node
:
cd ./api
node server.js
Or use grunt
to have it watch for changes and restart as neccessary:
grunt dev-api
Sentinel is reponsible for certain background tasks. It's not strictly required to access the application, but many features won't work without it.
Either start it directly with node
:
cd ./sentinel
node server.js
Or use grunt
to have it watch for changes and restart as neccessary:
grunt dev-sentinel
Navigate your browser to http://localhost:5988/medic/login
.
Follow the steps below to use an Android device with a development build of your application. This process is relevant when running v3.5.0 or greater of the Core Framework since it relies on service workers, which requires a valid HTTPS certificate. Use either serveo
or ngrok
to make your developer build accessible from your Android device by giving it a trusted URL.
- Start the api. This can be via docker, grunt, debug, horti, etc.
- Follow the instructions below to start serveo (preferred) or ngrok
- This will output a generated URL which you can enter into our android app or browser and connect to your local dev environment.
Proxying via serveo is generally more successful than ngrok so it is our preferred route. Sometimes it will be blocked by Chrome safe browsing however in which case you can try ngrok.
- To connect to an API running via
grunt
orhorti
, executessh -R 80:localhost:5988 serveo.net
- To connect to an API running via
Docker
, executessh -R 80:localhost:443 serveo.net
This will echo a URL which you can connect to.
ngrok sometimes fails due to connection throttling which can cause the service worker cache preload to fail. It's included here as an alternative in case serveo doesn't work for some reason.
- Create a ngrok account at https://ngrok.com/
- Follow instructions on downloading and linking your computer to your ngrok account.
- Start ngrok
- To connect to an API running via
grunt
orhorti
, execute./ngrok http 5988
- To connect to an API running via
Docker
, execute./ngrok http 443
- To connect to an API running via
- Access the app using the https address shown, eg https://123456.ngrok.io
To fill your app with generated data, you can batch-load messages from a CSV file, with the load_messages.js script.
Use curl
to submit a single message:
curl -i -u gateway:123qwe \
--data-urlencode 'message=Test One two' \
--data-urlencode 'from=+13125551212' \
--data-urlencode 'sent_timestamp=1403965605868' \
-X POST \
http://localhost:5988/api/v1/records
All text labels in the app are localized. See the translation documentation for more details on how to add new labels or modify existing ones.
Check out the Gruntfile for all the tests you can run.
They live in the tests
directories of each app. Run them with grunt: grunt unit-continuous
.
They live in tests. Run them with grunt: grunt e2e
. Docker is required (it should be available on the command line as docker
).
grunt api-e2e
Travis runs grunt ci
every time some new code is pushed to github.
To build reference documentation into a local folder jsdoc-docs
: grunt build-documentation
Code is automatically published via Travis CI to the staging server.