Skip to main content

Turn a Django app into a Django project so it can run as a microservice or whatever

Project description

Author:

Keryn Knight

Version:
0.1.1

Convert a Django app into a standalone Django project so you can run it independently as a microservice or what-have-you.

Configuration options given to run() are defaults, and may be replaced by environment variables thanks to django-environ

If your app crosses boundaries into another application via imports, you should hopefully get warnings. Figuring out how to do that was mostly why I did this. I’d never encountered sys.meta_path before.

Alternatives

If you’re starting a new app and want the proper micro experience, you maybe want django-micro instead. It has magic involved too, but different magic!

Usage

Create yourself a manage.py file in your app’s folder (eg: where your views.py, models.py are) and do something like this…

from microscope import run
from django.conf.urls import url  # or path etc.

def urls():
    from myapp.views import MyView
    return [url('^$', MyView.as_view())]

application = run(
    DEBUG=True,
    ROOT_URLCONF=urls
)

Running it

It’ll probably work, right? python manage.py [command] should all be OK. You could probably go even more minimal with dj-cmd so you can do dj r

Gunicorn

gunicorn manage and gunicorn manage:application should both work fine, as should DEBUG=0 gunicorn manage to change the setting by env-var.

UWSGI

Whatever madness of command-line arguments or ini configuration invokes uwsgi for you probably works; eg: uwsgi --http :8080 --wsgi-file `pwd`/manage.py --virtualenv $VIRTUAL_ENV

Environment variables should be fine?

mod_wsgi

It’d probably work, but environment variable substitution for settings won’t work at all, I don’t think.

URL configuration

the ROOT_URLCONF has to be a callable (function or object implementing __call__) to defer execution of imports related to userland code, which almost certainly depends on settings having already been configured. It’s also because your existing urls.py for the app may not be where you want the mountpoints when running it standalone vs as part of a monolith project.

You’ll eagerly get an ImproperlyConfigured exception when starting the app, if your ROOT_URLCONF is missing or not-callable.

Don’t worry, it’s only called at startup, not on every request!

If you do want to use another file, microscope.urlconf('dotted.path.to.urls') may help

from microscope import run, urlconf
application = run(ROOT_URLCONF=urlconf('path.to.urls'))

Tests

Literally none. If you have honcho you can do honcho -f demo_project.procfile start to start the demo_project.py (equivalent of manage.py) using runserver, gunicorn and uwsgi on ports 8000, 8001 and 8002 respectively.

The license

It’s the FreeBSD. There’s should be a LICENSE file in the root of the repository, and in any archives.


Copyright (c) 2018, Keryn Knight All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Change history for django-microscope

0.1.1

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-microscope-0.1.1.tar.gz (9.7 kB view hashes)

Uploaded Source

Built Distribution

django_microscope-0.1.1-py2.py3-none-any.whl (9.0 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page