diff --git a/.gitignore b/.gitignore index a4d3d6d..518be01 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ todo env __pycache__ +redis-data diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9268a9e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,29 @@ +version: '3.9' + +services: + web: + build: + context: . + dockerfile: ./web/Dockerfile + restart: unless-stopped + ports: + - 8464:80 + environment: + - CLIENT_ID=${CLIENT_ID} + - CLIENT_SECRET=${CLIENT_SECRET} + - BASE_URL=http://localhost:8464 + + redis: + image: redis + restart: unless-stopped + volumes: + - './redis-data:/data' + + populater: + build: + context: . + dockerfile: ./populater/Dockerfile + restart: unless-stopped + environment: + - CLIENT_ID=${CLIENT_ID} + - CLIENT_SECRET=${CLIENT_SECRET} diff --git a/populater/Dockerfile b/populater/Dockerfile new file mode 100644 index 0000000..61e7938 --- /dev/null +++ b/populater/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3 + +WORKDIR /usr/src/app + +COPY populater/requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt + +COPY populater/src . +ADD common ./common + +CMD [ "python", "-u", "app.py" ] diff --git a/requirements.txt b/populater/requirements.txt similarity index 93% rename from requirements.txt rename to populater/requirements.txt index e6c28ff..877cfbf 100644 --- a/requirements.txt +++ b/populater/requirements.txt @@ -3,6 +3,7 @@ certifi==2022.12.7 charset-normalizer==2.1.1 click==8.1.3 Flask==2.2.2 +gunicorn==20.1.0 idna==3.4 itsdangerous==2.1.2 Jinja2==3.1.2 diff --git a/populater/src/app.py b/populater/src/app.py index af773dc..5427486 100755 --- a/populater/src/app.py +++ b/populater/src/app.py @@ -16,7 +16,7 @@ CLIENT_SECRET = os.environ.get('CLIENT_SECRET', None) if CLIENT_SECRET is None: raise ValueError("CLIENT_SECRET cannot be None, set using environment variable") -db = Db('localhost', 6379, 0, CLIENT_ID, CLIENT_SECRET) +db = Db('redis', 6379, 0, CLIENT_ID, CLIENT_SECRET) def get_args(): diff --git a/populater/src/common b/populater/src/common deleted file mode 120000 index 248927d..0000000 --- a/populater/src/common +++ /dev/null @@ -1 +0,0 @@ -../../common/ \ No newline at end of file diff --git a/web/Dockerfile b/web/Dockerfile new file mode 100644 index 0000000..e85b033 --- /dev/null +++ b/web/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3 + +ENV PYTHONUNBUFFERED=value + +WORKDIR /usr/src/app + +COPY web/requirements.txt ./ + +RUN pip install --no-cache-dir -r requirements.txt + +COPY web/src . + +ADD common ./common + +CMD [ "gunicorn", "-b 0.0.0.0:80", "app:app" ] diff --git a/web/requirements.txt b/web/requirements.txt new file mode 100644 index 0000000..877cfbf --- /dev/null +++ b/web/requirements.txt @@ -0,0 +1,15 @@ +async-timeout==4.0.2 +certifi==2022.12.7 +charset-normalizer==2.1.1 +click==8.1.3 +Flask==2.2.2 +gunicorn==20.1.0 +idna==3.4 +itsdangerous==2.1.2 +Jinja2==3.1.2 +MarkupSafe==2.1.1 +redis==4.4.0 +requests==2.28.1 +six==1.16.0 +urllib3==1.26.13 +Werkzeug==2.2.2 diff --git a/web/src/app.py b/web/src/app.py index db7e698..d60b3ba 100644 --- a/web/src/app.py +++ b/web/src/app.py @@ -23,7 +23,11 @@ CLIENT_SECRET = os.environ.get('CLIENT_SECRET', None) if CLIENT_SECRET is None: raise ValueError("CLIENT_SECRET cannot be None, set using environment variable") -db = Db('localhost', 6379, 0, CLIENT_ID, CLIENT_SECRET) +BASE_URL = os.environ.get('BASE_URL', None) +if BASE_URL is None: + raise ValueError("BASE_URL cannot be None, set using environtment variable") + +db = Db('redis', 6379, 0, CLIENT_ID, CLIENT_SECRET) SPOTIFY_AUTHORIZE_ENDPOINT = os.environ.get('SPOTIFY_AUTHORIZE_ENDPOINT', 'https://accounts.spotify.com/authorize?') SPOTIFY_TOKEN_ENDPOINT = os.environ.get('SPOTIFY_AUTHORIZE_ENDPOINT', 'https://accounts.spotify.com/api/token') @@ -60,9 +64,10 @@ def index(): loginurl = SPOTIFY_AUTHORIZE_ENDPOINT + urllib.parse.urlencode({ 'client_id': CLIENT_ID, 'scope': SCOPES, - 'redirect_uri': 'http://localhost:5000' + url_for('cb'), + 'redirect_uri': BASE_URL + url_for('cb'), 'response_type': 'code' - }) + }), + message = { 'type':'', 'text': ''} )) resp.set_cookie( 'user_secret', @@ -130,7 +135,7 @@ def cb(): data = { 'code': spotify_code, 'grant_type': 'authorization_code', - 'redirect_uri': 'http://localhost:5000' + url_for('cb'), + 'redirect_uri': BASE_URL+ url_for('cb'), } headers = { 'Authorization': f'Basic {authstring}', @@ -139,6 +144,7 @@ def cb(): token_rq = rq.post(f"{SPOTIFY_TOKEN_ENDPOINT}", data = data, headers = headers) if token_rq.status_code != rq.codes.ok: + print(f"getting token failed {token_rq.text=}") return render_template("index_authorised.html", message={ 'type': 'error', 'text': f"unable to authenticate with spotify. please try again later." @@ -151,6 +157,7 @@ def cb(): me_rq = rq.get(f"{SPOTIFY_API_ENDPOINT}/me", headers = { 'Authorization': f"Bearer {spotify_token}" }) if me_rq.status_code != 200: + print(f"getting user info failed {me_rq.text=}") return render_template("index_authorised.html", message={ 'type': 'error', 'text': f"unable to authenticate with spotify. please try again later." diff --git a/web/src/common b/web/src/common deleted file mode 120000 index 248927d..0000000 --- a/web/src/common +++ /dev/null @@ -1 +0,0 @@ -../../common/ \ No newline at end of file