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

Feature: Set up stage environment in docker #613

Merged
merged 14 commits into from
Nov 11, 2024
Merged

Conversation

LoTerence
Copy link
Member

@LoTerence LoTerence commented Nov 10, 2024

Fixes #610

Goal: Set up the django backend for deployment to AWS.

As a developer, the develop branch should be ready to deploy to AWS at any moment.

Changes

  • Install daphne
  • Install whitenoise
  • Configure django security settings to prepare for prod deployment
  • remove frontend/dist folder from the repo and add to .gitignore
  • Updated /stage/Dockerfile and docker-compose.stage.yml. They are now fully functional and ready to deploy to a staging environment.
  • Added back in and updated /backend/entrypoint.sh from main-v1
  • Installed 3 python dependencies (pyyaml, inflection, and uritemplate) required for Django REST Framework to generate an OpenAPI schema.
  • Changed vite output directory to default /frontend/dist. The static frontend files should be copied over to the django app during the build phase in /stage/Dockerfile
  • Added SSL settings to stage.env.example

Misc:

  • ran some minor linting/formatting
  • refactor .gitignore files
  • refactor .dockerignore file
  • fix vite npm warning
  • fix npm security vulnerabilities

Testing

I fully tested on my local windows machine:

  • docker compose develop environment - fully functional
  • docker compose stage environment - working well

There is only one new bug in the stage environment - the frontend crashes when you try to visit the qualifiers page. This does not occur in dev mode. It has something to do with the static build and how react-router is set up. I will fix this later.

Starting a web server with daphne

  • We will deploy using ASGI (Asynchronous Server Gateway Interface)
    - Before, v1 was using WSGI and gunicorn to serve static files. Imo this approach is a bit outdated.
    - ASGI will allow for dynamic API calls and will overall be better for the long term maintainability of the project.
  • We will implement ASGI using Daphne
    • daphne is officially supported by Django:
      • "It’s written and maintained under the wing of the Django organization which is a big plus from a long-term support perspective. It should be maintained as long as Django exists."
    • Also supports runserver functionalities, so ASGI can be tested in dev mode:
      • "allows you to run your project under ASGI during development and benefit from all runserver functionalities"
  • Django REST Framework is compatible with ASGI
  • Whitenoise allows django + daphne to serve static files after running the django collectstatic command

daphne start server command:

poetry run daphne backend.asgi:application

OR

poetry run daphne -b 127.0.0.1 -p 8000 backend.asgi:application

Stage environment

Steps to start the staging environment on your local machine:

First, create a stage.env file in the /stage folder in your local repository.
You can use stage.env.example as a template. It can have all the same values as dev.env. The only significant difference as of now is ENVIRON=stage and DEBUG=false.

Next, open a terminal and simply run:

docker compose -f docker-compose.stage.yml up

Finally, open a browser and visit localhost:8000. You should see the CTJ website, and you should be able to access the API:

http://localhost:8000

http://localhost:8000/api/opportunities/

http://localhost:8000/api/healthcheck

Resources

@LoTerence LoTerence merged commit faf0849 into develop Nov 11, 2024
@LoTerence LoTerence deleted the feat/deploy-django branch November 11, 2024 02:01
@LoTerence LoTerence changed the title Feature: Deploy django, develop branch Feature: Set up stage environment in docker Nov 13, 2024
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.

1 participant