Skip to content

Commit 2f6df52

Browse files
committed
OpenConceptLab/ocl_issues#965 Using Gunicorn for Swagger and disabling DEBUG mode
1 parent 11b7a30 commit 2f6df52

10 files changed

Lines changed: 40 additions & 28 deletions

File tree

Dockerfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ RUN addgroup -S ocl && adduser -S ocl -G ocl
2323

2424
ENV APP_HOME=/code
2525

26-
RUN mkdir -p $APP_HOME
27-
RUN mkdir -p /temp
26+
RUN mkdir -p $APP_HOME /temp /staticfiles
2827

29-
RUN chown -R ocl:ocl /code /temp
28+
RUN chown -R ocl:ocl $APP_HOME /temp /staticfiles
3029

3130
WORKDIR $APP_HOME
3231

core/orgs/models.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from django.contrib import admin
21
from django.contrib.contenttypes.fields import GenericRelation
32
from django.core.validators import RegexValidator
43
from django.db import models, transaction
@@ -93,6 +92,3 @@ def delete_pins(self):
9392
Pin.objects.filter(resource_type__model='organization', resource_id=self.id).delete()
9493
# deletes pins for this org
9594
self.pins.all().delete()
96-
97-
98-
admin.site.register(Organization)

core/settings.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
SECRET_KEY = '=q1%fd62$x!35xzzlc3lix3g!s&!2%-1d@5a=rm!n4lu74&6)p'
3232

3333
# SECURITY WARNING: don't run with debug turned on in production!
34-
DEBUG = os.environ.get('DEBUG', True)
34+
DEBUG = os.environ.get('DEBUG') == 'TRUE'
3535

3636
ALLOWED_HOSTS = ['*']
3737

@@ -125,8 +125,8 @@
125125
}
126126

127127
MIDDLEWARE = [
128-
'django.middleware.gzip.GZipMiddleware',
129128
'django.middleware.security.SecurityMiddleware',
129+
'django.middleware.gzip.GZipMiddleware',
130130
'django.contrib.sessions.middleware.SessionMiddleware',
131131
'corsheaders.middleware.CorsMiddleware',
132132
'django.middleware.common.CommonMiddleware',
@@ -263,9 +263,10 @@
263263
USE_TZ = True
264264

265265
# Static files (CSS, JavaScript, Images)
266-
# https://docs.djangoproject.com/en/3.0/howto/static-files/
267-
268266
STATIC_URL = '/static/'
267+
STATIC_ROOT = '/staticfiles'
268+
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
269+
269270
AUTH_USER_MODEL = 'users.UserProfile'
270271
TEST_RUNNER = 'core.common.tests.CustomTestRunner'
271272
DEFAULT_LOCALE = os.environ.get('DEFAULT_LOCALE', 'en')
@@ -353,6 +354,10 @@
353354
('Jonathan Payne', 'paynejd@gmail.com'),
354355
)
355356

357+
if ENV and ENV != 'development':
358+
# Serving swagger static files (inserted after SecurityMiddleware)
359+
MIDDLEWARE.insert(1, 'whitenoise.middleware.WhiteNoiseMiddleware')
360+
356361
if not ENV or ENV in ['production']:
357362
EMAIL_SUBJECT_PREFIX = '[Openconceptlab.org] '
358363
else:

