/
contextualize_example.py
62 lines (45 loc) · 1.46 KB
/
contextualize_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
from sanic import Sanic
from sanic.response import text
from spf import SanicPluginsFramework
from spf.plugins.contextualize import instance as contextualize
app = Sanic(__name__)
spf = SanicPluginsFramework(app)
# You can create a context middleware _before_ registering the plugin
@contextualize.middleware
def middle1(request, context):
shared = context.shared
r = shared.request
r.hello = "true"
# and with args
@contextualize.middleware(priority=6)
def middle2(request, context):
shared = context.shared
r = shared.request
a_ = r.hello
# You can create a context route _before_ registering the plugin
@contextualize.route('/1')
def index1(request, context):
shared = context.shared
_ = shared.request
return text("hello world")
contextualize = spf.register_plugin(contextualize)
# Or you can create a context route _after_ registering the plugin
@contextualize.middleware
def middle3(request, context):
shared = context.shared
r = shared.request
r.test = "true"
# and with args
@contextualize.middleware(priority=7)
def middle4(request, context):
shared = context.shared
r = shared.request
_ = r.test
# And you can create a context route _after_ registering the plugin
@contextualize.route('/2/<args>')
def index2(request, args, context):
shared = context.shared
_ = shared.request
return text("hello world")
if __name__ == "__main__":
app.run("127.0.0.1", port=8098, debug=True, auto_reload=False)