Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Feat: environment variable on config.
Browse files Browse the repository at this point in the history
  • Loading branch information
seijihirao committed Feb 20, 2019
1 parent 87b7aee commit acbb3e6
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 9 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ The current config special properties are the following:
"color": "bool //default=true"
},
"server": {
"port": "int //default=8888",
"port": "int //default=8080",
"cors": "string or false //default=false"
},
"utils": ["string //default=[]. list of utils in order to load"],
"(...)": "(...) //you can add any other key and access it via `api.config['my_key']`"
}
```
You can also use environment variables
like `$PORT` (for `PORT` env var), and set a default value if no env var is found
like `$PORT|8080` or `$PORT|8080|int` (if type is needed)

### ENDPOINTS
This will be your main dev dir
Expand Down
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from apys import server

server.start()
server.start('dev')
2 changes: 1 addition & 1 deletion apys/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.0.0'
__version__ = '3.1.0'
1 change: 0 additions & 1 deletion apys/apiobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,3 @@ def error(self, msg, to='debug', ex=False):

def __reduce__(self):
return ApiObject, (self.config_file, )

53 changes: 52 additions & 1 deletion apys/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def load(scope=None):
raise EnvironmentError('No config file found for {}'.format(scope))

with open(path, 'r') as config:
obj = json.loads(config.read())
obj = __get_env(json.loads(config.read()))
obj['scope'] = scope

# Default Values
Expand Down Expand Up @@ -69,3 +69,54 @@ def __fill_default_value(obj, key, default_value):
"""
if key not in obj:
obj[key] = default_value


def __get_env(obj):
"""
gets value from environment variable if `$` is preceded on value
i.e.:
```
{
"key": "$API_KEY"
"db_url": "$DB|localhost"
"port": "$PORT|8080|int"
}
```
* `key` will now have the value of the `API_KEY` environment variable, or '$API_KEY' if env var does not exists
* `db_url` will now have the value of the `DB` environment variable, or 'localhost' if env var does not exists
* `port` will now have the value of the `PORT` environment variable, or 8080 if env var does not exists
:param obj: object to format
:return: formatted object
"""
for key in obj:
if type(obj[key]) == dict:
obj[key] = __get_env(obj[key])
elif type(obj[key]) == str and obj[key][0] == '$':
env_var = obj[key][1:]

# Case no default value
if env_var.count('|') == 0:
if env_var in os.environ:
obj[key] = os.environ[env_var]

# Case default value
if env_var.count('|') == 1:
env_var, default_val = env_var.split('|')
if env_var in os.environ:
obj[key] = os.environ[env_var]
else:
obj[key] = default_val

# Case default value with type
if env_var.count('|') == 2:
env_var, default_val, default_type = env_var.split('|')
if env_var in os.environ:
obj[key] = os.environ[env_var]
else:
obj[key] = eval(default_type)(default_val)
return obj
4 changes: 2 additions & 2 deletions apys/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

DEFAULT_LOG = False
DEFAULT_COLOR = True
DEFAULT_PORT = 8888
DEFAULT_PORT = '$PORT|8080|int'
DEFAULT_CORS = False

##
Expand All @@ -22,7 +22,7 @@
DIR_FILTERS = 'filters'
DIR_UTILS = 'utils'

DEFAULT_PROD_PORT = 80
DEFAULT_PROD_PORT = '$PORT|80|int'
DEFAULT_PROD_CORS = False
DEFAULT_PROD_LOG_DIR = os.path.join('/', 'var', 'logs', 'apys')
DEFAULT_PROD_LOG_DEBUG_FILE = 'debug.log'
Expand Down
2 changes: 1 addition & 1 deletion demo/hello_world/config/dev.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"server": {
"port": 8080,
"port": "$PORT|8080|int",
"cors": "*"
},
"utils": [
Expand Down
2 changes: 1 addition & 1 deletion demo/hello_world/config/prod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"server": {
"port": 8080,
"port": "$PORT",
"cors": false
},
"utils": [
Expand Down

0 comments on commit acbb3e6

Please sign in to comment.