Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include the Monarch link app as a service on IMAGE-server #894

Merged
merged 16 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions .github/workflows/monarch-link-app-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Monarch Link App Service
on:
push:
branches: [ main ]
tags: [ "service-monarch-link-app-[0-9]+.[0-9]+.[0-9]+" ]
paths: [ "services/monarch-link-app/**" ]
pull_request:
branches: [ main ]
paths: [ "services/monarch-link-app/**" ]
workflow_run:
workflows: [ "Schemas (Trigger)" ]
VenissaCarolQuadros marked this conversation as resolved.
Show resolved Hide resolved
types:
- completed
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: shared-reality-lab/image-service-monarch-link-app
jobs:
lint:
name: PEP 8 style check.
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install flake8
run: pip install flake8
- name: Check with flake8
run: python -m flake8 ./services/monarch-link-app --show-source
build-and-push-image:
name: Build and Push to Registry
needs: lint
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
submodules: true
- name: Log into GHCR
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get Correct Tags
run: |
if [[ ${{ github.ref }} =~ ^refs/tags/service-monarch-link-app-[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "TAGGED=true" >> $GITHUB_ENV
else
echo "TAGGED=false" >> $GITHUB_ENV
fi
- name: Get timestamp
run: echo "timestamp=$(date -u +'%Y-%m-%dT%H.%M')" >> $GITHUB_ENV
- name: Extract metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
flavor: |
latest=${{ env.TAGGED }}
tags: |
type=match,enable=${{ env.TAGGED }},priority=300,pattern=service-monarch-link-app-(\d+.\d+.\d+),group=1
type=raw,priority=200,value=unstable
type=raw,priority=100,value=${{ env.timestamp }}
labels: |
org.opencontainers.image.title=IMAGE Service Monarch Link App
org.opencontainers.image.description=Service to link Monarch client with tactile authoring tool.
org.opencontainers.image.authors=IMAGE Project <[email protected]>
org.opencontainers.image.licenses=AGPL-3.0-or-later
maintainer=IMAGE Project <[email protected]>
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
file: ./services/monarch-link-app/Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
5 changes: 5 additions & 0 deletions build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ services:
context: .
dockerfile: services/espnet-tts-fr/Dockerfile
image: "espnet-tts-fr:latest"
monarch-link-app-service:
build:
context: .
dockerfile: services/monarch-link-app/Dockerfile
image: "monarch-link-app:latest"
line-charts-preprocessor:
build:
context: .
Expand Down
15 changes: 14 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,20 @@ services:
image: ghcr.io/shared-reality-lab/image-handler-svg-action-recognition:${REGISTRY_TAG}
labels:
ca.mcgill.a11y.image.handler: enable


monarch-link-app:
profiles: [test, default]
image: "ghcr.io/shared-reality-lab/image-service-monarch-link-app:${REGISTRY_TAG}"
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.http.routers.monarch-link-app.rule=Host(`monarch.unicorn.cim.mcgill.ca`)"
- "traefik.http.routers.monarch-link-app.tls.certresolver=myresolver"
- traefik.docker.network=traefik
environment:
- SERVER_NAME=unicorn.cim.mcgill.ca
networks:
- traefik
# end - unicorn exclusive services
volumes:
sc-store:
Expand Down
19 changes: 19 additions & 0 deletions services/monarch-link-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM python:3.10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current versions is 3.13 - why are you using 3.10?


RUN apt-get install libcairo2

RUN adduser --disabled-password python
WORKDIR /usr/src/app
ENV PATH="/home/python/.local/bin:${PATH}"

RUN pip install --upgrade pip
COPY /services/monarch-link-app/requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

COPY /services/monarch-link-app/ /usr/src/app

RUN chown -R python:python /usr/src/app

EXPOSE 80
USER python
CMD ["gunicorn", "app:app", "-b", "0.0.0.0:80", "--capture-output", "--log-level=debug" ]
16 changes: 16 additions & 0 deletions services/monarch-link-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
This is the app used to connect IMAGE-TactileAuthoring to IMAGE-Monarch.

# Monarch Link App

![license: AGPL](https://img.shields.io/badge/license-AGPL-success) [GitHub Container Registry Package](https://github.com/Shared-Reality-Lab/IMAGE-server/pkgs/container/image-service-monarch-link-app)

## Overview

This is the containerized version of a web app used to publish content from [IMAGE-TactileAuthoring](https://github.com/Shared-Reality-Lab/IMAGE-TactileAuthoring) which can then be fetched by [IMAGE-Monarch client](https://github.com/Shared-Reality-Lab/IMAGE-Monarch). It stores the data in a json file data.json which is recreated each time the container is restarted.

This container runs on port 80.

## Endpoints
- POST `https://monarch.unicorn.cim.mcgill.ca/create/<subscribed_code>` where the body is a JSON object with the key `data` set to the SVG in base64 format, `secret` set to the secret key, and `layer` set to the layer to be shown or None if no default layer is selected.
This can be used to create a new channel `<subscribed_code>` if one doesn't already exist. The data is stored in data.json. If `<subscribed_code>` already exists the `data` field corresponding it is changed only if the `secret` field matches the secret key when the channel was created.
- GET `https://monarch.unicorn.cim.mcgill.ca/display/<subscribed_code>` returns a JSON object with the [tactile svg schema](https://github.com/Shared-Reality-Lab/IMAGE-server/blob/24a41b4f36a8c89b1a94d7c31388703ece8c81c7/renderers/tactilesvg.schema.json).
99 changes: 99 additions & 0 deletions services/monarch-link-app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Copyright (c) 2024 IMAGE Project, Shared Reality Lab, McGill University
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# and our Additional Terms along with this program.
# If not, see
# <https://github.com/Shared-Reality-Lab/IMAGE-server/blob/main/LICENSE>.

from flask import Flask, request, jsonify, abort, Response
from flask_cors import CORS, cross_origin
import logging
import hashlib
import json

app = Flask(__name__)
logging.basicConfig(level=logging.DEBUG)

CORS(
app, resources={r"/*": {"origins": "*"}}
) # CORS allowed for all domains on all routes

try:
with open("data.json", 'x') as file:
json.dump(dict(), file)
except FileExistsError:
logging.debug("The file already exists")


def write_data(svgData):
with open("data.json", "w") as outfile:
json.dump(svgData, outfile)


def read_data():
with open('data.json', 'r') as openfile:
try:
return json.load(openfile)
except Exception:
return dict()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK for quick prototyping, but this should not be used in production. Would suggest something like sqlite3 that may scale better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #897 to keep track of this



@app.route("/create/<id>", methods=["POST"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there restrictions on what "id" can be here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also - any reason why the create and update methods were combined?

Copy link
Member Author

@VenissaCarolQuadros VenissaCarolQuadros Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't limited this currently but I typically use a 6-digit numeric code. Flask has options to limit this something generic like 'int' or we can also build custom 'converters' if we want to enforce this.

EDIT: I have added in a custom converter.

@cross_origin()
def render(id):
if request.method == "POST":
req_data = request.get_json()
svgData = read_data()
if id in svgData:
if (svgData[id])["secret"] == req_data["secret"]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally considered bad practice to save this in plain text...compare using something like bcrypt?

svgData[id] = {"secret": req_data["secret"],
"data": req_data["data"],
"layer": req_data["layer"]}
write_data(svgData)
return jsonify("Graphic in channel "+id+" has been updated!")
else:
return jsonify("Unauthorized access to existing channel!")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unauthorized access should return 401 code.

else:
svgData[id] = {"secret": req_data["secret"],
"data": req_data["data"],
"layer": req_data["layer"]}
write_data(svgData)
return jsonify("New channel created with code "+id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the model we discussed earlier, the ID and secret was generated on the server and returned to the client. Create only allowed the client to specify a title. Any reason for the changes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, it looks like I've somehow completely messed up the implementation here! :')
Will probably need to touch base in person to sort this out.



@app.route("/display/<id>", methods=["GET"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for the change in name from receive? Also should check the ID.

@cross_origin()
def display(id):
if request.method == "GET":
svgData = read_data()
if id in svgData:
response = Response()
response.mimetype = "application/json"
response.set_data(json.dumps({"renderings": [
{"data": {"graphic": svgData[id]["data"],
"layer": svgData[id]["layer"]}}]}))
response.add_etag(hashlib.md5(
(svgData[id]["data"]+svgData[id]["layer"]).encode()))
response.make_conditional(request)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

return response
else:
return abort(404)


@app.route("/", methods=["POST", "GET"])
@cross_origin()
def home():
return "Hi"


if __name__ == "__main__":
app.run(host="0.0.0.0", port=80, debug=True)
15 changes: 15 additions & 0 deletions services/monarch-link-app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
click==8.1.3
colorama==0.4.6
Flask==2.2.2
Flask-Cors==4.0.2
Flask-Login==0.6.2
Flask-SQLAlchemy==3.0.2
greenlet==2.0.1
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
six==1.16.0
SQLAlchemy==1.4.43
SQLAlchemy-Utils==0.38.3
Werkzeug==2.2.2
gunicorn==22.0.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest setting only what you use directly with semver and allowing some newer verisons. pip freeze output is almost always overly restrictive.

Loading