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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

Details for the file django-microscope-0.1.1.tar.gz.

File metadata

  • Download URL: django-microscope-0.1.1.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.2

File hashes

Hashes for django-microscope-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8ed8debf1d49c255dc4e64959223b2dd6fe1da4ab0848abdcf6f9bb36b8f05c8
MD5 81d58d32dc8be591307ae27669e78601
BLAKE2b-256 0bf9193a442e7e9f62c64f45b321ff3e365d17ff6c4a09e73321a3dab49bb6ee

See more details on using hashes here.

File details

Details for the file django_microscope-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_microscope-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.2

File hashes

Hashes for django_microscope-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2574947f49297f522b2a99959b52f68ac4837f850f824d2038787d5fe02e21dd
MD5 06d2613e4ffae920e7f17af031a1b05b
BLAKE2b-256 a5ff1ea702275800aaeef7886d19a026af0b8015ce627ba1ce7aae954523957f

See more details on using hashes here.

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