core/urls.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1515
"""
1616
from django.conf.urls import url
17-
from django.contrib import admin
1817
from django.urls import path, include, re_path
1918
from drf_yasg import openapi
2019
from drf_yasg.views import get_schema_view
@@ -51,7 +50,6 @@
5150
url(r'^swagger/$', SchemaView.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
5251
url(r'^redoc/$', SchemaView.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
5352
path('healthcheck/', include('core.common.healthcheck.urls')),
54-
path('admin/', admin.site.urls, name='admin_urls'),
5553
path('admin/reports/authored/', report_views.AuthoredView.as_view(), name='authored-report'),
5654
path('admin/reports/monthly-usage/', report_views.MonthlyUsageView.as_view(), name='monthly-usage-report'),
5755
path('admin/concepts/locales/duplicate/', ConceptDuplicateLocalesView.as_view(), name='concept-duplicate-locales'),

core/users/models.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from django.contrib import admin
21
from django.contrib.auth.models import AbstractUser
32
from django.contrib.auth.password_validation import validate_password
43
from django.core.exceptions import ValidationError
@@ -149,6 +148,3 @@ def is_valid_auth_group(*names):
149148
@property
150149
def auth_groups(self):
151150
return self.groups.values_list('name', flat=True)
152-
153-
154-
admin.site.register(UserProfile)

docker-compose.ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,53 @@
11
version: '3'
22
services:
3+
db:
4+
restart: "no"
5+
redis:
6+
restart: "no"
37
api:
48
build: .
59
environment:
610
- ENVIRONMENT=ci
711
volumes:
812
- .:/code
13+
restart: "no"
914
celery:
1015
build: .
1116
volumes:
1217
- .:/code
1318
command: ["bash", "-c", "CELERY_WORKER_NAME=default ./start_celery_worker.sh -P prefork -Q default -c 1"]
19+
restart: "no"
1420
celery_indexing:
1521
build: .
1622
volumes:
1723
- .:/code
1824
command: ["bash", "-c", "CELERY_WORKER_NAME=indexing ./start_celery_worker.sh -P prefork -Q indexing -c 1"]
25+
restart: "no"
1926
celery_concurrent:
2027
build: .
2128
volumes:
2229
- .:/code
2330
command: ["bash", "-c", "CELERY_WORKER_NAME=concurrent ./start_celery_worker.sh -P prefork -Q concurrent -c 1"]
31+
restart: "no"
2432
celery_bulk_import_0_1:
2533
build: .
2634
volumes:
2735
- .:/code
36+
restart: "no"
2837
celery_bulk_import_0_2:
2938
build: .
3039
volumes:
3140
- .:/code
41+
restart: "no"
3242
celery_bulk_import_root:
3343
build: .
3444
volumes:
3545
- .:/code
46+
restart: "no"
3647
flower:
3748
build: .
3849
volumes:
3950
- .:/code
51+
restart: "no"
52+
es:
53+
restart: "no"

docker-compose.override.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ services:
1616
restart: "no"
1717
environment:
1818
- ENVIRONMENT=development
19+
- DEBUG=${DEBUG-TRUE}
1920
celery:
2021
build: .
2122
volumes:

docker-compose.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ services:
3131
- ES_HOST=${ES_HOST-es}
3232
- ES_PORT=${ES_PORT-9200}
3333
- ENVIRONMENT=${ENVIRONMENT-production}
34-
- DEBUG=True #needed to be able to serve staticfiles with Django server
34+
- DEBUG=${DEBUG-FALSE}
3535
- SECRET_KEY
3636
- SENTRY_DSN_KEY
3737
- API_SUPERUSER_PASSWORD=${API_SUPERUSER_PASSWORD-Root123}
@@ -47,34 +47,34 @@ services:
4747
test: "curl --silent --fail http://localhost:8000/version/ || exit 1"
4848
celery:
4949
image: openconceptlab/oclapi2:${ENVIRONMENT-production}
50-
command: ["bash", "-c", "CELERY_WORKER_NAME=default ./start_celery_worker.sh -P prefork -Q default --autoscale=10,2"]
50+
command: ["bash", "-c", "CELERY_WORKER_NAME=default ./start_celery_worker.sh -P prefork -Q default --autoscale=5,1"]
5151
restart: "always"
5252
healthcheck:
5353
test: ["CMD-SHELL", "-c", "CELERY_WORKER_NAME=default ./ping_celery_worker.sh"]
5454
environment:
5555
- REDIS_HOST=${REDIS_HOST-redis}
5656
- REDIS_PORT=${REDIS_PORT-6379}
57-
- DEBUG=${DEBUG-False}
57+
- DEBUG=${DEBUG-FALSE}
5858
celery_indexing:
5959
image: openconceptlab/oclapi2:${ENVIRONMENT-production}
60-
command: ["bash", "-c", "CELERY_WORKER_NAME=indexing ./start_celery_worker.sh -P prefork -Q indexing --autoscale=10,2"]
60+
command: ["bash", "-c", "CELERY_WORKER_NAME=indexing ./start_celery_worker.sh -P prefork -Q indexing --autoscale=2,1"]
6161
restart: "always"
6262
healthcheck:
6363
test: ["CMD-SHELL", "-c", "CELERY_WORKER_NAME=indexing ./ping_celery_worker.sh"]
6464
environment:
6565
- REDIS_HOST=${REDIS_HOST-redis}
6666
- REDIS_PORT=${REDIS_PORT-6379}
67-
- DEBUG=${DEBUG-False}
67+
- DEBUG=${DEBUG-FALSE}
6868
celery_concurrent:
6969
image: openconceptlab/oclapi2:${ENVIRONMENT-production}
70-
command: ["bash", "-c", "CELERY_WORKER_NAME=concurrent ./start_celery_worker.sh -P prefork -Q concurrent -c 10"]
70+
command: ["bash", "-c", "CELERY_WORKER_NAME=concurrent ./start_celery_worker.sh -P prefork -Q concurrent -c 5"]
7171
restart: "always"
7272
healthcheck:
7373
test: ["CMD-SHELL", "-c", "CELERY_WORKER_NAME=concurrent ./ping_celery_worker.sh"]
7474
environment:
7575
- REDIS_HOST=${REDIS_HOST-redis}
7676
- REDIS_PORT=${REDIS_PORT-6379}
77-
- DEBUG=${DEBUG-False}
77+
- DEBUG=${DEBUG-FALSE}
7878
celery_bulk_import_0_1:
7979
image: openconceptlab/oclapi2:${ENVIRONMENT-production}
8080
command: ["bash", "-c", "CELERY_WORKER_NAME=bulk_import_0_1 ./start_celery_worker.sh -Q bulk_import_0,bulk_import_1 -c 1"]
@@ -84,7 +84,7 @@ services:
8484
environment:
8585
- REDIS_HOST=${REDIS_HOST-redis}
8686
- REDIS_PORT=${REDIS_PORT-6379}
87-
- DEBUG=${DEBUG-False}
87+
- DEBUG=${DEBUG-FALSE}
8888
celery_bulk_import_2_3:
8989
image: openconceptlab/oclapi2:${ENVIRONMENT-production}
9090
command: ["bash", "-c", "CELERY_WORKER_NAME=bulk_import_2_3 ./start_celery_worker.sh -Q bulk_import_2,bulk_import_3 -c 1"]
@@ -94,7 +94,7 @@ services:
9494
environment:
9595
- REDIS_HOST=${REDIS_HOST-redis}
9696
- REDIS_PORT=${REDIS_PORT-6379}
97-
- DEBUG=${DEBUG-False}
97+
- DEBUG=${DEBUG-FALSE}
9898
celery_bulk_import_root:
9999
image: openconceptlab/oclapi2:${ENVIRONMENT-production}
100100
command: ["bash", "-c", "CELERY_WORKER_NAME=bulk_import_root ./start_celery_worker.sh -Q bulk_import_root -c 1"]
@@ -104,7 +104,7 @@ services:
104104
environment:
105105
- REDIS_HOST=${REDIS_HOST-redis}
106106
- REDIS_PORT=${REDIS_PORT-6379}
107-
- DEBUG=${DEBUG-False}
107+
- DEBUG=${DEBUG-FALSE}
108108
flower:
109109
image: openconceptlab/oclapi2:${ENVIRONMENT-production}
110110
command: ["bash", "-c", "./start_flower.sh"]

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Django==3.2.7
22
psycopg2==2.9.1
3-
gunicorn==20.1.0
3+
gunicorn==20.1.0 #production server
4+
whitenoise==5.3.0 #serving swagger static files
45
djangorestframework==3.12.4
56
pylint==2.10.2
67
pydash==5.0.2

startup.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ if [[ "$ENVIRONMENT" = "development" ]]; then
2525
echo "Starting up the development server"
2626
python manage.py runserver 0.0.0.0:${API_PORT:-8000}
2727
else
28+
echo "Collect static files"
29+
python manage.py collectstatic
2830
echo "Starting up the production server"
2931
gunicorn core.wsgi:application --bind 0.0.0.0:${API_PORT:-8000}
3032
fi

0 commit comments

Comments
 (0)