Skip to content

Instantly share code, notes, and snippets.

@BroHui
Last active September 28, 2023 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BroHui/7bddeb48f544d5c53ef639cf0d9fedfe to your computer and use it in GitHub Desktop.
Save BroHui/7bddeb48f544d5c53ef639cf0d9fedfe to your computer and use it in GitHub Desktop.
Lightweight Django with 3.2
#!/bin/bash
APP_ROOT='/usr/src/app'
STATIC_ROOT='/usr/src/static'
NODE_MODULES='/usr/src/node_modules'
cd $APP_ROOT
# collect statics
sed -i '/node_modules/c\"'$NODE_MODULES'",' `find . | grep settings.py`
sed -i '/^STATIC_ROOT/c\STATIC_ROOT="'$STATIC_ROOT'"' `find . | grep settings.py`
python manage.py collectstatic --noinput
# remove debug symbol
sed -i '/^DEBUG/c\DEBUG=False' `find . | grep settings.py`
exec $@
FROM python:3.7.9-slim-buster as base
WORKDIR /wheels
COPY ./requirements.txt .
RUN apt-get update && \
apt-get install -y \
default-libmysqlclient-dev \
libmariadb3 \
gcc
RUN pip install -U pip \
&& pip install --no-cache-dir wheel \
&& pip wheel --no-cache-dir --wheel-dir=/wheels -r requirements.txt
FROM node:12 as modules
WORKDIR /temp_node_modules
COPY ./package.json .
RUN npm install
FROM python:3.7.9-slim-buster
ENV PYTHONUNBUFFERED 1
WORKDIR /usr/src/app
COPY --from=base /wheels /wheels
COPY --from=base /usr/lib/x86_64-linux-gnu/libmariadb.so.3 /usr/lib/x86_64-linux-gnu/
COPY --from=modules /temp_node_modules/node_modules /usr/src/node_modules
RUN pip install -U pip \
&& pip install -f /wheels -r /wheels/requirements.txt \
&& rm -rf /wheels \
&& rm -rf /root/.cache/pip/*
EXPOSE 8080 8081
COPY ./docker-entrypoint.sh /
ENTRYPOINT ["sh", "/docker-entrypoint.sh"]
CMD ["uwsgi", "--ini", "uwsgi.ini"]
# coding: utf-8
import sys
import os
from functools import wraps
from django.conf import settings
from django.urls import path
from django.http import HttpResponse, HttpResponseForbidden
from django.core.wsgi import get_wsgi_application
DEBUG = os.getenv('DEBUG', '0') == '1'
SECRET_KEY = os.getenv('SECRET_KEY', '{{ secret_key }}')
TOKEN_LIST = os.getenv('TOKEN_LIST', '')
TOKEN_LIST = TOKEN_LIST.split(',') if TOKEN_LIST else []
if not TOKEN_LIST: print("Run server as NO AUTH mode!")
settings.configure(
DEBUG=DEBUG,
ROOT_URLCONF=sys.modules[__name__],
SECRET_KEY=SECRET_KEY,
ALLOW_HOST=['*'],
MIDDLEWARE_CLASSES=(
'django.middleware.common.CommonMiddleware',
),
TOKEN_LIST=TOKEN_LIST
)
""" Simple auth """
def verify_request(func):
@wraps(func)
def returned_wrapper(request, *args, **kwargs):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
request_ip = x_forwarded_for.split(',')[0] if x_forwarded_for else request.META.get('REMOTE_ADDR')
# Auth
token = request.GET.get('token', '')
if settings.TOKEN_LIST and (not token or token not in settings.TOKEN_LIST):
print(f'Unauthorized Request from {request_ip}')
return HttpResponseForbidden()
return func(request, *args, **kwargs)
return returned_wrapper
""" Your Views here """
@verify_request
def index(request):
return HttpResponse('Django works!')
urlpatterns = [
path('', index),
]
# uWSGI Supports
application = get_wsgi_application()
if __name__ == '__main__':
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Django >=3.2.0, <3.3
mysqlclient >= 2.0
requests >= 2.11.1
django-cors-headers >= 1.1.0
redis >= 3.5.3
uwsgi >= 2.0
django-bootstrap4 ==3.0.0
PyYAML == 5.4.1
[uwsgi]
chdir = /usr/src/app
module = demo.wsgi
master = true
processes = 5
socket = 0.0.0.0:8080
http = 0.0.0.0:8081
vacuum = true
static-map = /static=/usr/src/static
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment