Skip to content

Commit

Permalink
104 deploy to heroku (#112)
Browse files Browse the repository at this point in the history
* deployment dependencies

* node dependencies

* allowing heroku host

* updating modules

* reworking procfile

* reworking procfile

* updating static paths

* updating static paths

* update whitenoise dependency

* adding favicon

* add favicon

* base directory wrong

* base directory wrong

* fixing staticfiles for prod

* adding heroku domain

* updating for all host paths

* no more hashing in requirements.txt

* testing wo static deps

* the dumbest mistake ever

* revert to original viiews

* adding test route for deployment testing

* not cmopressing static anymroe

* reworking to make sure app is installed corectly

* reworking to make sure app is installed corectly

* make sure gunicorn is accessible

* using procfile to make sure settings are set correctly

* migrate back to procfile

* back to requirements.txt

* back to requirements.txt

* debug true

* basetemplate revise

* debug off now that things are working

* documented deployment process

* typo fix

* linting

* not going down the test fixing route now
  • Loading branch information
meganhmoore committed May 17, 2024
1 parent 5648e50 commit d34d299
Show file tree
Hide file tree
Showing 18 changed files with 1,107 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.env
*.env
app/staticfiles/

.DS_Store
# Byte-compiled / optimized / DLL files
Expand Down
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: pip install . && cd app && gunicorn geodjango.wsgi
28 changes: 22 additions & 6 deletions app/geodjango/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,34 @@
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = GEODJANGO_SECRET_KEY

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# The `DYNO` env var is set on Heroku CI, but it's not a real Heroku app, so we have to
# also explicitly exclude CI:
# https://devcenter.heroku.com/articles/heroku-ci#immutable-environment-variables
IS_HEROKU_APP = "DYNO" in os.environ and not "CI" in os.environ

ALLOWED_HOSTS = []
# SECURITY WARNING: don't run with debug turned on in production!
if not IS_HEROKU_APP:
DEBUG = True

"""ONLY uncomment the following lines when actively debugging a deployment problem"""
# else:
# DEBUG = True

# Application definition
# On Heroku, it's safe to use a wildcard for `ALLOWED_HOSTS``, since the Heroku
# router performs validation of the Host header in the incoming HTTP request.
if IS_HEROKU_APP:
ALLOWED_HOSTS = ["*"]
else:
ALLOWED_HOSTS = []

INSTALLED_APPS = [
"whitenoise.runserver_nostatic",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
Expand All @@ -57,6 +69,7 @@
]

MIDDLEWARE = [
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
Expand Down Expand Up @@ -139,8 +152,11 @@

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

# required for Heroku to access static features
STATIC_URL = "static/"
print(f"Base dir is: {BASE_DIR}")
STATIC_ROOT = BASE_DIR / "staticfiles"


# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
Expand Down
Empty file modified app/geodjango/wsgi.py
100644 → 100755
Empty file.
Binary file added app/route_rangers_api/static/favicon.ico
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
{% load static %}
<link rel="icon"href="{% static 'favicon.ico' %}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Plan Our Transit</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<html lang="en">

<head>
{% load static %}
<meta charset="UTF-8">
{% load static %}
<link rel="icon"href="{% static 'favicon.ico' %}">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Plan Our Transit</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
Expand Down
3 changes: 1 addition & 2 deletions app/route_rangers_api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ def test_hello_view(client):
assert response.status_code == 200


import pytest

from app.scripts.extract_scheduled_gtfs import (
# ingest_gtfs_feed,
CTA_URL,
Expand Down Expand Up @@ -118,6 +116,7 @@ def test_combine_different_feeds(cta_feed, metra_feed):
from app.scripts.extract_nyc_data import DATASETS, NY_TZ, extract_daily_data
import datetime
from parameterized import parameterized
from unittest import TestCase


class ExtractNYData(TestCase):
Expand Down
1 change: 1 addition & 0 deletions app/route_rangers_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
app_name = "app"

urlpatterns = [
path("test", views.test, name="test"),
path("", views.home, name="home"),
path("about/", views.about, name="about"),
path("dashboard/<str:city>", views.dashboard, name="dashboard"),
Expand Down
5 changes: 4 additions & 1 deletion app/route_rangers_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
from django.templatetags.static import static
from django.contrib.gis.geos import GEOSGeometry, MultiLineString, LineString


from app.route_rangers_api.utils.city_mapping import CITY_CONTEXT
from route_rangers_api.models import TransitRoute, TransitStation

import json


def test(request):
return HttpResponse("""This is a test route without any html/JS/static stuff""")


def home(request):
context = {"cities_class": "cs-li-link cs-active", "about_class": "cs-li-link"}
return render(request, "cities.html", context)
Expand Down
1 change: 0 additions & 1 deletion django
Submodule django deleted from b79ac8
43 changes: 41 additions & 2 deletions documentation/architecture-and-deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,49 @@ stations from Chicago's Metra system; it will be expanded out to ingest more kin
of data in the coming days.

### Frontend
To run the webserver (again make sure you have dependencies installed and `.env` up to date)
To run the webserver locally (again make sure you have dependencies installed and `.env` up to date)

Run:
```
cd app/
python -m manage runserver
```
```

#### Heroku Deployment
To deploy the production service, we are using heroku.

Note: we applied for heroku education credits to deploy for free to heroku (this is a one time task)

If starting from scratch and setting up a new heroku application most of it is easy to do through the heroku web interface:

1. If starting a new heroku application, follow the instructions [here](https://devcenter.heroku.com/articles/ getting-started-with-python#set-up) which require you to `$ brew install heroku` for mac and `$ heroku login` to enter your heroku login credentials
2. From the web interface in the application `settings` add the following buildpacks: https://github.com/heroku/heroku-geo-buildpack.git, heroku/python, heroku/nodejs (the geo buildpack must come first to set up the gdal stuff prior to django).
3. In the same web interface `settings` configure the `config vars` by copying in the keys and values from your `.env` file

You are now ready to deploy, the following steps are repeated every time you deploy:
1. From the root of your repo (i.e. `RouteRangers/`), generate a new `requirements.txt` to reflect any poetry updates:
```
poetry export --without-hashes --format=requirements.txt > requirements.txt
```
2. Then to deploy, commit your changes to the branch you are working on and are ready to deploy:
```
git add --all
git commit -m "<your commit message>"
git push
```
And then push to heroku:
```
git push heroku <your current branch>:main
```
Or if you are on main I think you can just run:
```
git push heroku main
```

Then if the full deployment script runs through you can check the given url that the changes have been reflected.

If there are no errors in deployment but the frontend doesn't run as expected:
1. Check the logs `$ heroku logs --tail`
2. If the logs don't show anything obvious and you are just getting a vague error in the frontend (ex. error 500), check the `app/geodjango/settings.py` file and uncomment the heroku `DEBUG=True` around line 48-49 to surface errors more effectively (only while debugging!!)


File renamed without changes.
File renamed without changes.
Loading

0 comments on commit d34d299

Please sign in to comment.