Skip to content

Commit

Permalink
Detect Heroku deployment, remove psql requirement
Browse files Browse the repository at this point in the history
This update sets up some settings to be adaptive based on whether
an app is deployed on Heroku, which is inferred from the environment
variable "ON_HEROKU" being set to "true".

This simplifies development setup a bit: Django's default SQLite
can be used to run locally.
  • Loading branch information
madprime committed Jan 30, 2018
1 parent 5020522 commit ff14fcf
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
15 changes: 14 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
DATABASE_URL=postgres:///myurl
################################
# #
# THIS IS A SECRET FILE!!! #
# #
# Do not add your .env to git! #
# #
################################

# SECRET_KEY encrypts things. In production it should be a random string.
SECRET_KEY=mysecretshouldnotbeongit

# Set DATABASE_URL to use something other than the default SQLite.
# DATABASE_URL=postgres:///myurl

# Set Open Humans project CLIENT_ID and CLIENT_SECRET.
OH_CLIENT_ID=myclientidshouldnotbeongit
OH_CLIENT_SECRET=myclientsecretshouldnotbeongit
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Bastian Greshake Tzovaras
Copyright (c) 2018 Bastian Greshake Tzovaras, Mad Price Ball

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,22 @@ conda create -n oh_uploader python=3.6
pip install -r requirements.txt
```

*Step 2: Install heroku-CLI & PostgreSQL*
*Step 2: Install Heroku Command Line Interface (CLI)*

If you are running MacOS the easiest way to do this is using [Homebrew](https://brew.sh/). If you are on a Linux machine you [should be able to do the same things using Linuxbrew](https://virtualenv.pypa.io/en/stable/). After installing Homebrew/Linuxbrew
you can do:
You should install the Heroku CLI to run this app. Heroku has [installation instructions for MacOS, Windows, and Linux](https://devcenter.heroku.com/articles/heroku-cli#download-and-install).

If you are running MacOS the easiest way to do this is using [Homebrew](https://brew.sh/). After installing Homebrew you can do:

```
brew install postgres
brew install heroku/brew/heroku
```

Once this is done you can start the setup of your *Open Humans uploader* by copying the example `.env` file (`cp .env.sample .env`) and entering at least your database URL. Once this is done you can migrate your database using `heroku local:run python manage.py migrate`. Afterwards you can start the webserver of your local heroku environment using `heroku local`.
Once this is done you can complete minimal setup by:
* Create an `.env` file from the example: `cp .env.sample .env`)
* Edit `.env` to set a random string for `DJANGO_SECRET`
* Migrate your database using `heroku local:run python manage.py migrate`

Now you can run the webserver of your local heroku environment using `heroku local`.

To fully set up your uploader you will have to modify some files, as described below.

Expand Down
5 changes: 3 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ project_description: This template demonstrates how you can run your own Open Hu
# REQUIRED: Where can we find your project on Open Humans
oh_activity_page: https://www.openhumans.org/activity/your-project-url/

# REQUIRED: which URL will lead to your Open Humans uploader
app_base_url: http://127.0.0.1:5000
# Optional: which URL will lead to your Open Humans uploader
# Defaults to http://127.0.0.1:500 if not set.
# app_base_url: http://127.0.0.1:5000

# REQUIRED: Tell Open Humans what kind of data is being uploaded
file_description: This is an example file that doesnt have any meaning.
Expand Down
22 changes: 18 additions & 4 deletions oh_data_uploader/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False if os.getenv('DEBUG', '').lower() == 'false' else True

ALLOWED_HOSTS = ['*']
# Establish whether this is running on Heroku
ON_HEROKU = os.getenv('ON_HEROKU', '').lower() == 'true'

if ON_HEROKU:
ALLOWED_HOSTS = ['*']
else:
ALLOWED_HOSTS = []


# Read OH settings from .env/environment variables
Expand All @@ -43,7 +49,8 @@
YAML_CONFIG = yaml.load(''.join(yaml_content))
YAML_CONFIG['file_tags_string'] = str(YAML_CONFIG['file_tags'])

APP_BASE_URL = YAML_CONFIG['app_base_url']
# Set up base URL.
APP_BASE_URL = YAML_CONFIG.get('app_base_url', 'http://127.0.0.1:5000')
if APP_BASE_URL[-1] == "/":
APP_BASE_URL = APP_BASE_URL[:-1]

Expand Down Expand Up @@ -96,9 +103,16 @@
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

DATABASES = {'default': db_from_env}
if ON_HEROKU:
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES = {'default': db_from_env}


# Password validation
Expand Down

0 comments on commit ff14fcf

Please sign in to comment.