From cfc34dfb378fe66937d381ab50b48dd95ae62593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Sun, 4 Nov 2018 13:38:53 +0100 Subject: [PATCH] Add application config endpoint --- app/api/blueprint.py | 3 +- app/api/resources/app.py | 62 +++++++++++++++++++ .../swagger/get_app_config_spec.yaml | 14 +++++ .../swagger/get_mqtt_config_spec.yaml | 1 - app/swagger/template.yaml | 55 ++++++++++++++++ config.py | 3 + 6 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 app/api/resources/swagger/get_app_config_spec.yaml diff --git a/app/api/blueprint.py b/app/api/blueprint.py index 42cb530..8b9065e 100644 --- a/app/api/blueprint.py +++ b/app/api/blueprint.py @@ -32,7 +32,7 @@ def add_resources(): DashboardListResource, DashboardWidgetResource, DashboardWidgetListResource) - from .resources.app import MqttConfigResource + from .resources.app import MqttConfigResource, AppConfigResource api.add_resource(AccountResource, '/v1/accounts/') api.add_resource(AccountListResource, '/v1/accounts') @@ -74,6 +74,7 @@ def add_resources(): api.add_resource(DashboardWidgetListResource, '/v1/dashboards//widgets') api.add_resource(MqttConfigResource, '/v1/config/mqtt') + api.add_resource(AppConfigResource, '/v1/config') add_resources() diff --git a/app/api/resources/app.py b/app/api/resources/app.py index 4e874b3..7745294 100644 --- a/app/api/resources/app.py +++ b/app/api/resources/app.py @@ -29,6 +29,38 @@ class MqttConfigSchema(BaseResourceSchema): endpoints = fields.Nested(BasicMqttEndpointSchema, many=True) +class BasicVersionInfoSchema(Schema): + name = fields.String() + build_number = fields.String() + + +class VersionInfoSchema(BaseResourceSchema, BasicVersionInfoSchema): + pass + + +class BasicFrontendInfoSchema(Schema): + url = fields.String() + + +class FrontendInfoSchema(BaseResourceSchema, BasicFrontendInfoSchema): + pass + + +class BasicEmailInfoSchema(Schema): + mailer_account = fields.String() + contact_accounts = fields.List(fields.String, many=True) + + +class EmailInfoSchema(BaseResourceSchema, BasicEmailInfoSchema): + pass + + +class AppConfigSchema(BaseResourceSchema): + version = fields.Nested(BasicVersionInfoSchema) + frontend = fields.Nested(BasicFrontendInfoSchema) + email = fields.Nested(BasicEmailInfoSchema) + + def get_mqtt_broker_info(config): return { 'url': config['MQTT_BROKER_URL'], @@ -68,6 +100,26 @@ def get_mqtt_endpoints(config): ] +def get_app_version_info(config): + return { + 'name': config['APP_VERSION'], + 'build_number': config['APP_RELEASE_VERSION_STRING'] + } + + +def get_frontend_info(config): + return { + 'url': config['FRONTEND_URL'] + } + + +def get_email_info(config): + return { + 'mailer_account': config['MAIL_DEFAULT_SENDER'], + 'contact_accounts': config['MAIL_CONTACT_ACCOUNTS'] + } + + class MqttConfigResource(ProtectedResource): @swag_from('swagger/get_mqtt_config_spec.yaml') def get(self): @@ -75,3 +127,13 @@ class MqttConfigResource(ProtectedResource): 'broker': get_mqtt_broker_info(app.config), 'endpoints': get_mqtt_endpoints(app.config) }, 200 + + +class AppConfigResource(ProtectedResource): + @swag_from('swagger/get_app_config_spec.yaml') + def get(self): + return AppConfigSchema().dump({ + 'version': get_app_version_info(app.config), + 'frontend': get_frontend_info(app.config), + 'email': get_email_info(app.config) + }), 200 diff --git a/app/api/resources/swagger/get_app_config_spec.yaml b/app/api/resources/swagger/get_app_config_spec.yaml new file mode 100644 index 0000000..beb47d9 --- /dev/null +++ b/app/api/resources/swagger/get_app_config_spec.yaml @@ -0,0 +1,14 @@ +Gets server app configuration and description +--- +tags: + - Config +responses: + 200: + description: Success + schema: + type: object + required: + - content + properties: + content: + $ref: '#/definitions/AppConfig' diff --git a/app/api/resources/swagger/get_mqtt_config_spec.yaml b/app/api/resources/swagger/get_mqtt_config_spec.yaml index 8dccfa4..195a6a8 100644 --- a/app/api/resources/swagger/get_mqtt_config_spec.yaml +++ b/app/api/resources/swagger/get_mqtt_config_spec.yaml @@ -13,4 +13,3 @@ responses: properties: content: $ref: '#/definitions/MqttConfig' - diff --git a/app/swagger/template.yaml b/app/swagger/template.yaml index 2b94189..5371f1b 100644 --- a/app/swagger/template.yaml +++ b/app/swagger/template.yaml @@ -328,6 +328,61 @@ definitions: name: ref: '#definitions/genericname' + VersionInfo: + type: object + required: + - name + - build_number + properties: + name: + type: string + description: Version name following semantic versioning + example: 0.4.3 + build_number: + type: string + description: Number of current build/release + example: v72 + + FrontendInfo: + type: object + required: + - url + properties: + url: + type: string + description: URL of frontend used with this backend. + example: https://iot-frontend-app.herokuapp.com + + EmailConfigInfo: + type: object + required: + - mailer_account + - contact_accounts + properties: + mailer_account: + type: string + description: Account used to send emails to users + example: final.iot.backend.mailer@gmail.com + contact_accounts: + type: string + description: Emails used to contact developers + example: [] + + AppConfig: + type: object + required: + - version + - frontend + - email + properties: + version: + $ref: '#/definitions/VersionInfo' + frontend: + $ref: '#/definitions/FrontendInfo' + email: + $ref: '#/definitions/EmailConfigInfo' + + MqttBroker: type: object required: diff --git a/config.py b/config.py index 83a8cb9..5edd4fb 100644 --- a/config.py +++ b/config.py @@ -3,6 +3,8 @@ import os # App configuration DEBUG = os.environ['DEBUG'] APP_VERSION = '0.4.2' +APP_RELEASE_VERSION_STRING = (os.environ.get('HEROKU_RELEASE_VERSION') + or 'Unknown') # Define the application directory BASE_DIR = os.path.abspath(os.path.dirname(__file__)) @@ -56,6 +58,7 @@ MAIL_PASSWORD = os.environ['APP_MAIL_PASSWORD'] # mail accounts MAIL_DEFAULT_SENDER = 'final.iot.backend.mailer@gmail.com' +MAIL_CONTACT_ACCOUNTS = ['esarajcic1@etf.unsa.ba', 'valjic1@etf.unsa.ba'] # frontend FRONTEND_URL = (os.environ.get('IOT_FRONTEND_URL') or