spiderweb/example.py

118 lines
3.1 KiB
Python
Raw Permalink Normal View History

2024-08-19 11:13:32 -04:00
from datetime import datetime, timedelta
from spiderweb.decorators import csrf_exempt
2024-08-21 21:07:44 -04:00
from spiderweb.example_validator import CommentForm
2024-08-18 17:49:16 -04:00
from spiderweb.main import SpiderwebRouter
2024-08-12 11:54:16 -04:00
from spiderweb.exceptions import ServerError
2024-08-14 17:28:01 -04:00
from spiderweb.response import (
HttpResponse,
JsonResponse,
TemplateResponse,
RedirectResponse,
)
2024-08-06 01:10:00 -04:00
2024-08-09 12:02:46 -04:00
app = SpiderwebRouter(
2024-08-09 12:02:46 -04:00
templates_dirs=["templates"],
middleware=[
2024-09-01 21:05:43 -04:00
"spiderweb.middleware.cors.CorsMiddleware",
"spiderweb.middleware.sessions.SessionMiddleware",
"spiderweb.middleware.csrf.CSRFMiddleware",
2024-08-09 12:02:46 -04:00
"example_middleware.TestMiddleware",
2024-08-09 14:15:32 -04:00
"example_middleware.RedirectMiddleware",
2024-08-21 21:07:44 -04:00
"spiderweb.middleware.pydantic.PydanticMiddleware",
2024-08-09 14:15:32 -04:00
"example_middleware.ExplodingMiddleware",
2024-08-09 12:02:46 -04:00
],
2024-08-14 19:27:37 -04:00
staticfiles_dirs=["static_files"],
2024-08-14 17:28:01 -04:00
append_slash=False, # default
cors_allow_all_origins=True,
static_url="static_stuff",
debug=True,
2024-08-09 12:02:46 -04:00
)
2024-08-06 01:10:00 -04:00
@app.route("/")
def index(request):
return TemplateResponse(request, "test.html", context={"value": "TEST!"})
@app.route("/example/<int:id>/<str:name>")
2024-09-16 18:34:55 -04:00
def example_with_multiple_values(request, id, name):
return HttpResponse(body=f"Example with id {id} and name {name}")
2024-08-06 01:10:00 -04:00
@app.route("/redirect")
def redirect(request):
return RedirectResponse("/")
2024-08-09 12:02:46 -04:00
@app.route("/json")
def json(request):
return JsonResponse(data={"key": "value"})
@app.route("/error")
def error(request):
2024-08-12 11:54:16 -04:00
raise ServerError
2024-08-09 12:02:46 -04:00
@app.route("/middleware")
def middleware(request):
return HttpResponse(
body="We'll never hit this because it's redirected in middleware"
)
2024-08-09 14:15:32 -04:00
@app.route("/example/<int:id>")
def example(request, id):
return HttpResponse(body=f"Example with id {id}")
@app.error(405)
def http405(request) -> HttpResponse:
return HttpResponse(body="Method not allowed", status_code=405)
@csrf_exempt
@app.route("/form", allowed_methods=["GET", "POST"])
2024-08-21 21:07:44 -04:00
def form(request: CommentForm):
if request.method == "POST":
2024-08-21 21:07:44 -04:00
return JsonResponse(data=request.validated_data.dict())
else:
return TemplateResponse(request, "form.html")
2024-08-21 21:07:44 -04:00
@app.route("/session")
def session(request):
if "test" not in request.SESSION:
request.SESSION["test"] = 0
else:
request.SESSION["test"] += 1
return HttpResponse(body=f"Session test: {request.SESSION['test']}")
2024-08-19 11:13:32 -04:00
@app.route("/cookies")
def cookies(request):
print("request.COOKIES: ", request.COOKIES)
resp = HttpResponse(body="COOKIES! NOM NOM NOM")
2024-08-21 21:07:44 -04:00
resp.set_cookie(name="nom", value="everyonelovescookies")
2024-08-19 11:13:32 -04:00
resp.set_cookie(name="nom2", value="seriouslycookies")
resp.set_cookie(
name="nom3",
value="yumyum",
partitioned=True,
2024-08-21 21:07:44 -04:00
expires=datetime.utcnow() + timedelta(seconds=10),
max_age=15,
2024-08-19 11:13:32 -04:00
)
return resp
2024-08-06 01:10:00 -04:00
if __name__ == "__main__":
2024-08-09 12:02:46 -04:00
# can also add routes like this:
2024-08-06 01:10:00 -04:00
# app.add_route("/", index)
#
# If gunicorn is installed, you can run this file directly through gunicorn with
# `gunicorn --workers=2 "example:app"` -- the biggest thing here is that all
# configuration must be done using decorators or top level in the file.
app.start()