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

Refactor app into eligibility_server/app #19

Merged
merged 11 commits into from
Oct 5, 2021
Merged

Conversation

machikoyasuda
Copy link
Member

@machikoyasuda machikoyasuda commented Oct 4, 2021

What this PR does

close #12

Testing

Make sure you can:

  • - docker compose build server docker compose up server - open http://localhost:XXXX/healthcheck
  • - open in VSCode in remote container: enter command flask run -h 0.0.0.0 then open http://localhost:XXXX/healthcheck
  • - open in VSCode in remote container: get debugger mode running with F5
  • - open in VSCode in remote container: get debugger mode running with F5, change some server-side code, and the debugger should automatically restart
  • - open in VSCode in remove container: run pytest

Docs

image

The new port can be found from the VS Code port panel, or with the docker command: docker ps

image

@machikoyasuda
Copy link
Member Author

At the moment I'm running this PR's dev container locally with this change:
image

@machikoyasuda
Copy link
Member Author

Tried:

docker-compose run --rm -e DEBUGGER=True -e FLASK_APP=eligibility_server/app.py -e FLASK_ENV=development --service-ports dev python -m debugpy --wait-for-client --listen 0.0.0.0:5678 eligibility_server/app.py

and added a breakpoint in

if __name__ == "__main__":
    print(app)
    print("STUFF")
    breakpoint()
    app.run(host="0.0.0.0", debug=True)  # nosec

I was able to get the VS Code debugger working and look into the app var, etc., but - once I clicked ▶️, the server broke.

➜  eligibility-server git:(chore/move-into-folder) ✗ docker-compose run --rm -e DEBUGGER=True -e FLASK_APP=eligibility_server/app.py -e FLASK_ENV=development --service-ports dev python -m debugpy --wait-for-client --listen 0.0.0.0:5678 eligibility_server/app.py
Creating eligibility-server_dev_run ... done
<Flask 'app'>
STUFF
 * Serving Flask app "app" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://192.168.0.2:5000/ (Press CTRL+C to quit)
 * Restarting with stat
No module named app

@thekaveman
Copy link
Member

thekaveman commented Oct 5, 2021

So I just came across this: https://code.visualstudio.com/docs/python/tutorial-flask#_run-the-app-in-the-debugger

Which makes this whole thing much easier! I feel like this is new, debugpy is built in now!? 🤷

So two tricks that got this working for me:

.vscode/launch.json was slightly different from the above docs, but not by much:

{
  "name": "Flask: Eligibility Verification Server",
  "type": "python",
  "request": "launch",
  "module": "flask",
  "args": [
    "run",
    "--host",
    "0.0.0.0"
  ],
  "env": {
    "FLASK_DEBUG": "1",
    "FLASK_ENV": "development"
  }
}

This now starts the server on-demand with F5, and watch/reload is also active! Possible to start/stop/restart as well.

docker-compose.yml got much simpler:

dev:
  build:
    context: .
    dockerfile: Dockerfile.dev
  entrypoint: []
  command: sleep infinity
  image: eligibility_server:dev
  ports:
    - 5000
  volumes:
    - ./:/home/calitp/app/

command: sleep infinity ensures the devcontainer continues to run even if the server code crashes/dies, or when debug mode is stopped.

@thekaveman
Copy link
Member

thekaveman commented Oct 5, 2021

There's a similar write-up for Django, which I tried with benefits and... it worked!

https://code.visualstudio.com/docs/python/tutorial-django#_create-a-debugger-launch-profile

See cal-itp/benefits#126

@machikoyasuda machikoyasuda changed the title WIP Refactor app into eligibility_server/app Oct 5, 2021
@machikoyasuda machikoyasuda marked this pull request as ready for review October 5, 2021 18:25
Copy link
Member

@thekaveman thekaveman left a comment

Choose a reason for hiding this comment

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

Great job! Few small changes, added some links to more info about Docker entrypoint/command.

.vscode/launch.json Outdated Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
docker-compose.yml Show resolved Hide resolved
docker-compose.yml Show resolved Hide resolved
Copy link
Member

@thekaveman thekaveman left a comment

Choose a reason for hiding this comment

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

Forgot to add:

You can remove debugpy from the requirements.txt file too!

Copy link
Member

@thekaveman thekaveman left a comment

Choose a reason for hiding this comment

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

🚀 🎸

@machikoyasuda machikoyasuda merged commit 0f70fdf into main Oct 5, 2021
@machikoyasuda machikoyasuda deleted the chore/move-into-folder branch October 5, 2021 20:29
@thekaveman thekaveman added this to the October 2021 milestone Oct 6, 2021
@angela-tran angela-tran mentioned this pull request Nov 14, 2022
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor server code into subdirectory
2 participants