From 025912f1db08ed8ca43cf8870d78c44bd316ac55 Mon Sep 17 00:00:00 2001 From: kyle-verhoog Date: Mon, 16 Nov 2020 17:03:06 -0500 Subject: [PATCH] python: add django 3 app --- python/django3/.gitignore | 1 + python/django3/Dockerfile | 7 ++ python/django3/app/__init__.py | 0 python/django3/app/asgi.py | 16 ++++ python/django3/app/settings.py | 120 ++++++++++++++++++++++++++++++ python/django3/app/urls.py | 26 +++++++ python/django3/app/views.py | 18 +++++ python/django3/app/wsgi.py | 17 +++++ python/django3/docker-compose.yml | 39 ++++++++++ python/django3/manage.py | 22 ++++++ python/django3/requirements.txt | 71 ++++++++++++++++++ 11 files changed, 337 insertions(+) create mode 100644 python/django3/.gitignore create mode 100644 python/django3/Dockerfile create mode 100644 python/django3/app/__init__.py create mode 100644 python/django3/app/asgi.py create mode 100644 python/django3/app/settings.py create mode 100644 python/django3/app/urls.py create mode 100644 python/django3/app/views.py create mode 100644 python/django3/app/wsgi.py create mode 100644 python/django3/docker-compose.yml create mode 100755 python/django3/manage.py create mode 100644 python/django3/requirements.txt diff --git a/python/django3/.gitignore b/python/django3/.gitignore new file mode 100644 index 00000000..1d17dae1 --- /dev/null +++ b/python/django3/.gitignore @@ -0,0 +1 @@ +.venv diff --git a/python/django3/Dockerfile b/python/django3/Dockerfile new file mode 100644 index 00000000..d968aa95 --- /dev/null +++ b/python/django3/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.8 + +WORKDIR /project + +COPY ./requirements.txt /project + +RUN pip install -r requirements.txt diff --git a/python/django3/app/__init__.py b/python/django3/app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/django3/app/asgi.py b/python/django3/app/asgi.py new file mode 100644 index 00000000..1e15a3bd --- /dev/null +++ b/python/django3/app/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for app project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") + +application = get_asgi_application() diff --git a/python/django3/app/settings.py b/python/django3/app/settings.py new file mode 100644 index 00000000..ec35bf72 --- /dev/null +++ b/python/django3/app/settings.py @@ -0,0 +1,120 @@ +""" +Django settings for app project. + +Generated by 'django-admin startproject' using Django 3.1. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.1/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve(strict=True).parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "le@f+r+o4qqd_s=i6$1d313h2)xj#j1*k5immu*7qz)!g-vw7o" + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ["*"] + + +# Application definition + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "app.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "app.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/3.1/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.1/topics/i18n/ + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.1/howto/static-files/ + +STATIC_URL = "/static/" diff --git a/python/django3/app/urls.py b/python/django3/app/urls.py new file mode 100644 index 00000000..559c7b5d --- /dev/null +++ b/python/django3/app/urls.py @@ -0,0 +1,26 @@ +"""app URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path + +from . import views + + +urlpatterns = [ + path("admin/", admin.site.urls), + path("download/", views.file_response), + path("streaming/", views.streaming_response), +] diff --git a/python/django3/app/views.py b/python/django3/app/views.py new file mode 100644 index 00000000..58c88b92 --- /dev/null +++ b/python/django3/app/views.py @@ -0,0 +1,18 @@ +import time +import random + +from django.http import HttpResponse, StreamingHttpResponse, FileResponse + + +def file_response(request): + return FileResponse(open("manage.py", "rb")) + + +def get_resp(): + for i in range(0, 10): + time.sleep(random.random() / 10) + yield "%d\n" % i + + +def streaming_response(request): + return StreamingHttpResponse(get_resp()) diff --git a/python/django3/app/wsgi.py b/python/django3/app/wsgi.py new file mode 100644 index 00000000..10a94440 --- /dev/null +++ b/python/django3/app/wsgi.py @@ -0,0 +1,17 @@ +""" +WSGI config for app project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/ +""" + +import os + +from ddtrace.contrib.wsgi import DDWSGIMiddleware +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") + +application = DDWSGIMiddleware(get_wsgi_application()) diff --git a/python/django3/docker-compose.yml b/python/django3/docker-compose.yml new file mode 100644 index 00000000..aeed78a6 --- /dev/null +++ b/python/django3/docker-compose.yml @@ -0,0 +1,39 @@ +version: "3" + +services: + agent: + image: datadog/agent:latest + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + - /proc/:/host/proc/:ro + - /sys/fs/cgroup/:/host/sys/fs/cgroup:ro + ports: + - 127.0.0.1:8126:8126/tcp + - 127.0.0.1:8125:8125/udp + environment: + - DD_API_KEY=${DD_API_KEY} + - DD_APM_ENABLED=true + - DD_APM_NON_LOCAL_TRAFFIC=true + - DD_LOG_LEVEL=INFO + - DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true + - DD_AC_EXCLUDE=name:datadog-agent + app_dev: + build: + context: . + dockerfile: ./Dockerfile + command: ddtrace-run python manage.py runserver 0.0.0.0:8000 + environment: + - DD_ENV=test + - "DD_TRACE_AGENT_URL=http://agent:8126" + - DD_SERVICE=django-test + - DD_DJANGO_CACHE_SERVICE_NAME=cache + - DD_DJANGO_DATABASE_SERVICE_NAME=django-test + - DD_DJANGO_USE_HANDLER_RESOURCE_FORMAT=true + - DD_SQLITE_SERVICE=test-db + - DD_PROFILING_ENABLED=true + # - DD_TRACE_DEBUG=true + ports: + - 8000:8000 + volumes: + - .:/project + diff --git a/python/django3/manage.py b/python/django3/manage.py new file mode 100755 index 00000000..1a64b14a --- /dev/null +++ b/python/django3/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == "__main__": + main() diff --git a/python/django3/requirements.txt b/python/django3/requirements.txt new file mode 100644 index 00000000..9ade4122 --- /dev/null +++ b/python/django3/requirements.txt @@ -0,0 +1,71 @@ +alabaster==0.7.12 +appdirs==1.4.4 +attrs==20.3.0 +Babel==2.8.1 +black==20.8b1 +certifi==2020.11.8 +chardet==3.0.4 +click==7.1.2 +Cython==0.29.21 +ddtrace +distlib==0.3.1 +docutils==0.16 +dulwich==0.20.11 +filelock==3.0.12 +flake8==3.8.4 +gevent==20.9.0 +greenlet==0.4.17 +idna==2.10 +imagesize==1.2.0 +iniconfig==1.1.1 +intervaltree==3.1.0 +Jinja2==2.11.2 +MarkupSafe==1.1.1 +mccabe==0.6.1 +mock==4.0.2 +multidict==5.0.0 +mypy-extensions==0.4.3 +opentracing==2.3.0 +packaging==20.4 +pathspec==0.8.1 +pbr==5.5.1 +pluggy==0.13.1 +protobuf==3.13.0 +py==1.9.0 +py-cpuinfo==7.0.0 +pycodestyle==2.6.0 +pyenchant==3.1.1 +pyflakes==2.2.0 +Pygments==2.7.2 +pyparsing==2.4.7 +pytest==6.1.2 +pytest-benchmark==3.2.3 +pytz==2020.4 +PyYAML==5.3.1 +regex==2020.11.11 +reno==3.2.0 +requests==2.25.0 +riot==0.2.0 +six==1.15.0 +snowballstemmer==2.0.0 +sortedcontainers==2.3.0 +Sphinx==3.3.0 +sphinx-rtd-theme==0.5.0 +sphinxcontrib-applehelp==1.0.2 +sphinxcontrib-devhelp==1.0.2 +sphinxcontrib-htmlhelp==1.0.3 +sphinxcontrib-jsmath==1.0.1 +sphinxcontrib-qthelp==1.0.3 +sphinxcontrib-serializinghtml==1.1.4 +sphinxcontrib-spelling==7.1.0 +tenacity==6.2.0 +toml==0.10.2 +typed-ast==1.4.1 +typing-extensions==3.7.4.3 +urllib3==1.26.1 +vcrpy==4.1.1 +virtualenv==20.1.0 +wrapt==1.12.1 +yarl==1.6.2 +zope.event==4.5.0 +zope.interface==5.2.0