Skip to content

Commit

Permalink
Merge pull request #28 from ral-facilities/13_setup_config_file
Browse files Browse the repository at this point in the history
Use a config file
  • Loading branch information
keiranjprice101 authored Aug 5, 2019
2 parents 27cfed0 + fb96925 commit 251d2bd
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 20 deletions.
27 changes: 12 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ The required python libraries:
- [requests](https://2.python-requests.org/en/master/)

## Setup and running the API
The database connection needs to be set up first, currently it is set in `common/constants.py`

```python
class Constants:
DATABASE_URL = "mysql+pymysql://root:rootpw@localhost:13306/icatdb"
```
The database connection needs to be set up first. This is set in config.json


To run the API from the command line, the enviroment variable `FLASK_APP` should be set to `src/main.py`. Once this is
Expand Down Expand Up @@ -83,15 +78,17 @@ This is illustrated below.
│ ├── swagger
│ │ └── openapi.yaml
│ └── main.py
└── test
├── resources
│ ├── entities
│ │ └──test_<entity>.py
│ └── non_entities
│ └── test_<non_entity>.py
└── test_base
├── constants.py
└── rest_test.py
├── test
│ ├── resources
│ │ ├── entities
│ │ │ └──test_<entity>.py
│ │ └── non_entities
│ │ └── test_<non_entity>.py
│ └── test_base
│ ├── constants.py
│ └── rest_test.py
├── logs.log
└── config.json
`````
#### Main:
The main entry point is in `/src/main.py`. This is where each endpoint route is defined and its
Expand Down
31 changes: 31 additions & 0 deletions common/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json
import sys


class Config(object):

def __init__(self):
with open("../config.json") as target:
self.config = json.load(target)
target.close()

def get_db_url(self):
try:
return self.config["DB_URL"]
except:
sys.exit("Missing config value, DB_URL")

def get_log_level(self):
try:
return self.config["log_level"]
except:
sys.exit("Missing config value, log_level")

def is_debug_mode(self):
try:
return self.config["debug_mode"]
except:
sys.exit("Missing config value, debug_mode")


config = Config()
5 changes: 4 additions & 1 deletion common/constants.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
from common.config import config


class Constants:
DATABASE_URL = "mysql+pymysql://root:rootpw@localhost:13306/icatdb"
DATABASE_URL = config.get_db_url()
6 changes: 3 additions & 3 deletions common/logger_setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging.config

from common.config import config

log_level = "DEBUG"
LOG_FILE_NAME = "../logs.log"
Expand All @@ -9,15 +9,15 @@
"format": "[%(asctime)s] {%(module)s:%(filename)s:%(funcName)s:%(lineno)d} %(levelname)s -%(message)s ",
}},
"handlers": {"default": {
"level": "DEBUG",
"level": config.get_log_level(),
"formatter": "default",
"class": "logging.handlers.RotatingFileHandler",
"filename": LOG_FILE_NAME,
"maxBytes": 5000000,
"backupCount": 10
}},
"root": {
"level": log_level,
"level": config.get_log_level(),
"handlers": ["default"]
}
}
Expand Down
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"DB_URL": "mysql+pymysql://root:rootpw@localhost:13306/icatdb",
"log_level": "WARN",
"debug_mode": false
}




9 changes: 9 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"DB_URL": "mysql+pymysql://root:rootpw@localhost:13306/icatdb",
"log_level": "WARN",
"debug_mode": false
}




3 changes: 2 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Flask
from flask_restful import Api

from common.config import config
from common.logger_setup import setup_logger
from src.resources.entities.dataset_type_endpoints import *
from src.resources.entities.applications_endpoints import *
Expand Down Expand Up @@ -200,4 +201,4 @@


if __name__ == "__main__":
app.run(debug=True)
app.run(debug=config.is_debug_mode())

0 comments on commit 251d2bd

Please sign in to comment.