Turn a Django app into a Django project so it can run as a microservice or whatever
Project description
- 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:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_microscope-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2574947f49297f522b2a99959b52f68ac4837f850f824d2038787d5fe02e21dd |
|
MD5 | 06d2613e4ffae920e7f17af031a1b05b |
|
BLAKE2b-256 | a5ff1ea702275800aaeef7886d19a026af0b8015ce627ba1ce7aae954523957f |