README ¶
Open mHealth Framework for GO
Introduction
This repository contains library and reference servers for building and running Open mHealth compatible web services. There are two APIs that are currently available.
- Data Server Unit (DSU)
- Team Server Unit (TSU)
In addition, there are libraries to send push messages to mobile devices over Firebase Cloud Messaging service. We also provide a corn-like scheduler for sending repeated messages.
The server depend on several other services to run. These are:
- Postgres SQL server
- Mongo database
- Firebase account
- PMSYS Policy Server Unit
- Auth0 account
All services must be configures before the service is started.
Configuration
All configuration options for the TSU are defined in the Config.go
file. They can be set from several sources. They are processed in the following order:
- Defaults in
Config.go
- Environment variables
- The
config.yml
file - (optional) Any
config-<env>.yml
file
The TSU use Configor to handle configuration.
Sources for configuration
Configuration from environment variables
All configuration options can be set using environment variables. Thy are named according to the specification found in Config.go
, uppercased and prefixed with TSU
. Substrucures are seperaded with an underscore _
. For example,
Config.go |
ENV |
---|---|
Host | TSU_HOST |
Mongo.Post | TSU_MONGO_PORT |
Configuration from files
The server will read configuration from the environent variables and the config.yml
file. In addition, configuration will also be ready from an environment specific file config-<env>.yml
. The environement is specified in the TSU_ENV
environment variable. For instance, if you have set production confiugraiton in the config-prod.yml
, run:
TSU_ENV=prod ./pmsys-tsu
Postgresql Database setup
Postgres connection setup is controlled by the following configuration variables:
Options | Description |
---|---|
Postgres.User | Database username |
Postgres.Password | Password |
Postgres.Host | IP/Hostname for database server |
Postgres.Port | Portnumner (default=5432) |
Postgres.Database | Name of database table |
This project use the Goose migration tool to initiate and upgrade database tables. Install by
$ go get -u github.com/pressly/goose/cmd/goose
Then migrate the database by running (substituting config values).
$ goose postgres "user=<User> dbname=<Database> password=<Password> host=<Host> sslmode=disable" up
Mongo Database setup
Mongodb is controlled by the following configuration variables:
Options | Description |
---|---|
Postgres.User | Database username |
Postgres.Password | Password |
Postgres.Host | IP/Hostname for database server |
Postgres.Port | Portnumner (default=3306) |
Postgres.Database | Name of database table |
IMPORTANT: you must manually configure the dataPoints collections to make header.username and header.id unique together.
Firebase Cloud Messaging (FCM)
The following options are required for TSU messaging API.
Options | Description |
---|---|
FireBase.CredentialsFile | Path to the file containing your FCM credentials (a JSON file) |
FireBase.ProjectID | The Firebases ProjectID |
The required data can be found and downloaded from your Firebase console.
How to run
Compile and run the tsud server using:
go build ./... && ./pms-tsu
The app will bind itself to port 8080 (defined in Config.go
). If you
want to change it (e.g. bind it to the default HTTP port 80).
To load configuration from environment parameters stored in a file, the following is recommended.
set -a; source env; set +b; ./tsud
Live Code Reloading
For live code reloading, use a task runner that automatically restarts the server when it detects changes.
Install fresh:
go get github.com/pilu/fresh
Run by using the following command in your project root directory:
fresh
High-level Code Structure
Main server files (bootstrapping of http server):
cmd/tsu/main.go --> figure out application settings from various sources, start application context and kick the server
cmd/tsu/server.go --> actual server kicking is happening here: mainly loading of routes and middleware
Route definitions and handlers:
cmd/tsud/handlers.go --> defines the actual logic that gets executed when you visit a route and takes care of the response to the client
cmd/tsud/handlers_test.go --> tests for our route handlers
Data model descriptions and operations on the data:
internal/pmsys/models.go --> structs describing our data, bit similar to objects in other languages
cmd/tsud/database.go --> our mock/fake database implementation
cmd/tsud/database_test.go --> tests our mock/fake database