PMSYS Team Server Unit (TSU)
Introduction
The PMSYS TSU is the primary backend server for PMSYS Reporter and Trainer application.
It consist of the following components.
- Team Server Unit (TSU)
- Data Server Unit (DSU)
- Messaging
- Cronbox
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 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).
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):
main.go --> figure out application settings from various sources, start application context and kick the server
server.go --> actual server kicking is happening here: mainly loading of routes and middleware
Route definitions and handlers:
handlers.go --> defines the actual logic that gets executed when you visit a route and takes care of the response to the client
handlers_test.go --> tests for our route handlers
Data model descriptions and operations on the data:
models.go --> structs describing our data, bit similar to objects in other languages
database.go --> our mock/fake database implementation
database_test.go --> tests our mock/fake database
Test data in JSON format:
fixtures.json
Configuration file for [fresh](go get github.com/pilu/fresh):
runner.conf
Test that checks whether structs comply with the DataStorer interface:
interface_test.go
Helper structs and functions:
helpers.go
Defines application version using semantic versioning:
VERSION
Folder where we store all our go dependencies using the govendor
tool:
vendor/
API Routes
TSU API
DSU API
Messaging API
Cronbox API