Skip to content

Commit

Permalink
Merge pull request #218 from atlanticwave-sdx/217.docker-app-fixes
Browse files Browse the repository at this point in the history
Docker and app fixes
  • Loading branch information
sajith authored Feb 2, 2024
2 parents 2dd0436 + c3c040a commit a949ef3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ RUN --mount=source=.git,target=.git,type=bind \
EXPOSE 8080

ENTRYPOINT ["python3"]
CMD ["-m", "uvicorn", "sdx_controller.app:app"]
CMD ["-m", "uvicorn", "sdx_controller.app:asgi_app", "--host", "0.0.0.0", "--port", "8080"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ $ python3 -m venv venv --upgrade-deps
$ source ./venv/bin/activate
$ pip3 install [--editable] .
$ source .env
$ python3 -m sdx_controller
$ flask --app sdx_controller.app:app run --debug
```

### Test topology files and connection requests
Expand All @@ -165,7 +165,7 @@ with Docker:

```console
$ docker run --rm -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:latest
$ docker run --rm -d --name mongo -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=guest -e MONGO_INITDB_ROOT_PASSWORD=guest mongo:3.7
$ docker run --rm -d --name mongo -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=guest -e MONGO_INITDB_ROOT_PASSWORD=guest mongo:7.0.5
```

Some environment variables are expected to be set for the tests to
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ classifiers = [
dependencies = [
"jsonschema == 3.2.0",
"connexion[swagger-ui] >= 2.14.1",
"asgiref >= 3.7.2",
"python_dateutil >= 2.8",
"setuptools >= 21.0.0",
"pika >= 1.2.0",
Expand Down
10 changes: 6 additions & 4 deletions sdx_controller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def create_app(run_listener: bool = True):
Create a connexion app.
The object returned is a Connexion App, which in turn contains a
Flask app, that we can run either with Flask or WSGI server such
as uvicorn::
Flask app, that we can run either with Flask or an ASGI server
such as uvicorn::
$ flask run sdx_server.app
$ uvicorn run sdx_server.app:app
$ flask run sdx_server.app:app
$ uvicorn run sdx_server.app:asgi_app
We also create a thread that subscribes to our message queue.
Occasionally it might be useful not to start the thread (such as
Expand All @@ -66,5 +66,7 @@ def create_app(run_listener: bool = True):

if run_listener:
create_rpc_thread(app)
else:
app.rpc_consumer = None

return app
26 changes: 25 additions & 1 deletion sdx_controller/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import atexit

from asgiref.wsgi import WsgiToAsgi
from flask import redirect

from sdx_controller import create_app

# This is a `connexion.apps.flask_app.FlaskApp` that we created using
# connexion.App().
application = create_app()

# The application above contains a `flask.app.Flask` object. We can
# run the app using flask, like so:
#
# $ flask --app sdx_controller.app:app run --debug
#
app = application.app

# We use WsgiToAsgi adapter so that we can use an ASGI server (such as
# uvicorn or hypercorn), like so:
#
# $ uvicorn sdx_controller.app:asgi_app --host 0.0.0.0 --port 8080
#
asgi_app = WsgiToAsgi(app)


@app.route("/", methods=["GET"])
def index():
return redirect("/SDX-Controller/1.0.0/ui/")


@atexit.register
def on_app_exit():
Expand All @@ -14,7 +37,8 @@ def on_app_exit():
We run a message queue consumer in a separate thread, and here we
signal the thread that we're exiting.
"""
application.rpc_consumer.stop_threads()
if application.rpc_consumer:
application.rpc_consumer.stop_threads()


if __name__ == "__main__":
Expand Down

0 comments on commit a949ef3

Please sign in to comment.