Skip to content

Commit

Permalink
feat: init repo and add WhiteNoiseSPAMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
pradishb committed Aug 6, 2024
0 parents commit a95aad3
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Upload Python Package
on:
push:
branches:
- release
permissions:
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.x"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
135 changes: 135 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
.vscode

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
dist

# project specific
/test.py
/temp
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024 Pradish Bijukchhe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include LICENSE
include README.md
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# whitenoise-spa

Serve SPA (Single Page Application) in django using whitenoise

## Installation

You can install the package via pip:

```
pip install whitenoise-spa
```

## Usage

1. Edit your `settings.py` file and add `whitenoise_spa.middleware.WhiteNoiseSPAMiddleware` to the MIDDLEWARE list. The middleware should be placed directly after the Django SecurityMiddleware (if you are using it) and before all other middleware. This middleware replaces the whitenoise middleware.
```python
MIDDLEWARE = [
# ...
"django.middleware.security.SecurityMiddleware",
"whitenoise_spa.middleware.WhiteNoiseSPAMiddleware",
# ...
]
```
2. Configure SPA root and urls.
```python
WHITENOISE_SPA_ROOT = BASE_DIR / "dist"
WHITENOISE_SPA_URLS = ["/", "/login/", "/settings/"]
```

## Limitations

- Does not support wildcard urls for SPA urls.

## License

This project is licensed under the terms of the MIT license.
37 changes: 37 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[build-system]
requires = ["setuptools>=70.0.0"]
build-backend = "setuptools.build_meta"

[project]
name = "whitenoise-spa"
version = "1.0.0"
dependencies = ["whitenoise"]
requires-python = ">=3"
authors = [{ name = "Pradish Bijukchhe", email = "[email protected]" }]
description = "Serve SPA (Single Page Application) in django using whitenoise"
readme = "README.md"
license = { file = "LICENSE" }
keywords = []
classifiers = ["Programming Language :: Python :: 3"]

[project.urls]
Homepage = "https://github.com/sandbox-pokhara/whitenoise-spa"
Issues = "https://github.com/sandbox-pokhara/whitenoise-spa/issues"

[tool.setuptools]
include-package-data = true

[tool.setuptools.package-dir]
"whitenoise_spa" = "whitenoise_spa"

[tool.isort]
line_length = 79
force_single_line = true

[tool.black]
line-length = 79
preview = true

[tool.pyright]
include = ["whitenoise_spa"]
typeCheckingMode = "strict"
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
whitenoise
Empty file added whitenoise_spa/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions whitenoise_spa/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import Any

from django.conf import settings
from whitenoise.middleware import WhiteNoiseMiddleware # type:ignore


class WhiteNoiseSPAMiddleware(WhiteNoiseMiddleware):
def __init__(self, *args: Any, **kwargs: Any):
self.urls: list[str] = getattr(settings, "WHITENOISE_SPA_URLS", ["/"])
super().__init__(*args, **kwargs) # type:ignore
root = getattr(settings, "WHITENOISE_SPA_ROOT", None)
if root is not None:
self.add_files(root) # type:ignore

def update_files_dictionary(self, *args: Any, **kwargs: Any):
# add spa urls (DEBUG=False)
super().update_files_dictionary(*args, **kwargs) # type:ignore
self.files: dict[str, Any]
if "/index.html" not in self.files:
return
for url in self.urls:
self.files[url] = self.files["/index.html"]

def find_file(self, url: str):
# add spa urls (DEBUG=True)
if url in self.urls:
url = "/index.html"
return super().find_file(url) # type:ignore
Empty file added whitenoise_spa/py.typed
Empty file.

0 comments on commit a95aad3

Please sign in to comment.