-
Notifications
You must be signed in to change notification settings - Fork 11
/
rolling.py
61 lines (48 loc) · 1.76 KB
/
rolling.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
"""
This example demonstrates base usage of this library. The session will expire in 10 seconds of inactivity but will be
extended by another 10 seconds while you use the demo.
Usage:
> uvicorn examples.rolling:app
Open http://localhost:8000 for demo page.
"""
import datetime
import json
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import HTMLResponse, RedirectResponse
from starlette.routing import Route
from starsessions import CookieStore, SessionAutoloadMiddleware, SessionMiddleware
async def homepage(request: Request) -> HTMLResponse:
"""Access this view (GET "/") to display session contents."""
return HTMLResponse(
f"<div>session data: {json.dumps(request.session)}</div>"
"<ol>"
'<li><a href="/set">set example data</a></li>'
'<li><a href="/clean">clear example data</a></li>'
"</ol>"
)
async def set_time(request: Request) -> RedirectResponse:
"""Access this view (GET "/set") to set session contents."""
request.session["date"] = datetime.datetime.now().isoformat()
return RedirectResponse("/")
async def clean(request: Request) -> RedirectResponse:
"""Access this view (GET "/clean") to remove all session contents."""
request.session.clear()
return RedirectResponse("/")
routes = [
Route("/", endpoint=homepage),
Route("/set", endpoint=set_time),
Route("/clean", endpoint=clean),
]
middleware = [
Middleware(
SessionMiddleware,
store=CookieStore(secret_key="key"),
rolling=True,
cookie_https_only=False,
lifetime=10,
),
Middleware(SessionAutoloadMiddleware),
]
app = Starlette(debug=True, routes=routes, middleware=middleware)