From 4b3816ce791ef363d1bfe7a6c51a8bff6ae957c8 Mon Sep 17 00:00:00 2001
From: James Kirk <james.kirk@noc.ac.uk>
Date: Thu, 9 Feb 2023 12:42:14 +0000
Subject: [PATCH] refactor: removed MQ_HOST outside of compose fix: added
 shebang to run-compose refactor: DATA_DIR used in container as well as
 external to it

---
 .env                       | 1 -
 README.md                  | 1 -
 docker/docker-compose.yaml | 6 +++---
 endpoints/token.py         | 5 +++--
 run-compose.sh             | 6 ++----
 5 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/.env b/.env
index 107450b..f6fbd39 100644
--- a/.env
+++ b/.env
@@ -1,4 +1,3 @@
-MQ_HOST=rmq
 # DATA_DIR=
 # SOAR_TOKEN_LIFETIME=
 # SOAR_TOKEN_SECRET=
\ No newline at end of file
diff --git a/README.md b/README.md
index 29b10f2..a2b15ce 100644
--- a/README.md
+++ b/README.md
@@ -27,7 +27,6 @@ Subsequent requests to the client endpoint return the client_id but not the secr
 
 Using `docker-compose` will mean that everything is setup automatically, this includes the `rabbitmq` container, the backbone API, and the backbone bus. The `run-compose.sh` script has been provided to simplify this even further - all you have to do is set whatever env vars you need in the `.env` file and then run `./run-compose.sh` (the defaults in `.env` are fine for local dev work, but ones not labelled `optional` will need setting in a production setting). The env vars are:
 
-- `MQ_HOST` - The hostname of our `rabbitmq` instance. This defaults to `rmq` which matches the container name of the `rabbitmq` instance in the compose yaml file
 - `DATA_DIR` - Where to mount the volume of the API container on your local system. This defaults to the result of `pwd`, which should be within the `communications-backbone` repo
 - `SOAR_TOKEN_LIFETIME` (Optional) - The number of hours until a newly created token expires
 - `SOAR_TOKEN_SECRET` (Optional) - A secret key used to encrypt/decrypt token data. If specified the value should be set using TokenModel.getKey()
diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml
index b2b629c..8e282ab 100644
--- a/docker/docker-compose.yaml
+++ b/docker/docker-compose.yaml
@@ -18,7 +18,7 @@ services:
     depends_on:
       - rabbitmq__local
     environment:
-      - MQ_HOST=${MQ_HOST}
+      - MQ_HOST=rmq
     command: "soar_bus.py"
 
   soar_api:
@@ -33,7 +33,7 @@ services:
     depends_on:
       - rabbitmq__local
     environment:
-      - MQ_HOST=${MQ_HOST}
+      - MQ_HOST=rmq
     volumes:
-      - ${DATA_DIR}/data:/data
+      - ${DATA_DIR}/data:${DATA_DIR}/data
     command: "api.py"
\ No newline at end of file
diff --git a/endpoints/token.py b/endpoints/token.py
index 0d040b4..9448bf8 100644
--- a/endpoints/token.py
+++ b/endpoints/token.py
@@ -1,9 +1,9 @@
 import json 
 from flask_restful import Resource, request, abort
 from marshmallow import Schema, fields
-import pika
 from models.token import TokenModel
 
+import os
 
 class TokenQuerySchema(Schema):
     client_id = fields.Str(required=True)
@@ -18,7 +18,8 @@ class Token(Resource):
     def __init__(self):
         self.schema = TokenQuerySchema()
         self.model = TokenModel()
-        with open("/data/clients.json", "r") as clients_file:
+        data_dir = os.getenv("DATA_DIR", "/data")
+        with open(data_dir + "/clients.json", "r") as clients_file:
             self.clients = json.load(clients_file)
     
     def get(self):
diff --git a/run-compose.sh b/run-compose.sh
index eb6ff2d..a5b16a0 100755
--- a/run-compose.sh
+++ b/run-compose.sh
@@ -1,3 +1,5 @@
+#! /usr/bin/env bash
+
 set -a
 source .env
 
@@ -5,8 +7,4 @@ if [[ -z "${DATA_DIR}" ]]; then
     DATA_DIR=$(pwd)
 fi
 
-if [[ -z "${MQ_HOST}" ]]; then
-    MQ_HOST=rmq
-fi
-
 docker-compose -f docker/docker-compose.yaml up --build
\ No newline at end of file
-- 
GitLab