├── backend ├── char_count │ ├── __init__.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── admin.py │ ├── tests.py │ ├── apps.py │ └── views.py ├── hello_world │ ├── __init__.py │ ├── settings │ │ ├── __init__.py │ │ ├── development.py │ │ ├── production.py │ │ └── base.py │ ├── wsgi.py │ └── urls.py ├── requirements.txt ├── Dockerfile └── manage.py ├── .dockerignore ├── frontend ├── public │ ├── favicon.ico │ ├── manifest.json │ └── index.html ├── src │ ├── App.test.js │ ├── index.css │ ├── index.js │ ├── App.css │ ├── App.js │ ├── logo.svg │ └── serviceWorker.js ├── Dockerfile ├── .gitignore ├── package.json └── README.md ├── heroku.yml ├── docker-compose.yml ├── README.md ├── LICENSE ├── Dockerfile └── .gitignore /backend/char_count/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /backend/hello_world/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /backend/char_count/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /backend/hello_world/settings/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /backend/requirements.txt: -------------------------------------------------------------------------------- 1 | django 2 | whitenoise 3 | -------------------------------------------------------------------------------- /backend/char_count/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /backend/char_count/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /backend/char_count/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | # JS stuff 2 | node_modules 3 | 4 | # Dev stuff 5 | .git 6 | .gitignore 7 | LICENSE 8 | README.md 9 | .vscode -------------------------------------------------------------------------------- /frontend/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cfranklin11/docker-django-react/HEAD/frontend/public/favicon.ico -------------------------------------------------------------------------------- /heroku.yml: -------------------------------------------------------------------------------- 1 | build: 2 | docker: 3 | web: Dockerfile 4 | run: 5 | web: python3 backend/manage.py runserver 0.0.0.0:$PORT 6 | -------------------------------------------------------------------------------- /backend/char_count/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class CharCountConfig(AppConfig): 5 | name = 'char_count' 6 | -------------------------------------------------------------------------------- /backend/char_count/views.py: -------------------------------------------------------------------------------- 1 | from django.http import JsonResponse 2 | 3 | 4 | def char_count(request): 5 | text = request.GET.get("text", "") 6 | 7 | return JsonResponse({"count": len(text)}) 8 | -------------------------------------------------------------------------------- /frontend/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /frontend/Dockerfile: -------------------------------------------------------------------------------- 1 | # Use an official node runtime as a parent image 2 | FROM node:14 3 | 4 | WORKDIR /app/ 5 | 6 | # Install dependencies 7 | COPY package.json yarn.lock /app/ 8 | 9 | RUN yarn install 10 | 11 | # Add rest of the client code 12 | COPY . /app/ 13 | 14 | EXPOSE 3000 15 | 16 | CMD yarn start 17 | -------------------------------------------------------------------------------- /frontend/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": ".", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /frontend/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /frontend/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 5 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 6 | sans-serif; 7 | -webkit-font-smoothing: antialiased; 8 | -moz-osx-font-smoothing: grayscale; 9 | } 10 | 11 | code { 12 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 13 | monospace; 14 | } 15 | -------------------------------------------------------------------------------- /backend/hello_world/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for hello_world project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_world.settings.base") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /backend/hello_world/settings/development.py: -------------------------------------------------------------------------------- 1 | from hello_world.settings.base import * 2 | 3 | # Quick-start development settings - unsuitable for production 4 | # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ 5 | 6 | # SECURITY WARNING: keep the secret key used in production secret! 7 | SECRET_KEY = "8lj(bgx5_*o%d1$u-#gco+ioorntjjlybb#i1+rl-il0jb=)d7" 8 | 9 | # SECURITY WARNING: don't run with debug turned on in production! 10 | DEBUG = True 11 | 12 | ALLOWED_HOSTS = ["backend"] 13 | -------------------------------------------------------------------------------- /frontend/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: https://bit.ly/CRA-PWA 12 | serviceWorker.unregister(); 13 | -------------------------------------------------------------------------------- /backend/Dockerfile: -------------------------------------------------------------------------------- 1 | # Use an official Python runtime as a parent image 2 | FROM python:3.8 3 | 4 | # Adding backend directory to make absolute filepaths consistent across services 5 | WORKDIR /app/backend 6 | 7 | # Install Python dependencies 8 | COPY requirements.txt /app/backend 9 | RUN pip3 install --upgrade pip -r requirements.txt 10 | 11 | # Add the rest of the code 12 | COPY . /app/backend 13 | 14 | # Make port 8000 available for the app 15 | EXPOSE 8000 16 | 17 | # Be sure to use 0.0.0.0 for the host within the Docker container, 18 | # otherwise the browser won't be able to find it 19 | CMD python3 manage.py runserver 0.0.0.0:8000 20 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.2" 2 | services: 3 | backend: 4 | build: ./backend 5 | volumes: 6 | - ./backend:/app/backend 7 | ports: 8 | - "8000:8000" 9 | stdin_open: true 10 | tty: true 11 | environment: 12 | - DJANGO_SETTINGS_MODULE=hello_world.settings.development 13 | command: python3 manage.py runserver 0.0.0.0:8000 14 | frontend: 15 | build: ./frontend 16 | volumes: 17 | - ./frontend:/app 18 | # One-way volume to use node_modules from inside image 19 | - /app/node_modules 20 | ports: 21 | - "3000:3000" 22 | environment: 23 | - NODE_ENV=development 24 | depends_on: 25 | - backend 26 | command: npm start 27 | -------------------------------------------------------------------------------- /backend/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hello_world.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /frontend/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 40vmin; 8 | pointer-events: none; 9 | } 10 | 11 | .App-header { 12 | background-color: #282c34; 13 | min-height: 100vh; 14 | display: flex; 15 | flex-direction: column; 16 | align-items: center; 17 | justify-content: center; 18 | font-size: calc(10px + 2vmin); 19 | color: white; 20 | } 21 | 22 | .App-link { 23 | color: #61dafb; 24 | } 25 | 26 | @keyframes App-logo-spin { 27 | from { 28 | transform: rotate(0deg); 29 | } 30 | to { 31 | transform: rotate(360deg); 32 | } 33 | } 34 | 35 | input { 36 | margin-left: 10px; 37 | margin-right: 10px; 38 | } 39 | -------------------------------------------------------------------------------- /frontend/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-world", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "axios": "^0.20.0", 7 | "react": "^16.13.1", 8 | "react-dom": "^16.13.1", 9 | "react-scripts": "3.4.3" 10 | }, 11 | "scripts": { 12 | "start": "react-scripts start", 13 | "build": "react-scripts build", 14 | "test": "react-scripts test", 15 | "eject": "react-scripts eject" 16 | }, 17 | "eslintConfig": { 18 | "extends": "react-app" 19 | }, 20 | "browserslist": { 21 | "production": [ 22 | ">0.2%", 23 | "not dead", 24 | "not op_mini all" 25 | ], 26 | "development": [ 27 | "last 1 chrome version", 28 | "last 1 firefox version", 29 | "last 1 safari version" 30 | ] 31 | }, 32 | "proxy": "http://backend:8000" 33 | } -------------------------------------------------------------------------------- /frontend/src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import axios from 'axios'; 3 | import './App.css'; 4 | 5 | function handleSubmit(event) { 6 | const text = document.querySelector('#char-input').value 7 | 8 | axios 9 | .get(`/char_count?text=${text}`).then(({data}) => { 10 | document.querySelector('#char-count').textContent = `${data.count} characters!` 11 | }) 12 | .catch(err => console.log(err)) 13 | } 14 | 15 | function App() { 16 | return ( 17 |
18 |
19 | 20 | 21 | 22 |
23 | 24 |
25 |

