File: apache.txt

package info (click to toggle)
django-downloadview 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 748 kB
  • sloc: python: 2,507; makefile: 187
file content (131 lines) | stat: -rw-r--r-- 3,477 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
######
Apache
######

If you serve Django behind Apache, then you can delegate the file streaming
to Apache and get increased performance:

* lower resources used by Python/Django workers ;
* faster download.

See `Apache mod_xsendfile documentation`_ for details.


*****************
Known limitations
*****************

* Apache needs access to the resource by path on local filesystem.
* Thus only files that live on local filesystem can be streamed by Apache.


************
Given a view
************

Let's consider the following view:

.. literalinclude:: /../demo/demoproject/apache/views.py
   :language: python
   :lines: 1-6, 8-16

What is important here is that the files will have an ``url`` property
implemented by storage. Let's setup an optimization rule based on that URL.

.. note::

   It is generally easier to setup rules based on URL rather than based on
   name in filesystem. This is because path is generally relative to storage,
   whereas URL usually contains some storage identifier, i.e. it is easier to
   target a specific location by URL rather than by filesystem name.


***************************
Setup XSendfile middlewares
***************************

Make sure ``django_downloadview.SmartDownloadMiddleware`` is in
``MIDDLEWARE`` of your `Django` settings.

Example:

.. literalinclude:: /../demo/demoproject/settings.py
   :language: python
   :lines: 63-70

Then set ``django_downloadview.apache.XSendfileMiddleware`` as
``DOWNLOADVIEW_BACKEND``:

.. literalinclude:: /../demo/demoproject/settings.py
   :language: python
   :lines: 79

Then register as many ``DOWNLOADVIEW_RULES`` as you wish:

.. literalinclude:: /../demo/demoproject/settings.py
   :language: python
   :lines: 84, 92-100, 110

Each item in ``DOWNLOADVIEW_RULES`` is a dictionary of keyword arguments passed
to the middleware factory. In the example above, we capture responses by
``source_url`` and convert them to internal redirects to ``destination_dir``.

.. autoclass:: django_downloadview.apache.middlewares.XSendfileMiddleware
   :members:
   :inherited-members:
   :undoc-members:
   :show-inheritance:
   :member-order: bysource


****************************************
Per-view setup with x_sendfile decorator
****************************************

Middlewares should be enough for most use cases, but you may want per-view
configuration. For `Apache`, there is ``x_sendfile``:

.. autofunction:: django_downloadview.apache.decorators.x_sendfile

As an example:

.. literalinclude:: /../demo/demoproject/apache/views.py
   :language: python
   :lines: 1-7, 17-


*************************************
Test responses with assert_x_sendfile
*************************************

Use :func:`~django_downloadview.apache.decorators.assert_x_sendfile`
function as a shortcut in your tests.

.. literalinclude:: /../demo/demoproject/apache/tests.py
   :language: python

.. autofunction:: django_downloadview.apache.tests.assert_x_sendfile

The tests above assert the `Django` part is OK. Now let's configure `Apache`.


************
Setup Apache
************

See `Apache mod_xsendfile documentation`_ for details.


*********************************************
Assert everything goes fine with healthchecks
*********************************************

:doc:`Healthchecks </healthchecks>` are the best way to check the complete
setup.


.. rubric:: References

.. target-notes::

.. _`Apache mod_xsendfile documentation`: https://tn123.org/mod_xsendfile/