|
79 | 79 |
|
80 | 80 | import dj_database_url
|
81 | 81 |
|
82 |
| -if os.environ.get('DATABASE_URL'): |
| 82 | +if os.path.isdir('/opt/app-root/secrets/database'): |
| 83 | + # Note that as we set what is read from the file as the default |
| 84 | + # only, it can still be overridden by the 'DATABASE_URL' environment |
| 85 | + # variable if that is also set. |
| 86 | + |
| 87 | + def database_url(): |
| 88 | + try: |
| 89 | + from urllib.parse import urlparse |
| 90 | + except ImportError: |
| 91 | + from urlparse import urlparse |
| 92 | + |
| 93 | + with open('/opt/app-root/secrets/database/database_name') as fp: |
| 94 | + database_name = fp.read().strip() |
| 95 | + |
| 96 | + with open('/opt/app-root/secrets/database/uri') as fp: |
| 97 | + uri = fp.read().strip() |
| 98 | + |
| 99 | + with open('/opt/app-root/secrets/database/username') as fp: |
| 100 | + username = fp.read().strip() |
| 101 | + |
| 102 | + with open('/opt/app-root/secrets/database/password') as fp: |
| 103 | + password = fp.read().strip() |
| 104 | + |
| 105 | + address = urlparse(uri) |
| 106 | + |
| 107 | + scheme = address.scheme |
| 108 | + hostname = address.hostname |
| 109 | + port = address.port |
| 110 | + |
| 111 | + return '%s://%s:%s@%s:%s/%s' % (scheme, username, password, |
| 112 | + hostname, port, database_name) |
| 113 | + |
| 114 | + DATABASES = { |
| 115 | + 'default': dj_database_url.config(default=database_url(), |
| 116 | + conn_max_age=600) |
| 117 | + } |
| 118 | + |
| 119 | +elif os.environ.get('DATABASE_URL'): |
83 | 120 | DATABASES = {
|
84 | 121 | 'default': dj_database_url.config(conn_max_age=600)
|
85 | 122 | }
|
|
0 commit comments