26 |
27 |
28 | ); 29 | } 30 | 31 | export default App; 32 | -------------------------------------------------------------------------------- /backend/hello_world/settings/production.py: -------------------------------------------------------------------------------- 1 | import os 2 | from hello_world.settings.base import * 3 | 4 | SECRET_KEY = os.environ.get("SECRET_KEY") 5 | DEBUG = False 6 | ALLOWED_HOSTS = [os.environ.get("PRODUCTION_HOST")] 7 | 8 | INSTALLED_APPS.extend(["whitenoise.runserver_nostatic"]) 9 | 10 | # Must insert after SecurityMiddleware, which is first in settings/common.py 11 | MIDDLEWARE.insert(1, "whitenoise.middleware.WhiteNoiseMiddleware") 12 | 13 | TEMPLATES[0]["DIRS"] = [os.path.join(BASE_DIR, "../", "frontend", "build")] 14 | 15 | # Static files (CSS, JavaScript, Images) 16 | # https://docs.djangoproject.com/en/2.1/howto/static-files/ 17 | 18 | STATICFILES_DIRS = [os.path.join(BASE_DIR, "../", "frontend", "build", "static")] 19 | STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" 20 | STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") 21 | 22 | STATIC_URL = "/static/" 23 | WHITENOISE_ROOT = os.path.join(BASE_DIR, "../", "frontend", "build", "root") 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker + Django + React App Tutorial 2 | 3 | Code repo for a series of tutorials on setting up an app with Docker Compose, Django, and Create React App. `master` has the final version of the code. 4 | 5 | - Part One: [Creating an app with Docker Compose, Django, and Create React App](https://dev.to/englishcraig/creating-an-app-with-docker-compose-django-and-create-react-app-31lf) 6 | - Part Two: [Docker, Django, and React: Building Assets and Deploying to Heroku](https://dev.to/englishcraig/docker-django-react-building-assets-and-deploying-to-heroku-24jh) 7 | 8 | ## Dependencies 9 | 10 | - All tutorials require [Docker](https://docs.docker.com/docker-for-mac/install/) 11 | - Deploying to Heroku requires the [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli) 12 | 13 | ## Setting up the app 14 | 15 | Checkout the branch for a given tutorial, and run `docker-compose build` 16 | 17 | ## Running the app on local 18 | 19 | Run `docker-compose up` to see messages in the terminal. Run `docker-compose start` to run the app in the background. 20 | -------------------------------------------------------------------------------- /backend/hello_world/urls.py: -------------------------------------------------------------------------------- 1 | """hello_world URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/2.2/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path, re_path 18 | from django.views.generic import TemplateView 19 | from char_count.views import char_count 20 | 21 | urlpatterns = [ 22 | path("admin/", admin.site.urls), 23 | path("char_count", char_count, name="char_count"), 24 | re_path(".*", TemplateView.as_view(template_name="index.html")), 25 | ] 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Craig 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.8 2 | 3 | # Install curl, node, & yarn 4 | RUN apt-get -y install curl \ 5 | && curl -sL https://deb.nodesource.com/setup_14.x | bash \ 6 | && apt-get install nodejs \ 7 | && curl -o- -L https://yarnpkg.com/install.sh | bash 8 | 9 | WORKDIR /app/backend 10 | 11 | # Install Python dependencies 12 | COPY ./backend/requirements.txt /app/backend/ 13 | RUN pip3 install --upgrade pip -r requirements.txt 14 | 15 | # Install JS dependencies 16 | WORKDIR /app/frontend 17 | 18 | COPY ./frontend/package.json ./frontend/yarn.lock /app/frontend/ 19 | RUN $HOME/.yarn/bin/yarn install 20 | 21 | # Add the rest of the code 22 | COPY . /app/ 23 | 24 | # Build static files 25 | RUN $HOME/.yarn/bin/yarn build 26 | 27 | # Have to move all static files other than index.html to root/ 28 | # for whitenoise middleware 29 | WORKDIR /app/frontend/build 30 | 31 | RUN mkdir root && mv *.ico *.js *.json root 32 | 33 | # Collect static files 34 | RUN mkdir /app/backend/staticfiles 35 | 36 | WORKDIR /app 37 | 38 | # SECRET_KEY is only included here to avoid raising an error when generating static files. 39 | # Be sure to add a real SECRET_KEY config variable in Heroku. 40 | RUN DJANGO_SETTINGS_MODULE=hello_world.settings.production \ 41 | SECRET_KEY=somethingsupersecret \ 42 | python3 backend/manage.py collectstatic --noinput 43 | 44 | EXPOSE $PORT 45 | 46 | CMD python3 backend/manage.py runserver 0.0.0.0:$PORT 47 | -------------------------------------------------------------------------------- /frontend/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 22 | React App 23 | 24 | 25 | 26 |
27 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /frontend/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /frontend/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `npm run eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `npm run build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /backend/hello_world/settings/base.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for hello_world project. 3 | 4 | Generated by 'django-admin startproject' using Django 2.2. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/2.2/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")) 17 | 18 | # Application definition 19 | 20 | INSTALLED_APPS = [ 21 | "char_count.apps.CharCountConfig", 22 | "django.contrib.admin", 23 | "django.contrib.auth", 24 | "django.contrib.contenttypes", 25 | "django.contrib.sessions", 26 | "django.contrib.messages", 27 | "django.contrib.staticfiles", 28 | ] 29 | 30 | MIDDLEWARE = [ 31 | "django.middleware.security.SecurityMiddleware", 32 | "django.contrib.sessions.middleware.SessionMiddleware", 33 | "django.middleware.common.CommonMiddleware", 34 | "django.middleware.csrf.CsrfViewMiddleware", 35 | "django.contrib.auth.middleware.AuthenticationMiddleware", 36 | "django.contrib.messages.middleware.MessageMiddleware", 37 | "django.middleware.clickjacking.XFrameOptionsMiddleware", 38 | ] 39 | 40 | ROOT_URLCONF = "hello_world.urls" 41 | 42 | TEMPLATES = [ 43 | { 44 | "BACKEND": "django.template.backends.django.DjangoTemplates", 45 | "DIRS": [], 46 | "APP_DIRS": True, 47 | "OPTIONS": { 48 | "context_processors": [ 49 | "django.template.context_processors.debug", 50 | "django.template.context_processors.request", 51 | "django.contrib.auth.context_processors.auth", 52 | "django.contrib.messages.context_processors.messages", 53 | ] 54 | }, 55 | } 56 | ] 57 | 58 | WSGI_APPLICATION = "hello_world.wsgi.application" 59 | 60 | 61 | # Database 62 | # https://docs.djangoproject.com/en/2.2/ref/settings/#databases 63 | 64 | DATABASES = { 65 | "default": { 66 | "ENGINE": "django.db.backends.sqlite3", 67 | "NAME": os.path.join(BASE_DIR, "db.sqlite3"), 68 | } 69 | } 70 | 71 | 72 | # Password validation 73 | # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators 74 | 75 | AUTH_PASSWORD_VALIDATORS = [ 76 | { 77 | "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator" 78 | }, 79 | {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"}, 80 | {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"}, 81 | {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"}, 82 | ] 83 | 84 | 85 | # Internationalization 86 | # https://docs.djangoproject.com/en/2.2/topics/i18n/ 87 | 88 | LANGUAGE_CODE = "en-us" 89 | 90 | TIME_ZONE = "UTC" 91 | 92 | USE_I18N = True 93 | 94 | USE_L10N = True 95 | 96 | USE_TZ = True 97 | 98 | 99 | # Static files (CSS, JavaScript, Images) 100 | # https://docs.djangoproject.com/en/2.2/howto/static-files/ 101 | 102 | STATIC_URL = "/static/" 103 | -------------------------------------------------------------------------------- /frontend/src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.1/8 is considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl) 104 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | --------------------------------------------------------------------------------