-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.py
119 lines (92 loc) · 3.22 KB
/
example.py
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
from datetime import datetime, timedelta
from spiderweb.decorators import csrf_exempt
from spiderweb.example_validator import CommentForm
from spiderweb.main import SpiderwebRouter
from spiderweb.exceptions import ServerError
from spiderweb.response import (
HttpResponse,
JsonResponse,
TemplateResponse,
RedirectResponse,
)
app = SpiderwebRouter(
templates_dirs=["templates"],
middleware=[
"spiderweb.middleware.cors.CorsMiddleware",
"spiderweb.middleware.sessions.SessionMiddleware",
"spiderweb.middleware.csrf.CSRFMiddleware",
"example_middleware.TestMiddleware",
"example_middleware.RedirectMiddleware",
"spiderweb.middleware.pydantic.PydanticMiddleware",
"example_middleware.ExplodingMiddleware",
# "example_middleware.CaseTransformMiddleware",
],
staticfiles_dirs=["static_files"],
append_slash=False, # default
cors_allow_all_origins=True,
static_url="static_stuff",
debug=True,
case_transform_middleware_type="spongebob",
)
@app.route("/")
def index(request):
return TemplateResponse(request, "test.html", context={"value": "TEST!"})
@app.route("/example/<int:id>/<str:name>")
def example_with_multiple_values(request, id, name):
return HttpResponse(body=f"Example with id {id} and name {name}")
@app.route("/redirect")
def redirect(request):
return RedirectResponse("/")
@app.route("/json")
def json(request):
return JsonResponse(data={"key": "value"})
@app.route("/error")
def error(request):
raise ServerError
@app.route("/middleware")
def middleware(request):
return HttpResponse(
body="We'll never hit this because it's redirected in middleware"
)
@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"])
def form(request: CommentForm):
if request.method == "POST":
return JsonResponse(data=request.validated_data.dict())
else:
return TemplateResponse(request, "form.html")
@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']}")
@app.route("/cookies")
def cookies(request):
print("request.COOKIES: ", request.COOKIES)
resp = HttpResponse(body="COOKIES! NOM NOM NOM")
resp.set_cookie(name="nom", value="everyonelovescookies")
resp.set_cookie(name="nom2", value="seriouslycookies")
resp.set_cookie(
name="nom3",
value="yumyum",
partitioned=True,
expires=datetime.utcnow() + timedelta(seconds=10),
max_age=15,
)
return resp
if __name__ == "__main__":
# can also add routes like this:
# 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()