File-based enforcer provider with hot-reload for casbin-fastapi-decorator
Project description
casbin-fastapi-decorator-file
File-based enforcer provider with hot-reload for casbin-fastapi-decorator.
Provides CachedFileEnforcerProvider — loads the casbin Enforcer once from model.conf + policy.csv and automatically reloads when either file changes on disk (via watchdog).
Install
pip install casbin-fastapi-decorator-file
Usage
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from casbin_fastapi_decorator import PermissionGuard
from casbin_fastapi_decorator_file import CachedFileEnforcerProvider
provider = CachedFileEnforcerProvider(
model_path="casbin/model.conf",
policy_path="casbin/policy.csv",
)
@asynccontextmanager
async def lifespan(app: FastAPI):
async with provider: # starts file watcher
yield # stops file watcher on shutdown
guard = PermissionGuard(
user_provider=get_current_user,
enforcer_provider=provider,
error_factory=lambda *_: HTTPException(403, "Forbidden"),
)
app = FastAPI(lifespan=lifespan)
@app.get("/articles")
@guard.require_permission("articles", "read")
async def list_articles(): ...
Any change to model.conf or policy.csv is detected automatically — the enforcer reloads on the next request with zero downtime.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file casbin_fastapi_decorator_file-1.2.1.tar.gz.
File metadata
- Download URL: casbin_fastapi_decorator_file-1.2.1.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e061d710a496ddebf3a915644ca17ab45b197df7130101c3b73ccbcbe1e57baf
|
|
| MD5 |
83c6788149b763a82abbc988b85dd11b
|
|
| BLAKE2b-256 |
abcba42b12f8ee04b69e2e7b6944e3866b5a3ad0b800bd55931ae6e830cefd72
|
File details
Details for the file casbin_fastapi_decorator_file-1.2.1-py3-none-any.whl.
File metadata
- Download URL: casbin_fastapi_decorator_file-1.2.1-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5d045a6ee1bc5d64c45561a6f546de20f509f048a52fb2b1bc32e1962a09abe
|
|
| MD5 |
321f538122178af46f4819d69b96d013
|
|
| BLAKE2b-256 |
cd78b1169e196519c57d33f172b614cd5503e04299b76f3f0706efa80b4a3b5d
|