gomhdsu

module
v2.1.1+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2018 License: MIT

README

Open mHealth compatible Data Storage Unit (DSU) in Gin Gonic (GO)

Introduction

This repository contains an Open mHealth Compatable Data Storage Unit (DSU) written for GO using the Gin Gonic framework. Unlike our previous implementaiton, this one is designed for using different storage backends; most notably now supporting MySQL.

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 DSU. Substrucures are seperaded with an underscore _. For example,

Config.go ENV
Host DSU_HOST
MySQL.Port DSU_MYSQL_PORT

To load environment configuration stored in a file, simply run:

set -a; source config/env; set +a
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 DSU_ENV environment variable. For instance, if you have set production confiugraiton in the config-prod.yml, run:

DSU_ENV=prod ./dsud

MySQL Database setup

Postgres connection setup is controlled by the following configuration variables:

Options Description
MySQL.User Database username
MySQL.Password Password
MySQL.Host IP/Hostname for database server
MySQL.Port Portnumner (default=5432)
MySQL.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=$DSU_POSTGRES_USER dbname=$DSU_POSTGRES_DATABASE password=$DSU_POSTGRES_PASSWORD host=$DSU_POSTGRES_HOST sslmode=disable" up

or, if environment variables are set:

goose mysql "$DSU_MYSQL_USER:$DSU_MYSQL_PASSWORD@tcp($DSU_MYSQL_HOST:$DSU_MYSQL_PORT)/$DSU_MYSQL_DATABASE?tls=skip-verify&autocommit=true&interpolateParams=true&parseTime=true" up

To generate a docker test database, run:

docker run --name=pmmysql -e MYSQL_DATABASE=$DSU_MYSQL_DATABASE -e MYSQL_ROOT_PASSWORD=$DSU_MYSQL_PASSWORD -e MYSQL_ROOT_HOST="%" -d mysql/mysql-server:5.7

How to run

Compile and run the tsud server using:

go build ./... && ./dsud

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.

./dsud
High-level Code Structure

Main server files (bootstrapping of http server):

cmd/dsu/main.go --> figure out application settings from various sources, start application context and kick the server
cmd/dsu/server.go --> actual server kicking is happening here: mainly loading of routes and middleware

Route definitions and handlers:

cmd/dsud/handlers.go --> defines the actual logic that gets executed when you visit a route and takes care of the response to the client
cmd/dsud/handlers_test.go --> tests for our route handlers

Data model descriptions and operations on the data:

internal/auth0 --> Gin Middleware for Auth0 JWT authentication.
internal/mysqldriver --> package for reading and writing OMH datapoints to a MySQL database.
internal/mongodriver --> package for reading and writing OMH datapoints to a Mongo database.
internal/pmsys/models.go --> structs describing our data, bit similar to objects in other languages.
cmd/dsud/database.go --> our mock/fake database implementation.
cmd/dsud/database_test.go --> tests our mock/fake database.
Updating RDBS definitions with XO

The database model for our mysql driver is generated using XO. XO can be installed by. To update the model, run the following from the root source directory:

Set working environment

set -a; source config/env-development

Then update the XO files.

For MySQL:

xo mysql://$DSU_MYSQL_USER:$DSU_MYSQL_PASSWORD@$DSU_MYSQL_HOST/$DSU_MYSQL_DATABASE -p mysqldriver -o pkg/mysqldriver

For Postgresql:

xo pgsql://$DSU_MYSQL_USER:$DSU_MYSQL_PASSWORD@$DSU_MYSQL_HOST/$DSU_MYSQL_DATABASE -p pgsqldriver -o pkg/pgsqldriver

Directories

Path Synopsis
cmd
pkg
mysqldriver
Package mysqldriver contains the types for schema 'dsu'.
Package mysqldriver contains the types for schema 'dsu'.
pgsqldriver
Package pgsqldriver contains the types for schema 'public'.
Package pgsqldriver contains the types for schema 'public'.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL