django-hostmap 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_hostmap-0.1.0/LICENSE +21 -0
- django_hostmap-0.1.0/PKG-INFO +163 -0
- django_hostmap-0.1.0/README.md +129 -0
- django_hostmap-0.1.0/pyproject.toml +91 -0
- django_hostmap-0.1.0/setup.cfg +4 -0
- django_hostmap-0.1.0/src/django_hostmap.egg-info/PKG-INFO +163 -0
- django_hostmap-0.1.0/src/django_hostmap.egg-info/SOURCES.txt +33 -0
- django_hostmap-0.1.0/src/django_hostmap.egg-info/dependency_links.txt +1 -0
- django_hostmap-0.1.0/src/django_hostmap.egg-info/requires.txt +9 -0
- django_hostmap-0.1.0/src/django_hostmap.egg-info/top_level.txt +1 -0
- django_hostmap-0.1.0/src/hostmap/__init__.py +4 -0
- django_hostmap-0.1.0/src/hostmap/apps.py +61 -0
- django_hostmap-0.1.0/src/hostmap/checks.py +252 -0
- django_hostmap-0.1.0/src/hostmap/conf.py +60 -0
- django_hostmap-0.1.0/src/hostmap/context.py +68 -0
- django_hostmap-0.1.0/src/hostmap/management/__init__.py +0 -0
- django_hostmap-0.1.0/src/hostmap/management/commands/__init__.py +0 -0
- django_hostmap-0.1.0/src/hostmap/management/commands/hostmap.py +31 -0
- django_hostmap-0.1.0/src/hostmap/map.py +194 -0
- django_hostmap-0.1.0/src/hostmap/middleware.py +79 -0
- django_hostmap-0.1.0/src/hostmap/resolvers.py +159 -0
- django_hostmap-0.1.0/src/hostmap/testing/__init__.py +10 -0
- django_hostmap-0.1.0/src/hostmap/testing/fixtures.py +18 -0
- django_hostmap-0.1.0/src/hostmap/testing/helpers.py +33 -0
- django_hostmap-0.1.0/src/hostmap/urls.py +196 -0
- django_hostmap-0.1.0/tests/test_checks.py +199 -0
- django_hostmap-0.1.0/tests/test_command.py +29 -0
- django_hostmap-0.1.0/tests/test_comparison.py +95 -0
- django_hostmap-0.1.0/tests/test_concurrency.py +55 -0
- django_hostmap-0.1.0/tests/test_edge_cases.py +119 -0
- django_hostmap-0.1.0/tests/test_explicit_api.py +84 -0
- django_hostmap-0.1.0/tests/test_patch_off.py +56 -0
- django_hostmap-0.1.0/tests/test_redirects.py +50 -0
- django_hostmap-0.1.0/tests/test_routing.py +113 -0
- django_hostmap-0.1.0/tests/test_testing_helpers.py +35 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ICV
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-hostmap
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Host-based URL routing and host-aware reversing for Django, with zero call-site changes
|
|
5
|
+
Author-email: ICV <dev@icv.dev>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/icvoss/django-hostmap
|
|
8
|
+
Project-URL: Source Code, https://github.com/icvoss/django-hostmap
|
|
9
|
+
Project-URL: Changelog, https://github.com/icvoss/django-hostmap/blob/main/CHANGELOG.md
|
|
10
|
+
Project-URL: Issue Tracker, https://github.com/icvoss/django-hostmap/issues
|
|
11
|
+
Project-URL: Documentation, https://github.com/icvoss/django-hostmap/tree/main/docs
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Framework :: Django
|
|
14
|
+
Classifier: Framework :: Django :: 5.2
|
|
15
|
+
Classifier: Framework :: Django :: 6.0
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
22
|
+
Requires-Python: >=3.12
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: Django>=5.2
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest-django>=4.8; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-cov>=5.0; extra == "dev"
|
|
30
|
+
Requires-Dist: ruff>=0.5.0; extra == "dev"
|
|
31
|
+
Requires-Dist: mypy>=1.10; extra == "dev"
|
|
32
|
+
Requires-Dist: django-stubs>=5.0; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# django-hostmap
|
|
36
|
+
|
|
37
|
+
Host-based URL routing and host-aware reversing for Django, with zero
|
|
38
|
+
call-site changes.
|
|
39
|
+
|
|
40
|
+
`django-hostmap` routes requests to different URLconfs by host (subdomain or
|
|
41
|
+
full domain) and makes URL reversing host-aware without changing a single
|
|
42
|
+
call site: the stock `reverse()`, `reverse_lazy()` and `{% url %}` keep
|
|
43
|
+
working everywhere, including inside third-party apps. Links within the
|
|
44
|
+
current host stay path-relative, exactly as Django produces them; links to
|
|
45
|
+
views on another host come back as absolute URLs. Configuration is one
|
|
46
|
+
declarative host map in settings plus one middleware.
|
|
47
|
+
|
|
48
|
+
It is the ecosystem's replacement for django-hosts, whose parallel reverse
|
|
49
|
+
API forces invasive changes across templates and Python code and cannot fix
|
|
50
|
+
third-party apps that call `django.urls.reverse()`.
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install django-hostmap
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
INSTALLED_APPS = [
|
|
60
|
+
# ...
|
|
61
|
+
"hostmap",
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
MIDDLEWARE = [
|
|
65
|
+
"django.middleware.security.SecurityMiddleware",
|
|
66
|
+
"hostmap.middleware.HostmapMiddleware", # before CommonMiddleware
|
|
67
|
+
"django.middleware.common.CommonMiddleware",
|
|
68
|
+
# ...
|
|
69
|
+
]
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`ROOT_URLCONF` remains required (Django needs it at startup) and should point
|
|
73
|
+
at the default entry's URLconf (`hostmap.W003` warns on mismatch).
|
|
74
|
+
|
|
75
|
+
## The host map
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
HOSTMAP = {
|
|
79
|
+
"www": {"subdomain": "www", "urlconf": "config.urls.www"},
|
|
80
|
+
"api": {"subdomain": "api", "urlconf": "config.urls.api"},
|
|
81
|
+
"apex": {"host": "example.com", "redirect_to": "www"},
|
|
82
|
+
}
|
|
83
|
+
HOSTMAP_PARENT_DOMAIN = "example.com"
|
|
84
|
+
HOSTMAP_DEFAULT = "www"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
With `www` active:
|
|
88
|
+
|
|
89
|
+
| Call | Returns |
|
|
90
|
+
|------|---------|
|
|
91
|
+
| `reverse("blog:index")` | `/blog/` (byte-identical to stock Django) |
|
|
92
|
+
| `reverse("api:user-detail", args=[7])` | `https://api.example.com/users/7/` |
|
|
93
|
+
| `{% url "api:user-detail" 7 %}` | the absolute URL, no template changes |
|
|
94
|
+
|
|
95
|
+
Same-host links stay relative; cross-host links come back absolute. Nothing
|
|
96
|
+
at the call site changes.
|
|
97
|
+
|
|
98
|
+
## Settings
|
|
99
|
+
|
|
100
|
+
| Setting | Default | Description |
|
|
101
|
+
|---------|---------|-------------|
|
|
102
|
+
| `HOSTMAP` | `{}` | The host map. Empty map disables all behaviour |
|
|
103
|
+
| `HOSTMAP_PARENT_DOMAIN` | `""` | Domain joined to `subdomain` entries |
|
|
104
|
+
| `HOSTMAP_DEFAULT` | `""` | Entry for unmatched hosts and out-of-request reversing |
|
|
105
|
+
| `HOSTMAP_PATCH_REVERSE` | `True` | Make stock `reverse()` / `{% url %}` host-aware |
|
|
106
|
+
| `HOSTMAP_SCHEME` | `"https"` | Scheme for cross-host absolute URLs |
|
|
107
|
+
| `HOSTMAP_PORT` | `""` | Port appended to all generated hosts |
|
|
108
|
+
| `HOSTMAP_UNMATCHED` | `"default"` | `"default"` routes unmatched hosts to the default entry; `"reject"` returns 404 |
|
|
109
|
+
| `HOSTMAP_REDIRECT_PERMANENT` | `True` | `redirect_to` entries use 301, else 302 |
|
|
110
|
+
|
|
111
|
+
## Explicit API
|
|
112
|
+
|
|
113
|
+
For code that needs an absolute URL regardless of the active host (emails,
|
|
114
|
+
Celery tasks, API payloads, webhooks):
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from hostmap.urls import reverse, build_absolute_uri, use_host
|
|
118
|
+
|
|
119
|
+
build_absolute_uri("api:user-detail", args=[7]) # always absolute
|
|
120
|
+
with use_host("api"):
|
|
121
|
+
reverse("user-detail", args=[7]) # api active
|
|
122
|
+
|
|
123
|
+
with use_host(host="acme.example.com"): # a wildcard host
|
|
124
|
+
reverse("dashboard")
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Development story
|
|
128
|
+
|
|
129
|
+
Modern browsers resolve `*.localhost` to loopback (RFC 6761), so the whole
|
|
130
|
+
map works locally with no `/etc/hosts` edits:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
HOSTMAP_PARENT_DOMAIN = "localhost"
|
|
134
|
+
HOSTMAP_SCHEME = "http"
|
|
135
|
+
HOSTMAP_PORT = "8000"
|
|
136
|
+
ALLOWED_HOSTS = [".localhost"]
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Behind a proxy
|
|
140
|
+
|
|
141
|
+
Host routing is only as good as the Host header that reaches Django. Behind a
|
|
142
|
+
reverse proxy, either the proxy passes Host through unchanged or the
|
|
143
|
+
deployment sets `USE_X_FORWARDED_HOST = True` with the proxy sending
|
|
144
|
+
`X-Forwarded-Host`. Misrouted hosts behind nginx are otherwise the first
|
|
145
|
+
support issue.
|
|
146
|
+
|
|
147
|
+
## Escape hatch
|
|
148
|
+
|
|
149
|
+
`HOSTMAP_PATCH_REVERSE = False` degrades cleanly to routing-only: the stock
|
|
150
|
+
`reverse()` is left untouched (cross-host links regress to paths), routing
|
|
151
|
+
keeps working, and the explicit API still works. Set it if a Django upgrade
|
|
152
|
+
lands before a compatible django-hostmap release; the startup self-test and
|
|
153
|
+
`hostmap.W004` warn before anything breaks.
|
|
154
|
+
|
|
155
|
+
## Diagnostics
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
manage.py hostmap # print the resolved map for the current settings
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Licence
|
|
162
|
+
|
|
163
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# django-hostmap
|
|
2
|
+
|
|
3
|
+
Host-based URL routing and host-aware reversing for Django, with zero
|
|
4
|
+
call-site changes.
|
|
5
|
+
|
|
6
|
+
`django-hostmap` routes requests to different URLconfs by host (subdomain or
|
|
7
|
+
full domain) and makes URL reversing host-aware without changing a single
|
|
8
|
+
call site: the stock `reverse()`, `reverse_lazy()` and `{% url %}` keep
|
|
9
|
+
working everywhere, including inside third-party apps. Links within the
|
|
10
|
+
current host stay path-relative, exactly as Django produces them; links to
|
|
11
|
+
views on another host come back as absolute URLs. Configuration is one
|
|
12
|
+
declarative host map in settings plus one middleware.
|
|
13
|
+
|
|
14
|
+
It is the ecosystem's replacement for django-hosts, whose parallel reverse
|
|
15
|
+
API forces invasive changes across templates and Python code and cannot fix
|
|
16
|
+
third-party apps that call `django.urls.reverse()`.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install django-hostmap
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
INSTALLED_APPS = [
|
|
26
|
+
# ...
|
|
27
|
+
"hostmap",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
MIDDLEWARE = [
|
|
31
|
+
"django.middleware.security.SecurityMiddleware",
|
|
32
|
+
"hostmap.middleware.HostmapMiddleware", # before CommonMiddleware
|
|
33
|
+
"django.middleware.common.CommonMiddleware",
|
|
34
|
+
# ...
|
|
35
|
+
]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`ROOT_URLCONF` remains required (Django needs it at startup) and should point
|
|
39
|
+
at the default entry's URLconf (`hostmap.W003` warns on mismatch).
|
|
40
|
+
|
|
41
|
+
## The host map
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
HOSTMAP = {
|
|
45
|
+
"www": {"subdomain": "www", "urlconf": "config.urls.www"},
|
|
46
|
+
"api": {"subdomain": "api", "urlconf": "config.urls.api"},
|
|
47
|
+
"apex": {"host": "example.com", "redirect_to": "www"},
|
|
48
|
+
}
|
|
49
|
+
HOSTMAP_PARENT_DOMAIN = "example.com"
|
|
50
|
+
HOSTMAP_DEFAULT = "www"
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
With `www` active:
|
|
54
|
+
|
|
55
|
+
| Call | Returns |
|
|
56
|
+
|------|---------|
|
|
57
|
+
| `reverse("blog:index")` | `/blog/` (byte-identical to stock Django) |
|
|
58
|
+
| `reverse("api:user-detail", args=[7])` | `https://api.example.com/users/7/` |
|
|
59
|
+
| `{% url "api:user-detail" 7 %}` | the absolute URL, no template changes |
|
|
60
|
+
|
|
61
|
+
Same-host links stay relative; cross-host links come back absolute. Nothing
|
|
62
|
+
at the call site changes.
|
|
63
|
+
|
|
64
|
+
## Settings
|
|
65
|
+
|
|
66
|
+
| Setting | Default | Description |
|
|
67
|
+
|---------|---------|-------------|
|
|
68
|
+
| `HOSTMAP` | `{}` | The host map. Empty map disables all behaviour |
|
|
69
|
+
| `HOSTMAP_PARENT_DOMAIN` | `""` | Domain joined to `subdomain` entries |
|
|
70
|
+
| `HOSTMAP_DEFAULT` | `""` | Entry for unmatched hosts and out-of-request reversing |
|
|
71
|
+
| `HOSTMAP_PATCH_REVERSE` | `True` | Make stock `reverse()` / `{% url %}` host-aware |
|
|
72
|
+
| `HOSTMAP_SCHEME` | `"https"` | Scheme for cross-host absolute URLs |
|
|
73
|
+
| `HOSTMAP_PORT` | `""` | Port appended to all generated hosts |
|
|
74
|
+
| `HOSTMAP_UNMATCHED` | `"default"` | `"default"` routes unmatched hosts to the default entry; `"reject"` returns 404 |
|
|
75
|
+
| `HOSTMAP_REDIRECT_PERMANENT` | `True` | `redirect_to` entries use 301, else 302 |
|
|
76
|
+
|
|
77
|
+
## Explicit API
|
|
78
|
+
|
|
79
|
+
For code that needs an absolute URL regardless of the active host (emails,
|
|
80
|
+
Celery tasks, API payloads, webhooks):
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from hostmap.urls import reverse, build_absolute_uri, use_host
|
|
84
|
+
|
|
85
|
+
build_absolute_uri("api:user-detail", args=[7]) # always absolute
|
|
86
|
+
with use_host("api"):
|
|
87
|
+
reverse("user-detail", args=[7]) # api active
|
|
88
|
+
|
|
89
|
+
with use_host(host="acme.example.com"): # a wildcard host
|
|
90
|
+
reverse("dashboard")
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Development story
|
|
94
|
+
|
|
95
|
+
Modern browsers resolve `*.localhost` to loopback (RFC 6761), so the whole
|
|
96
|
+
map works locally with no `/etc/hosts` edits:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
HOSTMAP_PARENT_DOMAIN = "localhost"
|
|
100
|
+
HOSTMAP_SCHEME = "http"
|
|
101
|
+
HOSTMAP_PORT = "8000"
|
|
102
|
+
ALLOWED_HOSTS = [".localhost"]
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Behind a proxy
|
|
106
|
+
|
|
107
|
+
Host routing is only as good as the Host header that reaches Django. Behind a
|
|
108
|
+
reverse proxy, either the proxy passes Host through unchanged or the
|
|
109
|
+
deployment sets `USE_X_FORWARDED_HOST = True` with the proxy sending
|
|
110
|
+
`X-Forwarded-Host`. Misrouted hosts behind nginx are otherwise the first
|
|
111
|
+
support issue.
|
|
112
|
+
|
|
113
|
+
## Escape hatch
|
|
114
|
+
|
|
115
|
+
`HOSTMAP_PATCH_REVERSE = False` degrades cleanly to routing-only: the stock
|
|
116
|
+
`reverse()` is left untouched (cross-host links regress to paths), routing
|
|
117
|
+
keeps working, and the explicit API still works. Set it if a Django upgrade
|
|
118
|
+
lands before a compatible django-hostmap release; the startup self-test and
|
|
119
|
+
`hostmap.W004` warn before anything breaks.
|
|
120
|
+
|
|
121
|
+
## Diagnostics
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
manage.py hostmap # print the resolved map for the current settings
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Licence
|
|
128
|
+
|
|
129
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "django-hostmap"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Host-based URL routing and host-aware reversing for Django, with zero call-site changes"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.12"
|
|
12
|
+
authors = [{name = "ICV", email = "dev@icv.dev"}]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Framework :: Django",
|
|
16
|
+
"Framework :: Django :: 5.2",
|
|
17
|
+
"Framework :: Django :: 6.0",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
23
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"Django>=5.2",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
dev = [
|
|
31
|
+
"pytest>=8.0",
|
|
32
|
+
"pytest-django>=4.8",
|
|
33
|
+
"pytest-cov>=5.0",
|
|
34
|
+
"ruff>=0.5.0",
|
|
35
|
+
"mypy>=1.10",
|
|
36
|
+
"django-stubs>=5.0",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[project.urls]
|
|
40
|
+
"Homepage" = "https://github.com/icvoss/django-hostmap"
|
|
41
|
+
"Source Code" = "https://github.com/icvoss/django-hostmap"
|
|
42
|
+
"Changelog" = "https://github.com/icvoss/django-hostmap/blob/main/CHANGELOG.md"
|
|
43
|
+
"Issue Tracker" = "https://github.com/icvoss/django-hostmap/issues"
|
|
44
|
+
"Documentation" = "https://github.com/icvoss/django-hostmap/tree/main/docs"
|
|
45
|
+
|
|
46
|
+
[tool.setuptools.packages.find]
|
|
47
|
+
where = ["src"]
|
|
48
|
+
|
|
49
|
+
[tool.pytest.ini_options]
|
|
50
|
+
DJANGO_SETTINGS_MODULE = "settings"
|
|
51
|
+
pythonpath = ["src", "tests"]
|
|
52
|
+
|
|
53
|
+
[tool.coverage.run]
|
|
54
|
+
source = ["src"]
|
|
55
|
+
omit = ["*/testing/*"]
|
|
56
|
+
|
|
57
|
+
[tool.coverage.report]
|
|
58
|
+
show_missing = true
|
|
59
|
+
exclude_lines = [
|
|
60
|
+
"pragma: no cover",
|
|
61
|
+
"if TYPE_CHECKING:",
|
|
62
|
+
"if __name__ ==",
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
[tool.ruff]
|
|
66
|
+
target-version = "py312"
|
|
67
|
+
line-length = 120
|
|
68
|
+
src = ["src"]
|
|
69
|
+
|
|
70
|
+
[tool.ruff.lint]
|
|
71
|
+
select = [
|
|
72
|
+
"E", # pycodestyle errors
|
|
73
|
+
"W", # pycodestyle warnings
|
|
74
|
+
"F", # pyflakes
|
|
75
|
+
"I", # isort
|
|
76
|
+
"B", # flake8-bugbear
|
|
77
|
+
"UP", # pyupgrade
|
|
78
|
+
"N", # pep8-naming
|
|
79
|
+
"SIM", # flake8-simplify
|
|
80
|
+
"DJ", # flake8-django
|
|
81
|
+
]
|
|
82
|
+
ignore = [
|
|
83
|
+
"E501", # line length handled by the formatter
|
|
84
|
+
"SIM105", # contextlib.suppress less readable than try/except/pass for NoReverseMatch
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
[tool.ruff.lint.isort]
|
|
88
|
+
known-first-party = ["hostmap"]
|
|
89
|
+
|
|
90
|
+
[tool.ruff.format]
|
|
91
|
+
quote-style = "double"
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-hostmap
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Host-based URL routing and host-aware reversing for Django, with zero call-site changes
|
|
5
|
+
Author-email: ICV <dev@icv.dev>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/icvoss/django-hostmap
|
|
8
|
+
Project-URL: Source Code, https://github.com/icvoss/django-hostmap
|
|
9
|
+
Project-URL: Changelog, https://github.com/icvoss/django-hostmap/blob/main/CHANGELOG.md
|
|
10
|
+
Project-URL: Issue Tracker, https://github.com/icvoss/django-hostmap/issues
|
|
11
|
+
Project-URL: Documentation, https://github.com/icvoss/django-hostmap/tree/main/docs
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Framework :: Django
|
|
14
|
+
Classifier: Framework :: Django :: 5.2
|
|
15
|
+
Classifier: Framework :: Django :: 6.0
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
22
|
+
Requires-Python: >=3.12
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: Django>=5.2
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest-django>=4.8; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-cov>=5.0; extra == "dev"
|
|
30
|
+
Requires-Dist: ruff>=0.5.0; extra == "dev"
|
|
31
|
+
Requires-Dist: mypy>=1.10; extra == "dev"
|
|
32
|
+
Requires-Dist: django-stubs>=5.0; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# django-hostmap
|
|
36
|
+
|
|
37
|
+
Host-based URL routing and host-aware reversing for Django, with zero
|
|
38
|
+
call-site changes.
|
|
39
|
+
|
|
40
|
+
`django-hostmap` routes requests to different URLconfs by host (subdomain or
|
|
41
|
+
full domain) and makes URL reversing host-aware without changing a single
|
|
42
|
+
call site: the stock `reverse()`, `reverse_lazy()` and `{% url %}` keep
|
|
43
|
+
working everywhere, including inside third-party apps. Links within the
|
|
44
|
+
current host stay path-relative, exactly as Django produces them; links to
|
|
45
|
+
views on another host come back as absolute URLs. Configuration is one
|
|
46
|
+
declarative host map in settings plus one middleware.
|
|
47
|
+
|
|
48
|
+
It is the ecosystem's replacement for django-hosts, whose parallel reverse
|
|
49
|
+
API forces invasive changes across templates and Python code and cannot fix
|
|
50
|
+
third-party apps that call `django.urls.reverse()`.
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install django-hostmap
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
INSTALLED_APPS = [
|
|
60
|
+
# ...
|
|
61
|
+
"hostmap",
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
MIDDLEWARE = [
|
|
65
|
+
"django.middleware.security.SecurityMiddleware",
|
|
66
|
+
"hostmap.middleware.HostmapMiddleware", # before CommonMiddleware
|
|
67
|
+
"django.middleware.common.CommonMiddleware",
|
|
68
|
+
# ...
|
|
69
|
+
]
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`ROOT_URLCONF` remains required (Django needs it at startup) and should point
|
|
73
|
+
at the default entry's URLconf (`hostmap.W003` warns on mismatch).
|
|
74
|
+
|
|
75
|
+
## The host map
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
HOSTMAP = {
|
|
79
|
+
"www": {"subdomain": "www", "urlconf": "config.urls.www"},
|
|
80
|
+
"api": {"subdomain": "api", "urlconf": "config.urls.api"},
|
|
81
|
+
"apex": {"host": "example.com", "redirect_to": "www"},
|
|
82
|
+
}
|
|
83
|
+
HOSTMAP_PARENT_DOMAIN = "example.com"
|
|
84
|
+
HOSTMAP_DEFAULT = "www"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
With `www` active:
|
|
88
|
+
|
|
89
|
+
| Call | Returns |
|
|
90
|
+
|------|---------|
|
|
91
|
+
| `reverse("blog:index")` | `/blog/` (byte-identical to stock Django) |
|
|
92
|
+
| `reverse("api:user-detail", args=[7])` | `https://api.example.com/users/7/` |
|
|
93
|
+
| `{% url "api:user-detail" 7 %}` | the absolute URL, no template changes |
|
|
94
|
+
|
|
95
|
+
Same-host links stay relative; cross-host links come back absolute. Nothing
|
|
96
|
+
at the call site changes.
|
|
97
|
+
|
|
98
|
+
## Settings
|
|
99
|
+
|
|
100
|
+
| Setting | Default | Description |
|
|
101
|
+
|---------|---------|-------------|
|
|
102
|
+
| `HOSTMAP` | `{}` | The host map. Empty map disables all behaviour |
|
|
103
|
+
| `HOSTMAP_PARENT_DOMAIN` | `""` | Domain joined to `subdomain` entries |
|
|
104
|
+
| `HOSTMAP_DEFAULT` | `""` | Entry for unmatched hosts and out-of-request reversing |
|
|
105
|
+
| `HOSTMAP_PATCH_REVERSE` | `True` | Make stock `reverse()` / `{% url %}` host-aware |
|
|
106
|
+
| `HOSTMAP_SCHEME` | `"https"` | Scheme for cross-host absolute URLs |
|
|
107
|
+
| `HOSTMAP_PORT` | `""` | Port appended to all generated hosts |
|
|
108
|
+
| `HOSTMAP_UNMATCHED` | `"default"` | `"default"` routes unmatched hosts to the default entry; `"reject"` returns 404 |
|
|
109
|
+
| `HOSTMAP_REDIRECT_PERMANENT` | `True` | `redirect_to` entries use 301, else 302 |
|
|
110
|
+
|
|
111
|
+
## Explicit API
|
|
112
|
+
|
|
113
|
+
For code that needs an absolute URL regardless of the active host (emails,
|
|
114
|
+
Celery tasks, API payloads, webhooks):
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from hostmap.urls import reverse, build_absolute_uri, use_host
|
|
118
|
+
|
|
119
|
+
build_absolute_uri("api:user-detail", args=[7]) # always absolute
|
|
120
|
+
with use_host("api"):
|
|
121
|
+
reverse("user-detail", args=[7]) # api active
|
|
122
|
+
|
|
123
|
+
with use_host(host="acme.example.com"): # a wildcard host
|
|
124
|
+
reverse("dashboard")
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Development story
|
|
128
|
+
|
|
129
|
+
Modern browsers resolve `*.localhost` to loopback (RFC 6761), so the whole
|
|
130
|
+
map works locally with no `/etc/hosts` edits:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
HOSTMAP_PARENT_DOMAIN = "localhost"
|
|
134
|
+
HOSTMAP_SCHEME = "http"
|
|
135
|
+
HOSTMAP_PORT = "8000"
|
|
136
|
+
ALLOWED_HOSTS = [".localhost"]
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Behind a proxy
|
|
140
|
+
|
|
141
|
+
Host routing is only as good as the Host header that reaches Django. Behind a
|
|
142
|
+
reverse proxy, either the proxy passes Host through unchanged or the
|
|
143
|
+
deployment sets `USE_X_FORWARDED_HOST = True` with the proxy sending
|
|
144
|
+
`X-Forwarded-Host`. Misrouted hosts behind nginx are otherwise the first
|
|
145
|
+
support issue.
|
|
146
|
+
|
|
147
|
+
## Escape hatch
|
|
148
|
+
|
|
149
|
+
`HOSTMAP_PATCH_REVERSE = False` degrades cleanly to routing-only: the stock
|
|
150
|
+
`reverse()` is left untouched (cross-host links regress to paths), routing
|
|
151
|
+
keeps working, and the explicit API still works. Set it if a Django upgrade
|
|
152
|
+
lands before a compatible django-hostmap release; the startup self-test and
|
|
153
|
+
`hostmap.W004` warn before anything breaks.
|
|
154
|
+
|
|
155
|
+
## Diagnostics
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
manage.py hostmap # print the resolved map for the current settings
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Licence
|
|
162
|
+
|
|
163
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/django_hostmap.egg-info/PKG-INFO
|
|
5
|
+
src/django_hostmap.egg-info/SOURCES.txt
|
|
6
|
+
src/django_hostmap.egg-info/dependency_links.txt
|
|
7
|
+
src/django_hostmap.egg-info/requires.txt
|
|
8
|
+
src/django_hostmap.egg-info/top_level.txt
|
|
9
|
+
src/hostmap/__init__.py
|
|
10
|
+
src/hostmap/apps.py
|
|
11
|
+
src/hostmap/checks.py
|
|
12
|
+
src/hostmap/conf.py
|
|
13
|
+
src/hostmap/context.py
|
|
14
|
+
src/hostmap/map.py
|
|
15
|
+
src/hostmap/middleware.py
|
|
16
|
+
src/hostmap/resolvers.py
|
|
17
|
+
src/hostmap/urls.py
|
|
18
|
+
src/hostmap/management/__init__.py
|
|
19
|
+
src/hostmap/management/commands/__init__.py
|
|
20
|
+
src/hostmap/management/commands/hostmap.py
|
|
21
|
+
src/hostmap/testing/__init__.py
|
|
22
|
+
src/hostmap/testing/fixtures.py
|
|
23
|
+
src/hostmap/testing/helpers.py
|
|
24
|
+
tests/test_checks.py
|
|
25
|
+
tests/test_command.py
|
|
26
|
+
tests/test_comparison.py
|
|
27
|
+
tests/test_concurrency.py
|
|
28
|
+
tests/test_edge_cases.py
|
|
29
|
+
tests/test_explicit_api.py
|
|
30
|
+
tests/test_patch_off.py
|
|
31
|
+
tests/test_redirects.py
|
|
32
|
+
tests/test_routing.py
|
|
33
|
+
tests/test_testing_helpers.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hostmap
|