Compare commits
477 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
a5a9658896 | ||
|
91d7e6a77d | ||
|
eb3d78f687 | ||
|
d255d1aae1 | ||
|
47215d4635 | ||
|
38ff9069f3 | ||
|
4dde4572ec | ||
|
31d14704cb | ||
|
6a89f4b2fe | ||
|
16256522f6 | ||
|
205795d1e8 | ||
|
9cbe1fb8ad | ||
|
31d7ba8f8c | ||
|
dc3c4d1393 | ||
|
929d270569 | ||
|
93714df051 | ||
|
6e61eab872 | ||
|
6848ff24d8 | ||
|
666371bb92 | ||
|
4a2b82e42e | ||
|
5dd1623192 | ||
|
976da69e79 | ||
|
11a0b15194 | ||
|
c21999a248 | ||
|
c17230ef94 | ||
|
049983cb70 | ||
|
e374409567 | ||
|
4068a0d83d | ||
|
70da5e9879 | ||
|
f48506d620 | ||
|
f2cc83c1ba | ||
|
273825dab6 | ||
|
9a7dafd531 | ||
|
50117d174c | ||
|
af67801062 | ||
|
6eaab2a7e5 | ||
|
d680af3709 | ||
|
a8c2d77c91 | ||
|
6e1c787e5d | ||
|
932088e37e | ||
|
1a63b9bec0 | ||
|
61aa16f6ac | ||
|
71cd53b64e | ||
|
89188f5fc6 | ||
|
a245ab3773 | ||
|
ac1f56118a | ||
|
53820bc241 | ||
|
009954003c | ||
|
8f265b8169 | ||
|
5ee36fd933 | ||
|
08a81c81be | ||
|
5a0ed75171 | ||
|
d62a92fac9 | ||
|
88c918e72f | ||
|
c8aab8fb3d | ||
|
ecacfd396b | ||
|
3c361e9852 | ||
|
a5d7d03413 | ||
|
259e458847 | ||
|
cb49c2b26d | ||
|
dfc0704831 | ||
|
d238995f1b | ||
|
6f5303e080 | ||
|
5e7f6998bd | ||
|
c7a71cd00c | ||
|
9cb9e88678 | ||
|
30c53b6857 | ||
|
4ad8168bb0 | ||
|
28f5b3c301 | ||
|
c573019e7f | ||
|
029f564032 | ||
|
2abe66b670 | ||
|
911485d52e | ||
|
4744a89c33 | ||
|
f7040ccec8 | ||
|
518152d97e | ||
|
0e44e9cacb | ||
|
bfb54b0969 | ||
|
154863d6c6 | ||
|
a3ff0c13b7 | ||
|
95ee518aec | ||
|
71d3d87bcc | ||
|
b276b91c21 | ||
|
064168f3c8 | ||
|
db39e127bf | ||
|
13e9ab7ba9 | ||
|
92e7463721 | ||
|
8e720365c2 | ||
|
d4041161c7 | ||
|
f32437bf13 | ||
|
0909e94527 | ||
|
aef2673c38 | ||
|
4c14910d5b | ||
|
beae35f921 | ||
|
ad4e526c77 | ||
|
4422d0c34d | ||
|
ad9183d21d | ||
|
d70636ba2e | ||
|
da23f85675 | ||
|
3f4663b9f8 | ||
|
65d7447cf6 | ||
|
5369291c27 | ||
|
1c4925edf7 | ||
|
6b9edfd05c | ||
|
97f33f42df | ||
|
15a588a90c | ||
|
82421e7efc | ||
|
f891995b48 | ||
|
5052321801 | ||
|
23ce4eaaa4 | ||
|
23a430c4ad | ||
|
ec158ffa69 | ||
|
6e32270036 | ||
|
43ba381e7b | ||
|
16503319e5 | ||
|
389363ab71 | ||
|
7f894c45b3 | ||
|
4726cf1910 | ||
|
d352a4155e | ||
|
e5010286b4 | ||
|
358498db96 | ||
|
e4999401ab | ||
|
c8df0aa2cb | ||
|
5fb207176b | ||
|
a12b560478 | ||
|
753ee992a6 | ||
|
09089b1bd3 | ||
|
7ddbe5e844 | ||
|
ab5a7038af | ||
|
4f3c780dc3 | ||
|
71f7765a4c | ||
|
0392d1dcfc | ||
|
7827b1b41d | ||
|
8e9342e188 | ||
|
2f6f2bfa76 | ||
|
dee09d7fff | ||
|
9cf38a0a83 | ||
|
3def3d3569 | ||
|
e100a14fd4 | ||
|
2fa28f1711 | ||
|
9d415e4ec6 | ||
|
312ab298fd | ||
|
2fc21ad576 | ||
|
8f6c87c3d6 | ||
|
4429e76532 | ||
|
e4be70bae8 | ||
|
13d5a44278 | ||
|
aba333bfb6 | ||
|
b59da498cc | ||
|
70382f21ba | ||
|
0e1bf89fad | ||
|
6c48c8b3ba | ||
|
d1c5e8003b | ||
|
ce926a34f2 | ||
|
a744041e38 | ||
|
2f90a85df1 | ||
|
a411bc06e3 | ||
|
1668e1532f | ||
|
b87982769f | ||
|
65b53a5f3f | ||
|
49789b7841 | ||
|
c249004c30 | ||
|
4ee2e57ec8 | ||
|
86ae5f981c | ||
|
2bfa65e0de | ||
|
293278bb08 | ||
|
5d683c6ea4 | ||
|
78b6723149 | ||
|
3a6cc7389c | ||
|
cc97287f8e | ||
|
00218aa9f2 | ||
|
874718db94 | ||
|
bb4474897f | ||
|
0cb342aef4 | ||
|
030987480c | ||
|
f6fdc80b40 | ||
|
361c242473 | ||
|
32962d1e1c | ||
|
6e0a6871b5 | ||
|
0030425c8c | ||
|
c9dbc8ed26 | ||
|
44b108b564 | ||
|
2a8e91052f | ||
|
0c9df02e66 | ||
|
7523e87937 | ||
|
d4fb44e986 | ||
|
68b654d981 | ||
|
88bc6d8966 | ||
|
ac388d644b | ||
|
bb517ddcca | ||
|
b8d991420b | ||
|
4a416e177a | ||
|
8dfa49b648 | ||
|
8b0eaa097c | ||
|
101151b419 | ||
|
4669036f45 | ||
|
9bf9067c99 | ||
|
a7bc8b56ba | ||
|
371985d129 | ||
|
3eae00898d | ||
|
dc3ccba527 | ||
|
b91ffed010 | ||
|
8c07e388cd | ||
|
98ce4bdeb2 | ||
|
4659069350 | ||
|
080d41627a | ||
|
d799c5f03c | ||
|
abe062b371 | ||
|
b5a00ac1ca | ||
|
f282865362 | ||
|
377c2ada38 | ||
|
264453459e | ||
|
3d383d7b97 | ||
|
c0cc26021b | ||
|
96c027bad5 | ||
|
b2a1bc69f5 | ||
|
426742b3e2 | ||
|
ab35121864 | ||
|
cf3c205fa5 | ||
|
19f6544923 | ||
|
f641830d26 | ||
|
a8d55e180c | ||
|
55c36e0240 | ||
|
2c03eee329 | ||
|
65e28b8c22 | ||
|
dfd33dd63d | ||
|
722a6db8d9 | ||
|
9c576c74db | ||
|
523db190a7 | ||
|
95631b9686 | ||
|
0860bfe1f1 | ||
|
85e7b712b9 | ||
|
b731a6b48c | ||
|
cde02b5936 | ||
|
abeb8d0bc0 | ||
|
9a9f72ad64 | ||
|
392a497366 | ||
|
36e6a6c506 | ||
|
a361b345ad | ||
|
f5bd6e3b2f | ||
|
6c7df68c7c | ||
|
5b82884f8b | ||
|
f0f81ec458 | ||
|
71cc30e5cd | ||
|
645310cff6 | ||
|
2f30b5748a | ||
|
5e1ef96934 | ||
|
57e98b62b3 | ||
|
3262878ebd | ||
|
5e12edbc38 | ||
|
50a606adee | ||
|
f995612073 | ||
|
bc08383acd | ||
|
b83a1a184c | ||
|
59dd6814f8 | ||
|
f7abf3db1b | ||
|
cf1d2148ac | ||
|
b5f2bd9b0e | ||
|
ba2670e99c | ||
|
6ffc4d9756 | ||
|
595d2c76ac | ||
|
d9796e9b1e | ||
|
404c5f9f9e | ||
|
a937e08ef0 | ||
|
ef4f058a6c | ||
|
69c5dde9bf | ||
|
945885d501 | ||
|
9d0b54c90d | ||
|
2e5c288fea | ||
|
f32ef20b74 | ||
|
e2eefaac55 | ||
|
e1cfbf0fd9 | ||
|
08c5689441 | ||
|
8dbda247d6 | ||
|
71a631237d | ||
|
e22ff3828b | ||
|
b1b12e004e | ||
|
5308fec354 | ||
|
0ba57d4701 | ||
|
54ca6a6178 | ||
|
7dd4a78cf2 | ||
|
52ff49512a | ||
|
5a48b94089 | ||
|
ba1c73d947 | ||
|
4732b6bdfa | ||
|
a6e78b70ab | ||
|
bb1174afc5 | ||
|
df8abe9cfd | ||
|
c3bca97ee1 | ||
|
c3b6fa1bba | ||
|
94d496afe1 | ||
|
7b7a572f9b | ||
|
1b8cb742f9 | ||
|
3492d180a8 | ||
|
021da38373 | ||
|
ac784759d5 | ||
|
36eda2cd62 | ||
|
08a4b3013f | ||
|
1dd0332e8b | ||
|
a90877ac31 | ||
|
8b7ea27a48 | ||
|
8df80e276b | ||
|
30572c972d | ||
|
53da4dd091 | ||
|
108a4a99c7 | ||
|
7c180376d6 | ||
|
f39b8b32f7 | ||
|
c543d19f8a | ||
|
80fca9aef7 | ||
|
5bb9aa0c2c | ||
|
83c746ee57 | ||
|
aff6604636 | ||
|
2c80571a8a | ||
|
d964b552af | ||
|
48f8b37b74 | ||
|
141be0028d | ||
|
a140c47195 | ||
|
0c3a8392f2 | ||
|
16875b1f41 | ||
|
b1f31f2eeb | ||
|
d16b9e5a02 | ||
|
680484bdc8 | ||
|
05cd44b5dd | ||
|
ba374139f4 | ||
|
72a745bfd5 | ||
|
3a6fac7d59 | ||
|
28ba8e53df | ||
|
9b26358e63 | ||
|
e21521f45c | ||
|
30479765cb | ||
|
53a571ec6c | ||
|
ad97cac313 | ||
|
1a352ddf55 | ||
|
5ba43decf2 | ||
|
8f06d035cb | ||
|
b716f48c84 | ||
|
42b1e7143e | ||
|
eba7821a6d | ||
|
93a0246c03 | ||
|
dfd1787a49 | ||
|
4998fd54c0 | ||
|
7be5f0ed3d | ||
|
938d2b5923 | ||
|
13630a79ad | ||
|
2586989eb7 | ||
|
6763e2bb0a | ||
|
15a8b5c894 | ||
|
8a2ea626c6 | ||
|
01f238de79 | ||
|
7bed5a5c3c | ||
|
2fea954dcf | ||
|
d4660d0ca7 | ||
|
824f41d6e0 | ||
|
1165663ec1 | ||
|
b1a57a8b62 | ||
|
d76925cf35 | ||
|
400f54c7ec | ||
|
ec7e4390e8 | ||
|
5ca8bb85cd | ||
|
5c624f8b76 | ||
|
2c25af8cf5 | ||
|
be905e0009 | ||
|
0d2d62eae5 | ||
|
00a1ee0cb6 | ||
|
f0d44822bc | ||
|
e771e4e019 | ||
|
82e5f57edd | ||
|
93a697f6ea | ||
|
a733d32715 | ||
|
c41d7136e8 | ||
|
bb65dda277 | ||
|
e1e9a7cf20 | ||
|
27f64ddae2 | ||
|
4b968dc611 | ||
|
170177feb0 | ||
|
eef44ae179 | ||
|
161efc67a3 | ||
|
dbfc11f4c5 | ||
|
e5ea4f5019 | ||
|
62d0b05309 | ||
|
aae8f13d9a | ||
|
eac900d363 | ||
|
cd0c3d3eea | ||
|
b461cc338c | ||
|
7c34b57e92 | ||
|
c35a53fe24 | ||
|
055c78b475 | ||
|
a9a52ef1e9 | ||
|
a760989354 | ||
|
dee95fd1c0 | ||
|
9c48c7d39b | ||
|
fc5e3b4ede | ||
|
baf96275bf | ||
|
3a45d3291b | ||
|
c7307c1f85 | ||
|
dec46d5c4a | ||
|
93313e6385 | ||
|
070236677c | ||
|
6b86921c7a | ||
|
8f3e076661 | ||
|
37f0a51063 | ||
|
d6343c367f | ||
|
bad9fd0258 | ||
|
d402d0362e | ||
|
209579f280 | ||
|
9e23e75fb4 | ||
|
8f8c00a99d | ||
|
bb5501f4c4 | ||
|
d3cbcf0e46 | ||
|
15502182e3 | ||
|
375319038f | ||
|
4c29846cb1 | ||
|
c942a5c51c | ||
|
a913e712a7 | ||
|
7f63ad5484 | ||
|
a498c29ec9 | ||
|
973c315790 | ||
|
aea2e4e7f4 | ||
|
55a5ab4be1 | ||
|
5377a6eee3 | ||
|
6057da71f3 | ||
|
e91b3d40a2 | ||
|
b89f9a57e0 | ||
|
b850e49cb3 | ||
|
6b68c3702e | ||
|
5f17e9591b | ||
|
64f0496d9e | ||
|
0d5b2a0f69 | ||
|
c08b153cee | ||
|
a434ffa8b7 | ||
|
967c4e6a4e | ||
|
3f1e9ff528 | ||
|
96cc49e31e | ||
|
94e5f82a81 | ||
|
ffca01ffff | ||
|
0165c9c644 | ||
|
7b47a4bebc | ||
|
a46ea4fc59 | ||
|
4358a7eefd | ||
|
b958cdc151 | ||
|
afb527b37c | ||
|
65b76f2762 | ||
|
e54685d34d | ||
|
6009e6d35d | ||
|
d5f09dd9d8 | ||
|
ffae6f56c5 | ||
|
838563c751 | ||
|
83705b91c2 | ||
|
5abeae8f46 | ||
|
693f2a5014 | ||
|
5545264cea | ||
|
e04f206c50 | ||
|
e9459792a4 | ||
|
dadf76ce72 | ||
|
33d7f4da6b | ||
|
5f79291b55 | ||
|
792a72bdf4 | ||
|
c32e7fd678 | ||
|
76ef641743 | ||
|
1f0f4ef5d5 | ||
|
933d005e5d | ||
|
f8f215772c | ||
|
a0066e5752 | ||
|
976a4c764d | ||
|
0d7e2f0d67 | ||
|
6c03dd87b1 | ||
|
0c252e7904 | ||
|
8f4e0ad3c8 | ||
|
95b5260b27 | ||
|
2f48e81e0b | ||
|
7028eae083 | ||
|
574a9c27a6 | ||
|
f9414f89f3 | ||
|
fcdbeb7bd4 | ||
|
206992fe40 | ||
|
31337ba92a | ||
|
150d75b7c6 |
|
@ -1,2 +0,0 @@
|
||||||
[tool.black]
|
|
||||||
line-length = 79
|
|
12
.coveragerc
12
.coveragerc
|
@ -1,7 +1,15 @@
|
||||||
[run]
|
[run]
|
||||||
branch = True
|
branch = True
|
||||||
source = sanic
|
source = sanic
|
||||||
omit = site-packages, sanic/utils.py, sanic/__main__.py
|
omit =
|
||||||
|
site-packages
|
||||||
|
sanic/__main__.py
|
||||||
|
sanic/server/legacy.py
|
||||||
|
sanic/compat.py
|
||||||
|
sanic/simple.py
|
||||||
|
sanic/utils.py
|
||||||
|
sanic/cli
|
||||||
|
sanic/pages
|
||||||
|
|
||||||
[html]
|
[html]
|
||||||
directory = coverage
|
directory = coverage
|
||||||
|
@ -13,3 +21,5 @@ exclude_lines =
|
||||||
noqa
|
noqa
|
||||||
NOQA
|
NOQA
|
||||||
pragma: no cover
|
pragma: no cover
|
||||||
|
TYPE_CHECKING
|
||||||
|
skip_empty = True
|
||||||
|
|
3
.github/CODEOWNERS
vendored
Normal file
3
.github/CODEOWNERS
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
* @sanic-org/sanic-release-managers
|
||||||
|
/sanic/ @sanic-org/framework
|
||||||
|
/tests/ @sanic-org/framework
|
12
.github/FUNDING.yml
vendored
Normal file
12
.github/FUNDING.yml
vendored
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
# These are supported funding model platforms
|
||||||
|
|
||||||
|
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
||||||
|
patreon: # Replace with a single Patreon username
|
||||||
|
open_collective: sanic-org # Replace with a single Open Collective username
|
||||||
|
ko_fi: # Replace with a single Ko-fi username
|
||||||
|
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
||||||
|
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
||||||
|
liberapay: # Replace with a single Liberapay username
|
||||||
|
issuehunt: # Replace with a single IssueHunt username
|
||||||
|
otechie: # Replace with a single Otechie username
|
||||||
|
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
78
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
Normal file
78
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
name: 🐞 Bug report
|
||||||
|
description: Create a report to help us improve
|
||||||
|
labels: ["bug", "triage"]
|
||||||
|
body:
|
||||||
|
- type: checkboxes
|
||||||
|
id: existing
|
||||||
|
attributes:
|
||||||
|
label: Is there an existing issue for this?
|
||||||
|
description: Please search to see if an issue already exists for the bug you encountered.
|
||||||
|
options:
|
||||||
|
- label: I have searched the existing issues
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: description
|
||||||
|
attributes:
|
||||||
|
label: Describe the bug
|
||||||
|
description: A clear and concise description of what the bug is, make sure to paste any exceptions and tracebacks using markdown code-block syntax to make it easier to read.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: code
|
||||||
|
attributes:
|
||||||
|
label: Code snippet
|
||||||
|
description: |
|
||||||
|
Relevant source code, make sure to remove what is not necessary. Please try and format your code so that it is easier to read. For example:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from sanic import Sanic
|
||||||
|
|
||||||
|
app = Sanic("Example")
|
||||||
|
```
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: textarea
|
||||||
|
id: expected
|
||||||
|
attributes:
|
||||||
|
label: Expected Behavior
|
||||||
|
description: A concise description of what you expected to happen.
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: dropdown
|
||||||
|
id: running
|
||||||
|
attributes:
|
||||||
|
label: How do you run Sanic?
|
||||||
|
options:
|
||||||
|
- Sanic CLI
|
||||||
|
- As a module
|
||||||
|
- As a script (`app.run` or `Sanic.serve`)
|
||||||
|
- ASGI
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: os
|
||||||
|
attributes:
|
||||||
|
label: Operating System
|
||||||
|
description: What OS?
|
||||||
|
options:
|
||||||
|
- Linux
|
||||||
|
- MacOS
|
||||||
|
- Windows
|
||||||
|
- Other (tell us in the description)
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
id: version
|
||||||
|
attributes:
|
||||||
|
label: Sanic Version
|
||||||
|
description: Check startup logs or try `sanic --version`
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: additional
|
||||||
|
attributes:
|
||||||
|
label: Additional context
|
||||||
|
description: Add any other context about the problem here.
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
|
25
.github/ISSUE_TEMPLATE/bug_report.md
vendored
25
.github/ISSUE_TEMPLATE/bug_report.md
vendored
|
@ -1,25 +0,0 @@
|
||||||
---
|
|
||||||
name: Bug report
|
|
||||||
about: Create a report to help us improve
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Describe the bug**
|
|
||||||
A clear and concise description of what the bug is, make sure to paste any exceptions and tracebacks.
|
|
||||||
|
|
||||||
|
|
||||||
**Code snippet**
|
|
||||||
Relevant source code, make sure to remove what is not necessary.
|
|
||||||
|
|
||||||
|
|
||||||
**Expected behavior**
|
|
||||||
A clear and concise description of what you expected to happen.
|
|
||||||
|
|
||||||
|
|
||||||
**Environment (please complete the following information):**
|
|
||||||
- OS: [e.g. iOS]
|
|
||||||
- Version [e.g. 0.8.3]
|
|
||||||
|
|
||||||
|
|
||||||
**Additional context**
|
|
||||||
Add any other context about the problem here.
|
|
5
.github/ISSUE_TEMPLATE/config.yml
vendored
5
.github/ISSUE_TEMPLATE/config.yml
vendored
|
@ -1,5 +1,8 @@
|
||||||
blank_issues_enabled: true
|
blank_issues_enabled: false
|
||||||
contact_links:
|
contact_links:
|
||||||
- name: Questions and Help
|
- name: Questions and Help
|
||||||
url: https://community.sanicframework.org/c/questions-and-help
|
url: https://community.sanicframework.org/c/questions-and-help
|
||||||
about: Do you need help with Sanic? Ask your questions here.
|
about: Do you need help with Sanic? Ask your questions here.
|
||||||
|
- name: Discussion and Support
|
||||||
|
url: https://discord.gg/FARQzAEMAA
|
||||||
|
about: For live discussion and support, checkout the Sanic Discord server.
|
||||||
|
|
34
.github/ISSUE_TEMPLATE/feature-request.yml
vendored
Normal file
34
.github/ISSUE_TEMPLATE/feature-request.yml
vendored
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
name: 🌟 Feature request
|
||||||
|
description: Suggest an enhancement for Sanic
|
||||||
|
labels: ["feature request"]
|
||||||
|
body:
|
||||||
|
- type: checkboxes
|
||||||
|
id: existing
|
||||||
|
attributes:
|
||||||
|
label: Is there an existing issue for this?
|
||||||
|
description: Please search to see if an issue already exists for the enhancement you are proposing.
|
||||||
|
options:
|
||||||
|
- label: I have searched the existing issues
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: description
|
||||||
|
attributes:
|
||||||
|
label: Is your feature request related to a problem? Please describe.
|
||||||
|
description: A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: textarea
|
||||||
|
id: code
|
||||||
|
attributes:
|
||||||
|
label: Describe the solution you'd like
|
||||||
|
description: A clear and concise description of what you want to happen.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: additional
|
||||||
|
attributes:
|
||||||
|
label: Additional context
|
||||||
|
description: Add any other context about the problem here.
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
|
16
.github/ISSUE_TEMPLATE/feature_request.md
vendored
16
.github/ISSUE_TEMPLATE/feature_request.md
vendored
|
@ -1,16 +0,0 @@
|
||||||
---
|
|
||||||
name: Feature request
|
|
||||||
about: Suggest an idea for Sanic
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
**Is your feature request related to a problem? Please describe.**
|
|
||||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
|
||||||
|
|
||||||
|
|
||||||
**Describe the solution you'd like**
|
|
||||||
A clear and concise description of what you want to happen.
|
|
||||||
|
|
||||||
|
|
||||||
**Additional context**
|
|
||||||
Add any other context or sample code about the feature request here.
|
|
33
.github/ISSUE_TEMPLATE/rfc.yml
vendored
Normal file
33
.github/ISSUE_TEMPLATE/rfc.yml
vendored
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
name: 💡 Request for Comments
|
||||||
|
description: Open an RFC for discussion
|
||||||
|
labels: ["RFC"]
|
||||||
|
body:
|
||||||
|
- type: input
|
||||||
|
id: compare
|
||||||
|
attributes:
|
||||||
|
label: Link to code
|
||||||
|
description: If available, share a [comparison](https://github.com/sanic-org/sanic/compare) from a POC branch to main
|
||||||
|
placeholder: https://github.com/sanic-org/sanic/compare/main...some-new-branch
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: textarea
|
||||||
|
id: proposal
|
||||||
|
attributes:
|
||||||
|
label: Proposal
|
||||||
|
description: A thorough discussion of the proposal discussing the problem it solves, potential code, use cases, and impacts
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: additional
|
||||||
|
attributes:
|
||||||
|
label: Additional context
|
||||||
|
description: Add any other context that is relevant
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: checkboxes
|
||||||
|
id: breaking
|
||||||
|
attributes:
|
||||||
|
label: Is this a breaking change?
|
||||||
|
options:
|
||||||
|
- label: "Yes"
|
||||||
|
required: false
|
42
.github/workflows/codeql-analysis.yml
vendored
Normal file
42
.github/workflows/codeql-analysis.yml
vendored
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
name: "CodeQL"
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- current-release
|
||||||
|
- "*LTS"
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- current-release
|
||||||
|
- "*LTS"
|
||||||
|
types: [opened, synchronize, reopened, ready_for_review]
|
||||||
|
schedule:
|
||||||
|
- cron: '25 16 * * 0'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
analyze:
|
||||||
|
if: github.event.pull_request.draft == false
|
||||||
|
name: Analyze
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
language: [ 'python' ]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Initialize CodeQL
|
||||||
|
uses: github/codeql-action/init@v1
|
||||||
|
with:
|
||||||
|
languages: ${{ matrix.language }}
|
||||||
|
|
||||||
|
- name: Autobuild
|
||||||
|
uses: github/codeql-action/autobuild@v1
|
||||||
|
|
||||||
|
- name: Perform CodeQL Analysis
|
||||||
|
uses: github/codeql-action/analyze@v1
|
34
.github/workflows/coverage.yml
vendored
Normal file
34
.github/workflows/coverage.yml
vendored
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
name: Coverage check
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- current-release
|
||||||
|
- "*LTS"
|
||||||
|
tags:
|
||||||
|
- "!*" # Do not execute on tags
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- current-release
|
||||||
|
- "*LTS"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
coverage:
|
||||||
|
name: Check coverage
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Run coverage
|
||||||
|
uses: sanic-org/simple-tox-action@v1
|
||||||
|
with:
|
||||||
|
python-version: "3.11"
|
||||||
|
tox-env: coverage
|
||||||
|
ignore-errors: true
|
||||||
|
- name: Run Codecov
|
||||||
|
uses: codecov/codecov-action@v3
|
||||||
|
with:
|
||||||
|
files: ./coverage.xml
|
||||||
|
fail_ci_if_error: false
|
174
.github/workflows/publish-release.yml
vendored
Normal file
174
.github/workflows/publish-release.yml
vendored
Normal file
|
@ -0,0 +1,174 @@
|
||||||
|
name: Publish release
|
||||||
|
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
types: [created]
|
||||||
|
|
||||||
|
env:
|
||||||
|
IS_TEST: false
|
||||||
|
DOCKER_ORG_NAME: sanicframework
|
||||||
|
DOCKER_IMAGE_NAME: sanic
|
||||||
|
DOCKER_BASE_IMAGE_NAME: sanic-build
|
||||||
|
DOCKER_IMAGE_DOCKERFILE: ./docker/Dockerfile
|
||||||
|
DOCKER_BASE_IMAGE_DOCKERFILE: ./docker/Dockerfile-base
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
generate_info:
|
||||||
|
name: Generate info
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
outputs:
|
||||||
|
docker-tags: ${{ steps.generate_docker_info.outputs.tags }}
|
||||||
|
pypi-version: ${{ steps.parse_version_tag.outputs.pypi-version }}
|
||||||
|
steps:
|
||||||
|
- name: Parse version tag
|
||||||
|
id: parse_version_tag
|
||||||
|
env:
|
||||||
|
TAG_NAME: ${{ github.event.release.tag_name }}
|
||||||
|
run: |
|
||||||
|
tag_name="${{ env.TAG_NAME }}"
|
||||||
|
|
||||||
|
if [[ ! "${tag_name}" =~ ^v([0-9]{2})\.([0-9]{1,2})\.([0-9]+)$ ]]; then
|
||||||
|
echo "::error::Tag name must be in the format vYY.MM.MICRO"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
year_output="year=${BASH_REMATCH[1]}"
|
||||||
|
month_output="month=${BASH_REMATCH[2]}"
|
||||||
|
pypi_output="pypi-version=${tag_name#v}"
|
||||||
|
|
||||||
|
echo "${year_output}"
|
||||||
|
echo "${month_output}"
|
||||||
|
echo "${pypi_output}"
|
||||||
|
|
||||||
|
echo "${year_output}" >> $GITHUB_OUTPUT
|
||||||
|
echo "${month_output}" >> $GITHUB_OUTPUT
|
||||||
|
echo "${pypi_output}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Get latest release
|
||||||
|
id: get_latest_release
|
||||||
|
run: |
|
||||||
|
latest_tag=$(
|
||||||
|
curl -L \
|
||||||
|
-H "Accept: application/vnd.github+json" \
|
||||||
|
-H "Authorization: Bearer ${{ github.token }}" \
|
||||||
|
-H "X-GitHub-Api-Version: 2022-11-28" \
|
||||||
|
https://api.github.com/repos/${{ github.repository }}/releases/latest \
|
||||||
|
| jq -r '.tag_name'
|
||||||
|
)
|
||||||
|
echo "latest_tag=$latest_tag" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Generate Docker info
|
||||||
|
id: generate_docker_info
|
||||||
|
run: |
|
||||||
|
tag_year="${{ steps.parse_version_tag.outputs.year }}"
|
||||||
|
tag_month="${{ steps.parse_version_tag.outputs.month }}"
|
||||||
|
latest_tag="${{ steps.get_latest_release.outputs.latest_tag }}"
|
||||||
|
tag="${{ github.event.release.tag_name }}"
|
||||||
|
|
||||||
|
tags="${tag_year}.${tag_month}"
|
||||||
|
|
||||||
|
if [[ "${tag_month}" == "12" ]]; then
|
||||||
|
tags+=",LTS"
|
||||||
|
echo "::notice::Tag ${tag} is LTS version"
|
||||||
|
else
|
||||||
|
echo "::notice::Tag ${tag} is not LTS version"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "${latest_tag}" == "${{ github.event.release.tag_name }}" ]]; then
|
||||||
|
tags+=",latest"
|
||||||
|
echo "::notice::Tag ${tag} is marked as latest"
|
||||||
|
else
|
||||||
|
echo "::notice::Tag ${tag} is not marked as latest"
|
||||||
|
fi
|
||||||
|
|
||||||
|
tags_output="tags=${tags}"
|
||||||
|
|
||||||
|
echo "${tags_output}"
|
||||||
|
echo "${tags_output}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
publish_package:
|
||||||
|
name: Build and publish package
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: generate_info
|
||||||
|
steps:
|
||||||
|
- name: Checkout repo
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Setup Python
|
||||||
|
uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: "3.11"
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: pip install build twine
|
||||||
|
|
||||||
|
- name: Update package version
|
||||||
|
run: |
|
||||||
|
echo "__version__ = \"${{ needs.generate_info.outputs.pypi-version }}\"" > sanic/__version__.py
|
||||||
|
|
||||||
|
- name: Build a binary wheel and a source tarball
|
||||||
|
run: python -m build --sdist --wheel --outdir dist/ .
|
||||||
|
|
||||||
|
- name: Publish to PyPi 🚀
|
||||||
|
run: twine upload --non-interactive --disable-progress-bar dist/*
|
||||||
|
env:
|
||||||
|
TWINE_USERNAME: __token__
|
||||||
|
TWINE_PASSWORD: ${{ env.IS_TEST == 'true' && secrets.SANIC_TEST_PYPI_API_TOKEN || secrets.SANIC_PYPI_API_TOKEN }}
|
||||||
|
TWINE_REPOSITORY: ${{ env.IS_TEST == 'true' && 'testpypi' || 'pypi' }}
|
||||||
|
|
||||||
|
publish_docker:
|
||||||
|
name: Publish Docker / Python ${{ matrix.python-version }}
|
||||||
|
needs: [generate_info, publish_package]
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
fail-fast: true
|
||||||
|
matrix:
|
||||||
|
python-version: ["3.10", "3.11"]
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
|
||||||
|
- name: Login to Docker Hub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_ACCESS_USER }}
|
||||||
|
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push base image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
push: ${{ env.IS_TEST == 'false' }}
|
||||||
|
file: ${{ env.DOCKER_BASE_IMAGE_DOCKERFILE }}
|
||||||
|
tags: ${{ env.DOCKER_ORG_NAME }}/${{ env.DOCKER_BASE_IMAGE_NAME }}:${{ matrix.python-version }}
|
||||||
|
build-args: |
|
||||||
|
PYTHON_VERSION=${{ matrix.python-version }}
|
||||||
|
|
||||||
|
- name: Parse tags for this Python version
|
||||||
|
id: parse_tags
|
||||||
|
run: |
|
||||||
|
IFS=',' read -ra tags <<< "${{ needs.generate_info.outputs.docker-tags }}"
|
||||||
|
tag_args=""
|
||||||
|
|
||||||
|
for tag in "${tags[@]}"; do
|
||||||
|
tag_args+=",${{ env.DOCKER_ORG_NAME }}/${{ env.DOCKER_IMAGE_NAME }}:${tag}-py${{ matrix.python-version }}"
|
||||||
|
done
|
||||||
|
|
||||||
|
tag_args_output="tag_args=${tag_args:1}"
|
||||||
|
|
||||||
|
echo "${tag_args_output}"
|
||||||
|
echo "${tag_args_output}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Build and push Sanic image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
push: ${{ env.IS_TEST == 'false' }}
|
||||||
|
file: ${{ env.DOCKER_IMAGE_DOCKERFILE }}
|
||||||
|
tags: ${{ steps.parse_tags.outputs.tag_args }}
|
||||||
|
build-args: |
|
||||||
|
BASE_IMAGE_ORG=${{ env.DOCKER_ORG_NAME }}
|
||||||
|
BASE_IMAGE_NAME=${{ env.DOCKER_BASE_IMAGE_NAME }}
|
||||||
|
BASE_IMAGE_TAG=${{ matrix.python-version }}
|
||||||
|
SANIC_PYPI_VERSION=${{ needs.generate_info.outputs.pypi-version }}
|
56
.github/workflows/tests.yml
vendored
Normal file
56
.github/workflows/tests.yml
vendored
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
name: Tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- current-release
|
||||||
|
- "*LTS"
|
||||||
|
tags:
|
||||||
|
- "!*"
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- current-release
|
||||||
|
- "*LTS"
|
||||||
|
types: [opened, synchronize, reopened, ready_for_review]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
run_tests:
|
||||||
|
name: "${{ matrix.config.platform == 'windows-latest' && 'Windows' || 'Linux' }} / Python ${{ matrix.config.python-version }} / tox -e ${{ matrix.config.tox-env }}"
|
||||||
|
if: github.event.pull_request.draft == false
|
||||||
|
runs-on: ${{ matrix.config.platform || 'ubuntu-latest' }}
|
||||||
|
strategy:
|
||||||
|
fail-fast: true
|
||||||
|
matrix:
|
||||||
|
config:
|
||||||
|
- { python-version: "3.8", tox-env: security }
|
||||||
|
- { python-version: "3.9", tox-env: security }
|
||||||
|
- { python-version: "3.10", tox-env: security }
|
||||||
|
- { python-version: "3.11", tox-env: security }
|
||||||
|
- { python-version: "3.10", tox-env: lint }
|
||||||
|
# - { python-version: "3.10", tox-env: docs }
|
||||||
|
- { python-version: "3.8", tox-env: type-checking }
|
||||||
|
- { python-version: "3.9", tox-env: type-checking }
|
||||||
|
- { python-version: "3.10", tox-env: type-checking }
|
||||||
|
- { python-version: "3.11", tox-env: type-checking }
|
||||||
|
- { python-version: "3.8", tox-env: py38, max-attempts: 3 }
|
||||||
|
- { python-version: "3.8", tox-env: py38-no-ext, max-attempts: 3 }
|
||||||
|
- { python-version: "3.9", tox-env: py39, max-attempts: 3 }
|
||||||
|
- { python-version: "3.9", tox-env: py39-no-ext, max-attempts: 3 }
|
||||||
|
- { python-version: "3.10", tox-env: py310, max-attempts: 3 }
|
||||||
|
- { python-version: "3.10", tox-env: py310-no-ext, max-attempts: 3 }
|
||||||
|
- { python-version: "3.11", tox-env: py311, max-attempts: 3 }
|
||||||
|
- { python-version: "3.11", tox-env: py311-no-ext, max-attempts: 3 }
|
||||||
|
- { python-version: "3.8", tox-env: py38-no-ext, platform: windows-latest, ignore-errors: true }
|
||||||
|
- { python-version: "3.9", tox-env: py39-no-ext, platform: windows-latest, ignore-errors: true }
|
||||||
|
- { python-version: "3.10", tox-env: py310-no-ext, platform: windows-latest, ignore-errors: true }
|
||||||
|
- { python-version: "3.11", tox-env: py310-no-ext, platform: windows-latest, ignore-errors: true }
|
||||||
|
steps:
|
||||||
|
- name: Run tests
|
||||||
|
uses: sanic-org/simple-tox-action@v1
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.config.python-version }}
|
||||||
|
tox-env: ${{ matrix.config.tox-env }}
|
||||||
|
max-attempts: ${{ matrix.config.max-attempts || 1 }}
|
||||||
|
ignore-errors: ${{ matrix.config.ignore-errors || false }}
|
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -6,6 +6,7 @@
|
||||||
.coverage
|
.coverage
|
||||||
.coverage.*
|
.coverage.*
|
||||||
coverage
|
coverage
|
||||||
|
coverage.xml
|
||||||
.tox
|
.tox
|
||||||
settings.py
|
settings.py
|
||||||
.idea/*
|
.idea/*
|
||||||
|
@ -18,3 +19,8 @@ build/*
|
||||||
.DS_Store
|
.DS_Store
|
||||||
dist/*
|
dist/*
|
||||||
pip-wheel-metadata/
|
pip-wheel-metadata/
|
||||||
|
.pytest_cache/*
|
||||||
|
.venv/*
|
||||||
|
venv/*
|
||||||
|
.vscode/*
|
||||||
|
guide/node_modules/
|
||||||
|
|
114
.travis.yml
114
.travis.yml
|
@ -1,114 +0,0 @@
|
||||||
sudo: false
|
|
||||||
language: python
|
|
||||||
cache:
|
|
||||||
directories:
|
|
||||||
- $HOME/.cache/pip
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- env: TOX_ENV=py36
|
|
||||||
python: 3.6
|
|
||||||
name: "Python 3.6 with Extensions"
|
|
||||||
- env: TOX_ENV=py36-no-ext
|
|
||||||
python: 3.6
|
|
||||||
name: "Python 3.6 without Extensions"
|
|
||||||
- env: TOX_ENV=py37
|
|
||||||
python: 3.7
|
|
||||||
dist: xenial
|
|
||||||
sudo: true
|
|
||||||
name: "Python 3.7 with Extensions"
|
|
||||||
- env: TOX_ENV=py37-no-ext
|
|
||||||
python: 3.7
|
|
||||||
dist: xenial
|
|
||||||
sudo: true
|
|
||||||
name: "Python 3.7 without Extensions"
|
|
||||||
- env: TOX_ENV=py38
|
|
||||||
python: 3.8
|
|
||||||
dist: xenial
|
|
||||||
sudo: true
|
|
||||||
name: "Python 3.8 with Extensions"
|
|
||||||
- env: TOX_ENV=py38-no-ext
|
|
||||||
python: 3.8
|
|
||||||
dist: xenial
|
|
||||||
sudo: true
|
|
||||||
name: "Python 3.8 without Extensions"
|
|
||||||
- env: TOX_ENV=py39
|
|
||||||
python: 3.9
|
|
||||||
dist: bionic
|
|
||||||
sudo: true
|
|
||||||
name: "Python 3.9 with Extensions"
|
|
||||||
- env: TOX_ENV=py39-no-ext
|
|
||||||
python: 3.9
|
|
||||||
dist: bionic
|
|
||||||
sudo: true
|
|
||||||
name: "Python 3.9 without Extensions"
|
|
||||||
- env: TOX_ENV=type-checking
|
|
||||||
python: 3.6
|
|
||||||
name: "Python 3.6 Type checks"
|
|
||||||
- env: TOX_ENV=type-checking
|
|
||||||
python: 3.7
|
|
||||||
name: "Python 3.7 Type checks"
|
|
||||||
- env: TOX_ENV=type-checking
|
|
||||||
python: 3.8
|
|
||||||
name: "Python 3.8 Type checks"
|
|
||||||
- env: TOX_ENV=type-checking
|
|
||||||
python: 3.9
|
|
||||||
dist: bionic
|
|
||||||
name: "Python 3.9 Type checks"
|
|
||||||
- env: TOX_ENV=lint
|
|
||||||
python: 3.6
|
|
||||||
name: "Python 3.6 Linter checks"
|
|
||||||
- env: TOX_ENV=check
|
|
||||||
python: 3.6
|
|
||||||
name: "Python 3.6 Package checks"
|
|
||||||
- env: TOX_ENV=security
|
|
||||||
python: 3.6
|
|
||||||
dist: xenial
|
|
||||||
sudo: true
|
|
||||||
name: "Python 3.6 Bandit security scan"
|
|
||||||
- env: TOX_ENV=security
|
|
||||||
python: 3.7
|
|
||||||
dist: xenial
|
|
||||||
sudo: true
|
|
||||||
name: "Python 3.7 Bandit security scan"
|
|
||||||
- env: TOX_ENV=security
|
|
||||||
python: 3.8
|
|
||||||
dist: xenial
|
|
||||||
sudo: true
|
|
||||||
name: "Python 3.8 Bandit security scan"
|
|
||||||
- env: TOX_ENV=security
|
|
||||||
python: 3.9
|
|
||||||
dist: bionic
|
|
||||||
sudo: true
|
|
||||||
name: "Python 3.9 Bandit security scan"
|
|
||||||
- env: TOX_ENV=docs
|
|
||||||
python: 3.7
|
|
||||||
dist: xenial
|
|
||||||
sudo: true
|
|
||||||
name: "Python 3.7 Documentation tests"
|
|
||||||
- env: TOX_ENV=pyNightly
|
|
||||||
python: "nightly"
|
|
||||||
name: "Python nightly with Extensions"
|
|
||||||
- env: TOX_ENV=pyNightly-no-ext
|
|
||||||
python: "nightly"
|
|
||||||
name: "Python nightly without Extensions"
|
|
||||||
allow_failures:
|
|
||||||
- env: TOX_ENV=pyNightly
|
|
||||||
python: "nightly"
|
|
||||||
name: "Python nightly with Extensions"
|
|
||||||
- env: TOX_ENV=pyNightly-no-ext
|
|
||||||
python: "nightly"
|
|
||||||
name: "Python nightly without Extensions"
|
|
||||||
install:
|
|
||||||
- pip install -U tox
|
|
||||||
- pip install codecov
|
|
||||||
script: travis_retry tox -e $TOX_ENV
|
|
||||||
after_success:
|
|
||||||
- codecov
|
|
||||||
deploy:
|
|
||||||
provider: pypi
|
|
||||||
user: brewmaster
|
|
||||||
password:
|
|
||||||
secure: "GoawLwmbtJOgKB6AJ0ZSYUUnNwIoonseHBxaAUH3zu79TS/Afrq+yB3lsVaMSG0CbyDgN4FrfD1phT1NzbvZ1VcLIOTDtCrmpQ1kLDw+zwgF40ab8sp8fPkKVHHHfCCs1mjltHIpxQa5lZTJcAs6Bpi/lbUWWwYxFzSV8pHw4W4hY09EHUd2o+evLTSVxaploetSt725DJUYKICUr2eAtCC11IDnIW4CzBJEx6krVV3uhzfTJW0Ls17x0c6sdZ9icMnV/G9xO/eQH6RIHe4xcrWJ6cmLDNKoGAkJp+BKr1CeVVg7Jw/MzPjvZKL2/ki6Beue1y6GUIy7lOS7jPVaOEhJ23b0zQwFcLMZw+Tt+E3v6QfHk+B/WBBBnM3zUZed9UI+QyW8+lqLLt39sQX0FO0P3eaDh8qTXtUuon2jTyFMMAMTFRTNpJmpAzuBH9yeMmDeALPTh0HphI+BkoUl5q1QbWFYjjnZMH2CatApxpLybt9A7rwm//PbOG0TSI93GEKNQ4w5DYryKTfwHzRBptNSephJSuxZYEfJsmUtas5es1D7Fe0PkyjxNNSU+eO+8wsTlitLUsJO4k0jAgy+cEKdU7YJ3J0GZVXocSkrNnUfd2hQPcJ3UtEJx3hLqqr8EM7EZBAasc1yGHh36NFetclzFY24YPih0G1+XurhTys="
|
|
||||||
on:
|
|
||||||
tags: true
|
|
||||||
distributions: "sdist bdist_wheel"
|
|
826
CHANGELOG.rst
826
CHANGELOG.rst
File diff suppressed because it is too large
Load Diff
|
@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
|
||||||
## Enforcement
|
## Enforcement
|
||||||
|
|
||||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||||
reported by contacting the project team at sanic-maintainers@googlegroups.com. All
|
reported by contacting the project team at adam@sanicframework.org. All
|
||||||
complaints will be reviewed and investigated and will result in a response that
|
complaints will be reviewed and investigated and will result in a response that
|
||||||
is deemed necessary and appropriate to the circumstances. The project team is
|
is deemed necessary and appropriate to the circumstances. The project team is
|
||||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
obligated to maintain confidentiality with regard to the reporter of an incident.
|
108
CONTRIBUTING.rst
108
CONTRIBUTING.rst
|
@ -1,20 +1,17 @@
|
||||||
|
|
||||||
Contributing
|
|
||||||
============
|
|
||||||
|
|
||||||
Thank you for your interest! Sanic is always looking for contributors. If you
|
Thank you for your interest! Sanic is always looking for contributors. If you
|
||||||
don't feel comfortable contributing code, adding docstrings to the source files
|
don't feel comfortable contributing code, adding docstrings to the source files,
|
||||||
is very appreciated.
|
or helping with the `Sanic User Guide <https://github.com/sanic-org/sanic-guide>`_
|
||||||
|
by providing documentation or implementation examples would be appreciated!
|
||||||
|
|
||||||
We are committed to providing a friendly, safe and welcoming environment for all,
|
We are committed to providing a friendly, safe and welcoming environment for all,
|
||||||
regardless of gender, sexual orientation, disability, ethnicity, religion,
|
regardless of gender, sexual orientation, disability, ethnicity, religion,
|
||||||
or similar personal characteristic.
|
or similar personal characteristic.
|
||||||
Our `code of conduct <./CONDUCT.md>`_ sets the standards for behavior.
|
Our `code of conduct <https://github.com/sanic-org/sanic/blob/master/CONDUCT.md>`_ sets the standards for behavior.
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
|
||||||
To develop on sanic (and mainly to just run the tests) it is highly recommend to
|
To develop on Sanic (and mainly to just run the tests) it is highly recommend to
|
||||||
install from sources.
|
install from sources.
|
||||||
|
|
||||||
So assume you have already cloned the repo and are in the working directory with
|
So assume you have already cloned the repo and are in the working directory with
|
||||||
|
@ -22,7 +19,7 @@ a virtual environment already set up, then run:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
pip3 install -e . ".[dev]"
|
pip install -e ".[dev]"
|
||||||
|
|
||||||
Dependency Changes
|
Dependency Changes
|
||||||
------------------
|
------------------
|
||||||
|
@ -74,9 +71,9 @@ To execute only unittests, run ``tox`` with environment like so:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
tox -e py36 -v -- tests/test_config.py
|
|
||||||
# or
|
|
||||||
tox -e py37 -v -- tests/test_config.py
|
tox -e py37 -v -- tests/test_config.py
|
||||||
|
# or
|
||||||
|
tox -e py310 -v -- tests/test_config.py
|
||||||
|
|
||||||
Run lint checks
|
Run lint checks
|
||||||
---------------
|
---------------
|
||||||
|
@ -89,6 +86,17 @@ Permform ``flake8``\ , ``black`` and ``isort`` checks.
|
||||||
|
|
||||||
tox -e lint
|
tox -e lint
|
||||||
|
|
||||||
|
Run type annotation checks
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
``tox`` environment -> ``[testenv:type-checking]``
|
||||||
|
|
||||||
|
Permform ``mypy`` checks.
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
tox -e type-checking
|
||||||
|
|
||||||
Run other checks
|
Run other checks
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
@ -132,6 +140,7 @@ To maintain the code consistency, Sanic uses following tools.
|
||||||
#. `isort <https://github.com/timothycrosley/isort>`_
|
#. `isort <https://github.com/timothycrosley/isort>`_
|
||||||
#. `black <https://github.com/python/black>`_
|
#. `black <https://github.com/python/black>`_
|
||||||
#. `flake8 <https://github.com/PyCQA/flake8>`_
|
#. `flake8 <https://github.com/PyCQA/flake8>`_
|
||||||
|
#. `slotscheck <https://github.com/ariebovenberg/slotscheck>`_
|
||||||
|
|
||||||
isort
|
isort
|
||||||
*****
|
*****
|
||||||
|
@ -159,7 +168,19 @@ flake8
|
||||||
#. pycodestyle
|
#. pycodestyle
|
||||||
#. Ned Batchelder's McCabe script
|
#. Ned Batchelder's McCabe script
|
||||||
|
|
||||||
``isort``\ , ``black`` and ``flake8`` checks are performed during ``tox`` lint checks.
|
slotscheck
|
||||||
|
**********
|
||||||
|
|
||||||
|
``slotscheck`` ensures that there are no problems with ``__slots__``
|
||||||
|
(e.g. overlaps, or missing slots in base classes).
|
||||||
|
|
||||||
|
``isort``\ , ``black``\ , ``flake8`` and ``slotscheck`` checks are performed during ``tox`` lint checks.
|
||||||
|
|
||||||
|
The **easiest** way to make your code conform is to run the following before committing.
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
make pretty
|
||||||
|
|
||||||
Refer `tox <https://tox.readthedocs.io/en/latest/index.html>`_ documentation for more details.
|
Refer `tox <https://tox.readthedocs.io/en/latest/index.html>`_ documentation for more details.
|
||||||
|
|
||||||
|
@ -168,12 +189,13 @@ Pull requests
|
||||||
|
|
||||||
So the pull request approval rules are pretty simple:
|
So the pull request approval rules are pretty simple:
|
||||||
|
|
||||||
#. All pull requests must have a changelog details associated with it.
|
|
||||||
#. All pull requests must pass unit tests.
|
#. All pull requests must pass unit tests.
|
||||||
#. All pull requests must be reviewed and approved by at least one current collaborator on the project.
|
#. All pull requests must be reviewed and approved by at least one current member of the Core Developer team.
|
||||||
#. All pull requests must pass flake8 checks.
|
#. All pull requests must pass flake8 checks.
|
||||||
|
#. All pull requests must match ``isort`` and ``black`` requirements.
|
||||||
|
#. All pull requests must be **PROPERLY** type annotated, unless exemption is given.
|
||||||
#. All pull requests must be consistent with the existing code.
|
#. All pull requests must be consistent with the existing code.
|
||||||
#. If you decide to remove/change anything from any common interface a deprecation message should accompany it.
|
#. If you decide to remove/change anything from any common interface a deprecation message should accompany it in accordance with our `deprecation policy <https://sanicframework.org/en/guide/project/policies.html#deprecation>`_.
|
||||||
#. If you implement a new feature you should have at least one unit test to accompany it.
|
#. If you implement a new feature you should have at least one unit test to accompany it.
|
||||||
#. An example must be one of the following:
|
#. An example must be one of the following:
|
||||||
|
|
||||||
|
@ -182,56 +204,14 @@ So the pull request approval rules are pretty simple:
|
||||||
* Example of how to use Sanic and asynchronous library
|
* Example of how to use Sanic and asynchronous library
|
||||||
|
|
||||||
|
|
||||||
Changelog
|
|
||||||
---------
|
|
||||||
|
|
||||||
It is mandatory to add documentation for Change log as part of your Pull request when you fix/contribute something
|
|
||||||
to the ``sanic`` community. This will enable us in generating better and well defined change logs during the
|
|
||||||
release which can aid community users in a great way.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
Single line explaining the details of the PR in brief
|
|
||||||
|
|
||||||
Detailed description of what the PR is about and what changes or enhancements are being done.
|
|
||||||
No need to include examples or any other details here. But it is important that you provide
|
|
||||||
enough context here to let user understand what this change is all about and why it is being
|
|
||||||
introduced into the ``sanic`` codebase.
|
|
||||||
|
|
||||||
Make sure you leave an line space after the first line to make sure the document rendering is clean
|
|
||||||
|
|
||||||
|
|
||||||
.. list-table::
|
|
||||||
:header-rows: 1
|
|
||||||
|
|
||||||
* - Contribution Type
|
|
||||||
- Changelog file name format
|
|
||||||
- Changelog file location
|
|
||||||
* - Features
|
|
||||||
- <git_issue>.feature.rst
|
|
||||||
- ``changelogs``
|
|
||||||
* - Bugfixes
|
|
||||||
- <git_issue>.bugfix.rst
|
|
||||||
- ``changelogs``
|
|
||||||
* - Improved Documentation
|
|
||||||
- <git_issue>.doc.rst
|
|
||||||
- ``changelogs``
|
|
||||||
* - Deprecations and Removals
|
|
||||||
- <git_issue>.removal.rst
|
|
||||||
- ``changelogs``
|
|
||||||
* - Miscellaneous internal changes
|
|
||||||
- <git_issue>.misc.rst
|
|
||||||
- ``changelogs``
|
|
||||||
|
|
||||||
|
|
||||||
Documentation
|
Documentation
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
Sanic's documentation is built
|
Sanic's API documentation is built using `sphinx <http://www.sphinx-doc.org/en/1.5.1/>`_ with module references
|
||||||
using `sphinx <http://www.sphinx-doc.org/en/1.5.1/>`_. Guides are written in
|
|
||||||
Markdown and can be found in the ``docs`` folder, while the module reference is
|
|
||||||
automatically generated using ``sphinx-apidoc``.
|
automatically generated using ``sphinx-apidoc``.
|
||||||
|
|
||||||
|
The User Guide is in the `sanic-guide <https://github.com/sanic-org/sanic-guide>`_ repository.
|
||||||
|
|
||||||
To generate the documentation from scratch:
|
To generate the documentation from scratch:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
@ -245,6 +225,14 @@ To generate the documentation from scratch:
|
||||||
|
|
||||||
The HTML documentation will be created in the ``docs/_build`` folder.
|
The HTML documentation will be created in the ``docs/_build`` folder.
|
||||||
|
|
||||||
|
You can run the following to have a live development server with the API documents
|
||||||
|
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
make docs-serve
|
||||||
|
|
||||||
|
Refer to the User Guide repo for documentation on how to contribute there.
|
||||||
|
|
||||||
.. warning::
|
.. warning::
|
||||||
One of the main goals of Sanic is speed. Code that lowers the performance of
|
One of the main goals of Sanic is speed. Code that lowers the performance of
|
||||||
Sanic without significant gains in usability, security, or features may not be
|
Sanic without significant gains in usability, security, or features may not be
|
||||||
|
|
14
Makefile
14
Makefile
|
@ -49,6 +49,9 @@ test: clean
|
||||||
test-coverage: clean
|
test-coverage: clean
|
||||||
python setup.py test --pytest-args="--cov sanic --cov-report term --cov-append "
|
python setup.py test --pytest-args="--cov sanic --cov-report term --cov-append "
|
||||||
|
|
||||||
|
view-coverage:
|
||||||
|
sanic ./coverage --simple
|
||||||
|
|
||||||
install:
|
install:
|
||||||
python setup.py install
|
python setup.py install
|
||||||
|
|
||||||
|
@ -68,11 +71,12 @@ endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
black:
|
black:
|
||||||
black --config ./.black.toml sanic tests
|
black sanic tests
|
||||||
|
|
||||||
fix-import: black
|
isort:
|
||||||
isort sanic tests
|
isort sanic tests
|
||||||
|
|
||||||
|
pretty: black isort
|
||||||
|
|
||||||
docs-clean:
|
docs-clean:
|
||||||
cd docs && make clean
|
cd docs && make clean
|
||||||
|
@ -83,9 +87,15 @@ docs: docs-clean
|
||||||
docs-test: docs-clean
|
docs-test: docs-clean
|
||||||
cd docs && make dummy
|
cd docs && make dummy
|
||||||
|
|
||||||
|
docs-serve:
|
||||||
|
sphinx-autobuild docs docs/_build/html --port 9999 --watch ./
|
||||||
|
|
||||||
changelog:
|
changelog:
|
||||||
python scripts/changelog.py
|
python scripts/changelog.py
|
||||||
|
|
||||||
|
guide-serve:
|
||||||
|
cd guide && sanic server:app -r -R ./content -R ./style
|
||||||
|
|
||||||
release:
|
release:
|
||||||
ifdef version
|
ifdef version
|
||||||
python scripts/release.py --release-version ${version} --generate-changelog
|
python scripts/release.py --release-version ${version} --generate-changelog
|
||||||
|
|
67
README.rst
67
README.rst
|
@ -1,4 +1,4 @@
|
||||||
.. image:: https://raw.githubusercontent.com/huge-success/sanic-assets/master/png/sanic-framework-logo-400x97.png
|
.. image:: https://raw.githubusercontent.com/sanic-org/sanic-assets/master/png/sanic-framework-logo-400x97.png
|
||||||
:alt: Sanic | Build fast. Run fast.
|
:alt: Sanic | Build fast. Run fast.
|
||||||
|
|
||||||
Sanic | Build fast. Run fast.
|
Sanic | Build fast. Run fast.
|
||||||
|
@ -7,29 +7,28 @@ Sanic | Build fast. Run fast.
|
||||||
.. start-badges
|
.. start-badges
|
||||||
|
|
||||||
.. list-table::
|
.. list-table::
|
||||||
|
:widths: 15 85
|
||||||
:stub-columns: 1
|
:stub-columns: 1
|
||||||
|
|
||||||
* - Build
|
* - Build
|
||||||
- | |Build Status| |AppVeyor Build Status| |Codecov|
|
- | |Tests|
|
||||||
* - Docs
|
* - Docs
|
||||||
- |Documentation|
|
- | |UserGuide| |Documentation|
|
||||||
* - Package
|
* - Package
|
||||||
- | |PyPI| |PyPI version| |Wheel| |Supported implementations| |Code style black|
|
- | |PyPI| |PyPI version| |Wheel| |Supported implementations| |Code style black|
|
||||||
* - Support
|
* - Support
|
||||||
- | |Forums| |Join the chat at https://gitter.im/sanic-python/Lobby| |Awesome|
|
- | |Forums| |Discord| |Awesome|
|
||||||
* - Stats
|
* - Stats
|
||||||
- | |Downloads| |Conda downloads|
|
- | |Monthly Downloads| |Weekly Downloads| |Conda downloads|
|
||||||
|
|
||||||
|
.. |UserGuide| image:: https://img.shields.io/badge/user%20guide-sanic-ff0068
|
||||||
|
:target: https://sanicframework.org/
|
||||||
.. |Forums| image:: https://img.shields.io/badge/forums-community-ff0068.svg
|
.. |Forums| image:: https://img.shields.io/badge/forums-community-ff0068.svg
|
||||||
:target: https://community.sanicframework.org/
|
:target: https://community.sanicframework.org/
|
||||||
.. |Join the chat at https://gitter.im/sanic-python/Lobby| image:: https://badges.gitter.im/sanic-python/Lobby.svg
|
.. |Discord| image:: https://img.shields.io/discord/812221182594121728?logo=discord
|
||||||
:target: https://gitter.im/sanic-python/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
|
:target: https://discord.gg/FARQzAEMAA
|
||||||
.. |Codecov| image:: https://codecov.io/gh/huge-success/sanic/branch/master/graph/badge.svg
|
.. |Tests| image:: https://github.com/sanic-org/sanic/actions/workflows/tests.yml/badge.svg?branch=main
|
||||||
:target: https://codecov.io/gh/huge-success/sanic
|
:target: https://github.com/sanic-org/sanic/actions/workflows/tests.yml
|
||||||
.. |Build Status| image:: https://travis-ci.com/huge-success/sanic.svg?branch=master
|
|
||||||
:target: https://travis-ci.com/huge-success/sanic
|
|
||||||
.. |AppVeyor Build Status| image:: https://ci.appveyor.com/api/projects/status/d8pt3ids0ynexi8c/branch/master?svg=true
|
|
||||||
:target: https://ci.appveyor.com/project/huge-success/sanic
|
|
||||||
.. |Documentation| image:: https://readthedocs.org/projects/sanic/badge/?version=latest
|
.. |Documentation| image:: https://readthedocs.org/projects/sanic/badge/?version=latest
|
||||||
:target: http://sanic.readthedocs.io/en/latest/?badge=latest
|
:target: http://sanic.readthedocs.io/en/latest/?badge=latest
|
||||||
.. |PyPI| image:: https://img.shields.io/pypi/v/sanic.svg
|
.. |PyPI| image:: https://img.shields.io/pypi/v/sanic.svg
|
||||||
|
@ -47,25 +46,41 @@ Sanic | Build fast. Run fast.
|
||||||
.. |Awesome| image:: https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg
|
.. |Awesome| image:: https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg
|
||||||
:alt: Awesome Sanic List
|
:alt: Awesome Sanic List
|
||||||
:target: https://github.com/mekicha/awesome-sanic
|
:target: https://github.com/mekicha/awesome-sanic
|
||||||
.. |Downloads| image:: https://pepy.tech/badge/sanic/month
|
.. |Monthly Downloads| image:: https://img.shields.io/pypi/dm/sanic.svg
|
||||||
|
:alt: Downloads
|
||||||
|
:target: https://pepy.tech/project/sanic
|
||||||
|
.. |Weekly Downloads| image:: https://img.shields.io/pypi/dw/sanic.svg
|
||||||
:alt: Downloads
|
:alt: Downloads
|
||||||
:target: https://pepy.tech/project/sanic
|
:target: https://pepy.tech/project/sanic
|
||||||
.. |Conda downloads| image:: https://img.shields.io/conda/dn/conda-forge/sanic.svg
|
.. |Conda downloads| image:: https://img.shields.io/conda/dn/conda-forge/sanic.svg
|
||||||
:alt: Downloads
|
:alt: Downloads
|
||||||
:target: https://anaconda.org/conda-forge/sanic
|
:target: https://anaconda.org/conda-forge/sanic
|
||||||
|
.. |Linode| image:: https://www.linode.com/wp-content/uploads/2021/01/Linode-Logo-Black.svg
|
||||||
|
:alt: Linode
|
||||||
|
:target: https://www.linode.com
|
||||||
|
:width: 200px
|
||||||
|
|
||||||
.. end-badges
|
.. end-badges
|
||||||
|
|
||||||
Sanic is a **Python 3.6+** web server and web framework that's written to go fast. It allows the usage of the ``async/await`` syntax added in Python 3.5, which makes your code non-blocking and speedy.
|
Sanic is a **Python 3.8+** web server and web framework that's written to go fast. It allows the usage of the ``async/await`` syntax added in Python 3.5, which makes your code non-blocking and speedy.
|
||||||
|
|
||||||
Sanic is also ASGI compliant, so you can deploy it with an `alternative ASGI webserver <https://sanic.readthedocs.io/en/latest/sanic/deploying.html#running-via-asgi>`_.
|
Sanic is also ASGI compliant, so you can deploy it with an `alternative ASGI webserver <https://sanicframework.org/en/guide/deployment/running.html#asgi>`_.
|
||||||
|
|
||||||
`Source code on GitHub <https://github.com/huge-success/sanic/>`_ | `Help and discussion board <https://community.sanicframework.org/>`_.
|
`Source code on GitHub <https://github.com/sanic-org/sanic/>`_ | `Help and discussion board <https://community.sanicframework.org/>`_ | `User Guide <https://sanicframework.org>`_ | `Chat on Discord <https://discord.gg/FARQzAEMAA>`_
|
||||||
|
|
||||||
The project is maintained by the community, for the community. **Contributions are welcome!**
|
The project is maintained by the community, for the community. **Contributions are welcome!**
|
||||||
|
|
||||||
The goal of the project is to provide a simple way to get up and running a highly performant HTTP server that is easy to build, to expand, and ultimately to scale.
|
The goal of the project is to provide a simple way to get up and running a highly performant HTTP server that is easy to build, to expand, and ultimately to scale.
|
||||||
|
|
||||||
|
Sponsor
|
||||||
|
-------
|
||||||
|
|
||||||
|
Check out `open collective <https://opencollective.com/sanic-org>`_ to learn more about helping to fund Sanic.
|
||||||
|
|
||||||
|
Thanks to `Linode <https://www.linode.com>`_ for their contribution towards the development and community of Sanic.
|
||||||
|
|
||||||
|
|Linode|
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
@ -85,9 +100,6 @@ Installation
|
||||||
If you are running on a clean install of Fedora 28 or above, please make sure you have the ``redhat-rpm-config`` package installed in case if you want to
|
If you are running on a clean install of Fedora 28 or above, please make sure you have the ``redhat-rpm-config`` package installed in case if you want to
|
||||||
use ``sanic`` with ``ujson`` dependency.
|
use ``sanic`` with ``ujson`` dependency.
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
Windows support is currently "experimental" and on a best-effort basis. Multiple workers are also not currently supported on Windows (see `Issue #1517 <https://github.com/huge-success/sanic/issues/1517>`_), but setting ``workers=1`` should launch the server successfully.
|
|
||||||
|
|
||||||
Hello World Example
|
Hello World Example
|
||||||
-------------------
|
-------------------
|
||||||
|
@ -97,20 +109,20 @@ Hello World Example
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.response import json
|
from sanic.response import json
|
||||||
|
|
||||||
app = Sanic()
|
app = Sanic("my-hello-world-app")
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
async def test(request):
|
async def test(request):
|
||||||
return json({'hello': 'world'})
|
return json({'hello': 'world'})
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', port=8000)
|
app.run()
|
||||||
|
|
||||||
Sanic can now be easily run using ``sanic hello.app``.
|
Sanic can now be easily run using ``sanic hello.app``.
|
||||||
|
|
||||||
.. code::
|
.. code::
|
||||||
|
|
||||||
[2018-12-30 11:37:41 +0200] [13564] [INFO] Goin' Fast @ http://0.0.0.0:8000
|
[2018-12-30 11:37:41 +0200] [13564] [INFO] Goin' Fast @ http://127.0.0.1:8000
|
||||||
[2018-12-30 11:37:41 +0200] [13564] [INFO] Starting worker [13564]
|
[2018-12-30 11:37:41 +0200] [13564] [INFO] Starting worker [13564]
|
||||||
|
|
||||||
And, we can verify it is working: ``curl localhost:8000 -i``
|
And, we can verify it is working: ``curl localhost:8000 -i``
|
||||||
|
@ -127,24 +139,25 @@ And, we can verify it is working: ``curl localhost:8000 -i``
|
||||||
|
|
||||||
**Now, let's go build something fast!**
|
**Now, let's go build something fast!**
|
||||||
|
|
||||||
|
Minimum Python version is 3.8. If you need Python 3.7 support, please use v22.12LTS.
|
||||||
|
|
||||||
Documentation
|
Documentation
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
`Documentation on Readthedocs <http://sanic.readthedocs.io/>`_.
|
`User Guide <https://sanic.dev>`__ and `API Documentation <http://sanic.readthedocs.io/>`__.
|
||||||
|
|
||||||
Changelog
|
Changelog
|
||||||
---------
|
---------
|
||||||
|
|
||||||
`Release Changelogs <https://github.com/huge-success/sanic/blob/master/CHANGELOG.rst>`_.
|
`Release Changelogs <https://sanic.readthedocs.io/en/stable/sanic/changelog.html>`__.
|
||||||
|
|
||||||
|
|
||||||
Questions and Discussion
|
Questions and Discussion
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
`Ask a question or join the conversation <https://community.sanicframework.org/>`_.
|
`Ask a question or join the conversation <https://community.sanicframework.org/>`__.
|
||||||
|
|
||||||
Contribution
|
Contribution
|
||||||
------------
|
------------
|
||||||
|
|
||||||
We are always happy to have new contributions. We have `marked issues good for anyone looking to get started <https://github.com/huge-success/sanic/issues?q=is%3Aopen+is%3Aissue+label%3Abeginner>`_, and welcome `questions on the forums <https://community.sanicframework.org/>`_. Please take a look at our `Contribution guidelines <https://sanic.readthedocs.io/en/latest/sanic/contributing.html>`_.
|
We are always happy to have new contributions. We have `marked issues good for anyone looking to get started <https://github.com/sanic-org/sanic/issues?q=is%3Aopen+is%3Aissue+label%3Abeginner>`_, and welcome `questions on the forums <https://community.sanicframework.org/>`_. Please take a look at our `Contribution guidelines <https://github.com/sanic-org/sanic/blob/master/CONTRIBUTING.rst>`_.
|
||||||
|
|
24
SECURITY.md
24
SECURITY.md
|
@ -4,16 +4,26 @@
|
||||||
|
|
||||||
Sanic releases long term support release once a year in December. LTS releases receive bug and security updates for **24 months**. Interim releases throughout the year occur every three months, and are supported until the subsequent interim release.
|
Sanic releases long term support release once a year in December. LTS releases receive bug and security updates for **24 months**. Interim releases throughout the year occur every three months, and are supported until the subsequent interim release.
|
||||||
|
|
||||||
|
|
||||||
| Version | LTS | Supported |
|
| Version | LTS | Supported |
|
||||||
| ------- | ------------- | ------------------ |
|
| ------- | ------------- | ----------------------- |
|
||||||
| 20.9 | | :heavy_check_mark: |
|
| 22.12 | until 2024-12 | :white_check_mark: |
|
||||||
|
| 22.9 | | :x: |
|
||||||
|
| 22.6 | | :x: |
|
||||||
|
| 22.3 | | :x: |
|
||||||
|
| 21.12 | until 2023-12 | :ballot_box_with_check: |
|
||||||
|
| 21.9 | | :x: |
|
||||||
|
| 21.6 | | :x: |
|
||||||
|
| 21.3 | | :x: |
|
||||||
|
| 20.12 | | :x: |
|
||||||
|
| 20.9 | | :x: |
|
||||||
| 20.6 | | :x: |
|
| 20.6 | | :x: |
|
||||||
| 20.3 | | :x: |
|
| 20.3 | | :x: |
|
||||||
| 19.12 | until 2021-12 | :white_check_mark: |
|
| 19.12 | | :x: |
|
||||||
| 19.9 | | :x: |
|
| 19.9 | | :x: |
|
||||||
| 19.6 | | :x: |
|
| 19.6 | | :x: |
|
||||||
| 19.3 | | :x: |
|
| 19.3 | | :x: |
|
||||||
| 18.12 | until 2020-12 | :white_check_mark: |
|
| 18.12 | | :x: |
|
||||||
| 0.8.3 | | :x: |
|
| 0.8.3 | | :x: |
|
||||||
| 0.7.0 | | :x: |
|
| 0.7.0 | | :x: |
|
||||||
| 0.6.0 | | :x: |
|
| 0.6.0 | | :x: |
|
||||||
|
@ -23,11 +33,13 @@ Sanic releases long term support release once a year in December. LTS releases r
|
||||||
| 0.2.0 | | :x: |
|
| 0.2.0 | | :x: |
|
||||||
| 0.1.9 | | :x: |
|
| 0.1.9 | | :x: |
|
||||||
|
|
||||||
:white_check_mark: = security/bug fixes
|
:ballot_box_with_check: = security/bug fixes
|
||||||
:heavy_check_mark: = full support
|
:white_check_mark: = full support
|
||||||
|
|
||||||
## Reporting a Vulnerability
|
## Reporting a Vulnerability
|
||||||
|
|
||||||
If you discover a security vulnerability, we ask that you **do not** create an issue on GitHub. Instead, please [send a message to the core-devs](https://community.sanicframework.org/g/core-devs) on the community forums. Once logged in, you can send a message to the core-devs by clicking the message button.
|
If you discover a security vulnerability, we ask that you **do not** create an issue on GitHub. Instead, please [send a message to the core-devs](https://community.sanicframework.org/g/core-devs) on the community forums. Once logged in, you can send a message to the core-devs by clicking the message button.
|
||||||
|
|
||||||
|
Alternatively, you can send a private message to Adam Hopkins on Discord. Find him on the [Sanic discord server](https://discord.gg/FARQzAEMAA).
|
||||||
|
|
||||||
This will help to not publicize the issue until the team can address it and resolve it.
|
This will help to not publicize the issue until the team can address it and resolve it.
|
||||||
|
|
28
codecov.yml
Normal file
28
codecov.yml
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
coverage:
|
||||||
|
status:
|
||||||
|
patch:
|
||||||
|
default:
|
||||||
|
target: auto
|
||||||
|
threshold: 0.75
|
||||||
|
informational: true
|
||||||
|
project:
|
||||||
|
default:
|
||||||
|
target: auto
|
||||||
|
threshold: 0.5
|
||||||
|
precision: 3
|
||||||
|
codecov:
|
||||||
|
require_ci_to_pass: false
|
||||||
|
ignore:
|
||||||
|
- "sanic/__main__.py"
|
||||||
|
- "sanic/compat.py"
|
||||||
|
- "sanic/simple.py"
|
||||||
|
- "sanic/utils.py"
|
||||||
|
- "sanic/cli/"
|
||||||
|
- "sanic/pages/"
|
||||||
|
- ".github/"
|
||||||
|
- "changelogs/"
|
||||||
|
- "docker/"
|
||||||
|
- "docs/"
|
||||||
|
- "examples/"
|
||||||
|
- "scripts/"
|
||||||
|
- "tests/"
|
|
@ -1,28 +1,13 @@
|
||||||
FROM alpine:3.7
|
ARG BASE_IMAGE_ORG
|
||||||
|
ARG BASE_IMAGE_NAME
|
||||||
|
ARG BASE_IMAGE_TAG
|
||||||
|
|
||||||
RUN apk add --no-cache --update \
|
FROM ${BASE_IMAGE_ORG}/${BASE_IMAGE_NAME}:${BASE_IMAGE_TAG}
|
||||||
curl \
|
|
||||||
bash \
|
|
||||||
build-base \
|
|
||||||
ca-certificates \
|
|
||||||
git \
|
|
||||||
bzip2-dev \
|
|
||||||
linux-headers \
|
|
||||||
ncurses-dev \
|
|
||||||
openssl \
|
|
||||||
openssl-dev \
|
|
||||||
readline-dev \
|
|
||||||
sqlite-dev
|
|
||||||
|
|
||||||
|
RUN apk update
|
||||||
RUN update-ca-certificates
|
RUN update-ca-certificates
|
||||||
RUN rm -rf /var/cache/apk/*
|
|
||||||
|
|
||||||
ENV PYENV_ROOT="/root/.pyenv"
|
ARG SANIC_PYPI_VERSION
|
||||||
ENV PATH="$PYENV_ROOT/bin:$PATH"
|
|
||||||
|
|
||||||
ADD . /app
|
RUN pip install -U pip && pip install sanic==${SANIC_PYPI_VERSION}
|
||||||
WORKDIR /app
|
RUN apk del build-base
|
||||||
|
|
||||||
RUN /app/docker/bin/install_python.sh 3.5.4 3.6.4
|
|
||||||
|
|
||||||
ENTRYPOINT ["./docker/bin/entrypoint.sh"]
|
|
||||||
|
|
9
docker/Dockerfile-base
Normal file
9
docker/Dockerfile-base
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
ARG PYTHON_VERSION
|
||||||
|
|
||||||
|
FROM python:${PYTHON_VERSION}-alpine
|
||||||
|
RUN apk update
|
||||||
|
RUN apk add --no-cache --update build-base \
|
||||||
|
ca-certificates \
|
||||||
|
openssl
|
||||||
|
RUN update-ca-certificates
|
||||||
|
RUN rm -rf /var/cache/apk/*
|
|
@ -1,11 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
|
|
||||||
eval "$(pyenv init -)"
|
|
||||||
eval "$(pyenv virtualenv-init -)"
|
|
||||||
source /root/.pyenv/completions/pyenv.bash
|
|
||||||
|
|
||||||
pip install tox
|
|
||||||
|
|
||||||
exec $@
|
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
|
|
||||||
export CFLAGS='-O2'
|
|
||||||
export EXTRA_CFLAGS="-DTHREAD_STACK_SIZE=0x100000"
|
|
||||||
|
|
||||||
curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
|
|
||||||
eval "$(pyenv init -)"
|
|
||||||
|
|
||||||
for ver in $@
|
|
||||||
do
|
|
||||||
pyenv install $ver
|
|
||||||
done
|
|
||||||
|
|
||||||
pyenv global $@
|
|
||||||
pip install --upgrade pip
|
|
||||||
pyenv rehash
|
|
13
docs/_static/custom.css
vendored
Normal file
13
docs/_static/custom.css
vendored
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
.wy-side-nav-search,
|
||||||
|
.wy-nav-top {
|
||||||
|
background: #444444;
|
||||||
|
}
|
||||||
|
|
||||||
|
#changelog section {
|
||||||
|
padding-left: 3rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
#changelog section h2,
|
||||||
|
#changelog section h3 {
|
||||||
|
margin-left: -3rem;
|
||||||
|
}
|
BIN
docs/_static/sanic-framework-logo-white-400x97.png
vendored
Normal file
BIN
docs/_static/sanic-framework-logo-white-400x97.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
84
docs/conf.py
84
docs/conf.py
|
@ -10,9 +10,9 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
# Add support for auto-doc
|
# Add support for auto-doc
|
||||||
import recommonmark
|
|
||||||
from recommonmark.transform import AutoStructify
|
|
||||||
|
|
||||||
# Ensure that sanic is present in the path, to allow sphinx-apidoc to
|
# Ensure that sanic is present in the path, to allow sphinx-apidoc to
|
||||||
# autogenerate documentation from docstrings
|
# autogenerate documentation from docstrings
|
||||||
|
@ -21,23 +21,29 @@ sys.path.insert(0, root_directory)
|
||||||
|
|
||||||
import sanic
|
import sanic
|
||||||
|
|
||||||
|
|
||||||
# -- General configuration ------------------------------------------------
|
# -- General configuration ------------------------------------------------
|
||||||
|
|
||||||
extensions = ['sphinx.ext.autodoc', "recommonmark"]
|
extensions = [
|
||||||
|
"sphinx.ext.autodoc",
|
||||||
|
"m2r2",
|
||||||
|
"enum_tools.autoenum",
|
||||||
|
]
|
||||||
|
|
||||||
templates_path = ['_templates']
|
templates_path = ["_templates"]
|
||||||
|
|
||||||
# Enable support for both Restructured Text and Markdown
|
# Enable support for both Restructured Text and Markdown
|
||||||
source_suffix = ['.rst', '.md']
|
source_suffix = [".rst", ".md"]
|
||||||
|
|
||||||
# The master toctree document.
|
# The master toctree document.
|
||||||
master_doc = 'index'
|
master_doc = "index"
|
||||||
|
|
||||||
# General information about the project.
|
# General information about the project.
|
||||||
project = 'Sanic'
|
project = "Sanic"
|
||||||
copyright = '2018, Sanic contributors'
|
copyright = "2021, Sanic Community Organization"
|
||||||
author = 'Sanic contributors'
|
author = "Sanic Community Organization"
|
||||||
|
|
||||||
|
html_logo = "./_static/sanic-framework-logo-white-400x97.png"
|
||||||
# The version info for the project you're documenting, acts as replacement for
|
# The version info for the project you're documenting, acts as replacement for
|
||||||
# |version| and |release|, also used in various other places throughout the
|
# |version| and |release|, also used in various other places throughout the
|
||||||
# built documents.
|
# built documents.
|
||||||
|
@ -52,7 +58,7 @@ release = sanic.__version__
|
||||||
#
|
#
|
||||||
# This is also used if you do content translation via gettext catalogs.
|
# This is also used if you do content translation via gettext catalogs.
|
||||||
# Usually you set "language" from the command line for these cases.
|
# Usually you set "language" from the command line for these cases.
|
||||||
language = 'en'
|
language = "en"
|
||||||
|
|
||||||
# List of patterns, relative to source directory, that match files and
|
# List of patterns, relative to source directory, that match files and
|
||||||
# directories to ignore when looking for source files.
|
# directories to ignore when looking for source files.
|
||||||
|
@ -60,10 +66,10 @@ language = 'en'
|
||||||
#
|
#
|
||||||
# modules.rst is generated by sphinx-apidoc but is unused. This suppresses
|
# modules.rst is generated by sphinx-apidoc but is unused. This suppresses
|
||||||
# a warning about it.
|
# a warning about it.
|
||||||
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', 'modules.rst']
|
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "modules.rst"]
|
||||||
|
|
||||||
# The name of the Pygments (syntax highlighting) style to use.
|
# The name of the Pygments (syntax highlighting) style to use.
|
||||||
pygments_style = 'sphinx'
|
pygments_style = "sphinx"
|
||||||
|
|
||||||
# If true, `todo` and `todoList` produce output, else they produce nothing.
|
# If true, `todo` and `todoList` produce output, else they produce nothing.
|
||||||
todo_include_todos = False
|
todo_include_todos = False
|
||||||
|
@ -72,17 +78,17 @@ todo_include_todos = False
|
||||||
|
|
||||||
# The theme to use for HTML and HTML Help pages. See the documentation for
|
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||||
# a list of builtin themes.
|
# a list of builtin themes.
|
||||||
html_theme = 'sphinx_rtd_theme'
|
html_theme = "sphinx_rtd_theme"
|
||||||
|
|
||||||
# Add any paths that contain custom static files (such as style sheets) here,
|
# Add any paths that contain custom static files (such as style sheets) here,
|
||||||
# relative to this directory. They are copied after the builtin static files,
|
# relative to this directory. They are copied after the builtin static files,
|
||||||
# so a file named "default.css" will overwrite the builtin "default.css".
|
# so a file named "default.css" will overwrite the builtin "default.css".
|
||||||
html_static_path = ['_static']
|
html_static_path = ["_static"]
|
||||||
|
html_css_files = ["custom.css"]
|
||||||
# -- Options for HTMLHelp output ------------------------------------------
|
# -- Options for HTMLHelp output ------------------------------------------
|
||||||
|
|
||||||
# Output file base name for HTML help builder.
|
# Output file base name for HTML help builder.
|
||||||
htmlhelp_basename = 'Sanicdoc'
|
htmlhelp_basename = "Sanicdoc"
|
||||||
|
|
||||||
# -- Options for LaTeX output ---------------------------------------------
|
# -- Options for LaTeX output ---------------------------------------------
|
||||||
|
|
||||||
|
@ -90,15 +96,12 @@ latex_elements = {
|
||||||
# The paper size ('letterpaper' or 'a4paper').
|
# The paper size ('letterpaper' or 'a4paper').
|
||||||
#
|
#
|
||||||
# 'papersize': 'letterpaper',
|
# 'papersize': 'letterpaper',
|
||||||
|
|
||||||
# The font size ('10pt', '11pt' or '12pt').
|
# The font size ('10pt', '11pt' or '12pt').
|
||||||
#
|
#
|
||||||
# 'pointsize': '10pt',
|
# 'pointsize': '10pt',
|
||||||
|
|
||||||
# Additional stuff for the LaTeX preamble.
|
# Additional stuff for the LaTeX preamble.
|
||||||
#
|
#
|
||||||
# 'preamble': '',
|
# 'preamble': '',
|
||||||
|
|
||||||
# Latex figure (float) alignment
|
# Latex figure (float) alignment
|
||||||
#
|
#
|
||||||
# 'figure_align': 'htbp',
|
# 'figure_align': 'htbp',
|
||||||
|
@ -107,14 +110,21 @@ latex_elements = {
|
||||||
# Grouping the document tree into LaTeX files. List of tuples
|
# Grouping the document tree into LaTeX files. List of tuples
|
||||||
# (source start file, target name, title,
|
# (source start file, target name, title,
|
||||||
# author, documentclass [howto, manual, or own class]).
|
# author, documentclass [howto, manual, or own class]).
|
||||||
latex_documents = [(master_doc, 'Sanic.tex', 'Sanic Documentation',
|
latex_documents = [
|
||||||
'Sanic contributors', 'manual'), ]
|
(
|
||||||
|
master_doc,
|
||||||
|
"Sanic.tex",
|
||||||
|
"Sanic Documentation",
|
||||||
|
"Sanic contributors",
|
||||||
|
"manual",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
# -- Options for manual page output ---------------------------------------
|
# -- Options for manual page output ---------------------------------------
|
||||||
|
|
||||||
# One entry per manual page. List of tuples
|
# One entry per manual page. List of tuples
|
||||||
# (source start file, name, description, authors, manual section).
|
# (source start file, name, description, authors, manual section).
|
||||||
man_pages = [(master_doc, 'sanic', 'Sanic Documentation', [author], 1)]
|
man_pages = [(master_doc, "sanic", "Sanic Documentation", [author], 1)]
|
||||||
|
|
||||||
# -- Options for Texinfo output -------------------------------------------
|
# -- Options for Texinfo output -------------------------------------------
|
||||||
|
|
||||||
|
@ -122,8 +132,15 @@ man_pages = [(master_doc, 'sanic', 'Sanic Documentation', [author], 1)]
|
||||||
# (source start file, target name, title, author,
|
# (source start file, target name, title, author,
|
||||||
# dir menu entry, description, category)
|
# dir menu entry, description, category)
|
||||||
texinfo_documents = [
|
texinfo_documents = [
|
||||||
(master_doc, 'Sanic', 'Sanic Documentation', author, 'Sanic',
|
(
|
||||||
'One line description of project.', 'Miscellaneous'),
|
master_doc,
|
||||||
|
"Sanic",
|
||||||
|
"Sanic Documentation",
|
||||||
|
author,
|
||||||
|
"Sanic",
|
||||||
|
"One line description of project.",
|
||||||
|
"Miscellaneous",
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
# -- Options for Epub output ----------------------------------------------
|
# -- Options for Epub output ----------------------------------------------
|
||||||
|
@ -135,17 +152,18 @@ epub_publisher = author
|
||||||
epub_copyright = copyright
|
epub_copyright = copyright
|
||||||
|
|
||||||
# A list of files that should not be packed into the epub file.
|
# A list of files that should not be packed into the epub file.
|
||||||
epub_exclude_files = ['search.html']
|
epub_exclude_files = ["search.html"]
|
||||||
|
|
||||||
# -- Custom Settings -------------------------------------------------------
|
# -- Custom Settings -------------------------------------------------------
|
||||||
|
|
||||||
suppress_warnings = ['image.nonlocal_uri']
|
suppress_warnings = ["image.nonlocal_uri"]
|
||||||
|
|
||||||
|
|
||||||
# app setup hook
|
autodoc_typehints = "description"
|
||||||
def setup(app):
|
autodoc_default_options = {
|
||||||
app.add_config_value('recommonmark_config', {
|
"member-order": "groupwise",
|
||||||
'enable_eval_rst': True,
|
}
|
||||||
'enable_auto_doc_ref': False,
|
|
||||||
}, True)
|
html_theme_options = {
|
||||||
app.add_transform(AutoStructify)
|
"style_external_links": False,
|
||||||
|
}
|
||||||
|
|
|
@ -1,40 +1,23 @@
|
||||||
.. include:: sanic/index.rst
|
.. include:: ../README.rst
|
||||||
|
|
||||||
Guides
|
User Guide
|
||||||
======
|
==========
|
||||||
|
|
||||||
|
To learn about using Sanic, checkout the `User Guide <https://sanicframework.org/guide/>`__.
|
||||||
|
|
||||||
|
API
|
||||||
|
===
|
||||||
|
|
||||||
.. toctree::
|
.. toctree::
|
||||||
:maxdepth: 2
|
:maxdepth: 3
|
||||||
|
|
||||||
sanic/getting_started
|
👥 User Guide <https://sanicframework.org/guide/>
|
||||||
sanic/config
|
sanic/api_reference
|
||||||
sanic/logging
|
💻 Source code <https://github.com/sanic-org/sanic/>
|
||||||
sanic/request_data
|
|
||||||
sanic/response
|
|
||||||
sanic/cookies
|
|
||||||
sanic/routing
|
|
||||||
sanic/blueprints
|
|
||||||
sanic/static_files
|
|
||||||
sanic/versioning
|
|
||||||
sanic/exceptions
|
|
||||||
sanic/middleware
|
|
||||||
sanic/websocket
|
|
||||||
sanic/decorators
|
|
||||||
sanic/streaming
|
|
||||||
sanic/class_based_views
|
|
||||||
sanic/custom_protocol
|
|
||||||
sanic/sockets
|
|
||||||
sanic/ssl
|
|
||||||
sanic/debug_mode
|
|
||||||
sanic/testing
|
|
||||||
sanic/deploying
|
|
||||||
sanic/nginx
|
|
||||||
sanic/extensions
|
|
||||||
sanic/examples
|
|
||||||
sanic/changelog
|
sanic/changelog
|
||||||
sanic/contributing
|
sanic/contributing
|
||||||
sanic/api_reference
|
❓ Support <https://community.sanicframework.org/>
|
||||||
sanic/asyncio_python37
|
💬 Chat <https://discord.gg/FARQzAEMAA>
|
||||||
|
|
||||||
|
|
||||||
Module Documentation
|
Module Documentation
|
||||||
|
@ -44,4 +27,3 @@ Module Documentation
|
||||||
|
|
||||||
* :ref:`genindex`
|
* :ref:`genindex`
|
||||||
* :ref:`modindex`
|
* :ref:`modindex`
|
||||||
* :ref:`search`
|
|
||||||
|
|
33
docs/sanic/api/app.rst
Normal file
33
docs/sanic/api/app.rst
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
Application
|
||||||
|
===========
|
||||||
|
|
||||||
|
sanic.app
|
||||||
|
---------
|
||||||
|
|
||||||
|
.. automodule:: sanic.app
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
:inherited-members:
|
||||||
|
|
||||||
|
sanic.config
|
||||||
|
------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.config
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
sanic.application.constants
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.application.constants
|
||||||
|
:exclude-members: StrEnum
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
:inherited-members:
|
||||||
|
|
||||||
|
sanic.application.state
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.application.state
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
17
docs/sanic/api/blueprints.rst
Normal file
17
docs/sanic/api/blueprints.rst
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
Blueprints
|
||||||
|
==========
|
||||||
|
|
||||||
|
sanic.blueprints
|
||||||
|
----------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.blueprints
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
:inherited-members:
|
||||||
|
|
||||||
|
sanic.blueprint_group
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.blueprint_group
|
||||||
|
:members:
|
||||||
|
:special-members:
|
48
docs/sanic/api/core.rst
Normal file
48
docs/sanic/api/core.rst
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
Core
|
||||||
|
====
|
||||||
|
|
||||||
|
sanic.cookies
|
||||||
|
-------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.cookies
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
|
||||||
|
sanic.handlers
|
||||||
|
--------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.handlers
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
|
||||||
|
sanic.headers
|
||||||
|
--------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.headers
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
|
||||||
|
sanic.request
|
||||||
|
-------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.request
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
sanic.response
|
||||||
|
--------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.response
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
|
||||||
|
sanic.views
|
||||||
|
-----------
|
||||||
|
|
||||||
|
.. automodule:: sanic.views
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
16
docs/sanic/api/exceptions.rst
Normal file
16
docs/sanic/api/exceptions.rst
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
Exceptions
|
||||||
|
==========
|
||||||
|
|
||||||
|
sanic.errorpages
|
||||||
|
----------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.errorpages
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
sanic.exceptions
|
||||||
|
----------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.exceptions
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
18
docs/sanic/api/router.rst
Normal file
18
docs/sanic/api/router.rst
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
Routing
|
||||||
|
=======
|
||||||
|
|
||||||
|
sanic_routing models
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
.. autoclass:: sanic_routing.route::Route
|
||||||
|
:members:
|
||||||
|
|
||||||
|
.. autoclass:: sanic_routing.group::RouteGroup
|
||||||
|
:members:
|
||||||
|
|
||||||
|
sanic.router
|
||||||
|
------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.router
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
18
docs/sanic/api/server.rst
Normal file
18
docs/sanic/api/server.rst
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
Sanic Server
|
||||||
|
============
|
||||||
|
|
||||||
|
sanic.http
|
||||||
|
----------
|
||||||
|
|
||||||
|
.. automodule:: sanic.http
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
|
||||||
|
sanic.server
|
||||||
|
------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.server
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
16
docs/sanic/api/utility.rst
Normal file
16
docs/sanic/api/utility.rst
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
Utility
|
||||||
|
=======
|
||||||
|
|
||||||
|
sanic.compat
|
||||||
|
------------
|
||||||
|
|
||||||
|
.. automodule:: sanic.compat
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
||||||
|
|
||||||
|
sanic.log
|
||||||
|
---------
|
||||||
|
|
||||||
|
.. automodule:: sanic.log
|
||||||
|
:members:
|
||||||
|
:show-inheritance:
|
|
@ -1,159 +1,13 @@
|
||||||
API Reference
|
📑 API Reference
|
||||||
=============
|
================
|
||||||
|
|
||||||
Submodules
|
.. toctree::
|
||||||
----------
|
:maxdepth: 2
|
||||||
|
|
||||||
sanic.app module
|
api/app
|
||||||
----------------
|
api/blueprints
|
||||||
|
api/core
|
||||||
.. automodule:: sanic.app
|
api/exceptions
|
||||||
:members:
|
api/router
|
||||||
:undoc-members:
|
api/server
|
||||||
:show-inheritance:
|
api/utility
|
||||||
|
|
||||||
sanic.blueprints module
|
|
||||||
-----------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.blueprints
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.blueprint_group module
|
|
||||||
----------------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.blueprint_group
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
|
|
||||||
sanic.config module
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.config
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.constants module
|
|
||||||
----------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.constants
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.cookies module
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.cookies
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.exceptions module
|
|
||||||
-----------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.exceptions
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.handlers module
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.handlers
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.log module
|
|
||||||
----------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.log
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.request module
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.request
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.response module
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.response
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.router module
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.router
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.server module
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.server
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.static module
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.static
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.testing module
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.testing
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.views module
|
|
||||||
------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.views
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.websocket module
|
|
||||||
----------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.websocket
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
sanic.worker module
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
.. automodule:: sanic.worker
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
||||||
|
|
||||||
Module contents
|
|
||||||
---------------
|
|
||||||
|
|
||||||
.. automodule:: sanic
|
|
||||||
:members:
|
|
||||||
:undoc-members:
|
|
||||||
:show-inheritance:
|
|
||||||
|
|
|
@ -1,58 +0,0 @@
|
||||||
Python 3.7 AsyncIO examples
|
|
||||||
###########################
|
|
||||||
|
|
||||||
With Python 3.7 AsyncIO got major update for the following types:
|
|
||||||
|
|
||||||
- asyncio.AbstractEventLoop
|
|
||||||
- asyncio.AbstractServer
|
|
||||||
|
|
||||||
|
|
||||||
This example shows how to use sanic with Python 3.7, to be precise: how to retrieve an asyncio server instance:
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import socket
|
|
||||||
import os
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
async def test(request):
|
|
||||||
return json({"hello": "world"})
|
|
||||||
|
|
||||||
|
|
||||||
server_socket = '/tmp/sanic.sock'
|
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
||||||
|
|
||||||
try:
|
|
||||||
os.remove(server_socket)
|
|
||||||
finally:
|
|
||||||
sock.bind(server_socket)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
srv_coro = app.create_server(
|
|
||||||
sock=sock,
|
|
||||||
return_asyncio_server=True,
|
|
||||||
asyncio_server_kwargs=dict(
|
|
||||||
start_serving=False
|
|
||||||
)
|
|
||||||
)
|
|
||||||
srv = loop.run_until_complete(srv_coro)
|
|
||||||
try:
|
|
||||||
assert srv.is_serving() is False
|
|
||||||
loop.run_until_complete(srv.start_serving())
|
|
||||||
assert srv.is_serving() is True
|
|
||||||
loop.run_until_complete(srv.serve_forever())
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
srv.close()
|
|
||||||
loop.close()
|
|
||||||
|
|
||||||
|
|
||||||
Please note that uvloop does not support these features yet.
|
|
|
@ -1,301 +0,0 @@
|
||||||
Blueprints
|
|
||||||
==========
|
|
||||||
|
|
||||||
Blueprints are objects that can be used for sub-routing within an application.
|
|
||||||
Instead of adding routes to the application instance, blueprints define similar
|
|
||||||
methods for adding routes, which are then registered with the application in a
|
|
||||||
flexible and pluggable manner.
|
|
||||||
|
|
||||||
Blueprints are especially useful for larger applications, where your
|
|
||||||
application logic can be broken down into several groups or areas of
|
|
||||||
responsibility.
|
|
||||||
|
|
||||||
My First Blueprint
|
|
||||||
------------------
|
|
||||||
|
|
||||||
The following shows a very simple blueprint that registers a handler-function at
|
|
||||||
the root `/` of your application.
|
|
||||||
|
|
||||||
Suppose you save this file as `my_blueprint.py`, which can be imported into your
|
|
||||||
main application later.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import json
|
|
||||||
from sanic import Blueprint
|
|
||||||
|
|
||||||
bp = Blueprint('my_blueprint')
|
|
||||||
|
|
||||||
@bp.route('/')
|
|
||||||
async def bp_root(request):
|
|
||||||
return json({'my': 'blueprint'})
|
|
||||||
|
|
||||||
Registering blueprints
|
|
||||||
----------------------
|
|
||||||
|
|
||||||
Blueprints must be registered with the application.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from my_blueprint import bp
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
app.blueprint(bp)
|
|
||||||
|
|
||||||
app.run(host='0.0.0.0', port=8000, debug=True)
|
|
||||||
|
|
||||||
This will add the blueprint to the application and register any routes defined
|
|
||||||
by that blueprint. In this example, the registered routes in the `app.router`
|
|
||||||
will look like:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
[Route(handler=<function bp_root at 0x7f908382f9d8>, methods=frozenset({'GET'}), pattern=re.compile('^/$'), parameters=[], name='my_blueprint.bp_root', uri='/')]
|
|
||||||
|
|
||||||
Blueprint groups and nesting
|
|
||||||
----------------------------
|
|
||||||
|
|
||||||
Blueprints may also be registered as part of a list or tuple, where the registrar will recursively cycle through any sub-sequences of blueprints and register them accordingly. The `Blueprint.group` method is provided to simplify this process, allowing a 'mock' backend directory structure mimicking what's seen from the front end. Consider this (quite contrived) example:
|
|
||||||
|
|
||||||
| api/
|
|
||||||
| ├──content/
|
|
||||||
| │ ├──authors.py
|
|
||||||
| │ ├──static.py
|
|
||||||
| │ └──__init__.py
|
|
||||||
| ├──info.py
|
|
||||||
| └──__init__.py
|
|
||||||
| app.py
|
|
||||||
|
|
||||||
Initialization of this app's blueprint hierarchy could go as follows:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# api/content/authors.py
|
|
||||||
from sanic import Blueprint
|
|
||||||
|
|
||||||
authors = Blueprint('content_authors', url_prefix='/authors')
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# api/content/static.py
|
|
||||||
from sanic import Blueprint
|
|
||||||
|
|
||||||
static = Blueprint('content_static', url_prefix='/static')
|
|
||||||
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# api/content/__init__.py
|
|
||||||
from sanic import Blueprint
|
|
||||||
|
|
||||||
from .static import static
|
|
||||||
from .authors import authors
|
|
||||||
|
|
||||||
content = Blueprint.group(static, authors, url_prefix='/content')
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# api/info.py
|
|
||||||
from sanic import Blueprint
|
|
||||||
|
|
||||||
info = Blueprint('info', url_prefix='/info')
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# api/__init__.py
|
|
||||||
from sanic import Blueprint
|
|
||||||
|
|
||||||
from .content import content
|
|
||||||
from .info import info
|
|
||||||
|
|
||||||
api = Blueprint.group(content, info, url_prefix='/api')
|
|
||||||
|
|
||||||
And registering these blueprints in `app.py` can now be done like so:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# app.py
|
|
||||||
from sanic import Sanic
|
|
||||||
|
|
||||||
from .api import api
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
app.blueprint(api)
|
|
||||||
|
|
||||||
Using Blueprints
|
|
||||||
----------------
|
|
||||||
|
|
||||||
Blueprints have almost the same functionality as an application instance.
|
|
||||||
|
|
||||||
WebSocket routes
|
|
||||||
~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
WebSocket handlers can be registered on a blueprint using the `@bp.websocket`
|
|
||||||
decorator or `bp.add_websocket_route` method.
|
|
||||||
|
|
||||||
Blueprint Middleware
|
|
||||||
~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Using blueprints allows you to also register middleware globally.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@bp.middleware
|
|
||||||
async def print_on_request(request):
|
|
||||||
print("I am a spy")
|
|
||||||
|
|
||||||
@bp.middleware('request')
|
|
||||||
async def halt_request(request):
|
|
||||||
return text('I halted the request')
|
|
||||||
|
|
||||||
@bp.middleware('response')
|
|
||||||
async def halt_response(request, response):
|
|
||||||
return text('I halted the response')
|
|
||||||
|
|
||||||
|
|
||||||
Blueprint Group Middleware
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Using this middleware will ensure that you can apply a common middleware to all the blueprints that form the
|
|
||||||
current blueprint group under consideration.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
bp1 = Blueprint('bp1', url_prefix='/bp1')
|
|
||||||
bp2 = Blueprint('bp2', url_prefix='/bp2')
|
|
||||||
|
|
||||||
@bp1.middleware('request')
|
|
||||||
async def bp1_only_middleware(request):
|
|
||||||
print('applied on Blueprint : bp1 Only')
|
|
||||||
|
|
||||||
@bp1.route('/')
|
|
||||||
async def bp1_route(request):
|
|
||||||
return text('bp1')
|
|
||||||
|
|
||||||
@bp2.route('/<param>')
|
|
||||||
async def bp2_route(request, param):
|
|
||||||
return text(param)
|
|
||||||
|
|
||||||
group = Blueprint.group(bp1, bp2)
|
|
||||||
|
|
||||||
@group.middleware('request')
|
|
||||||
async def group_middleware(request):
|
|
||||||
print('common middleware applied for both bp1 and bp2')
|
|
||||||
|
|
||||||
# Register Blueprint group under the app
|
|
||||||
app.blueprint(group)
|
|
||||||
|
|
||||||
Exceptions
|
|
||||||
~~~~~~~~~~
|
|
||||||
|
|
||||||
Exceptions can be applied exclusively to blueprints globally.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@bp.exception(NotFound)
|
|
||||||
def ignore_404s(request, exception):
|
|
||||||
return text("Yep, I totally found the page: {}".format(request.url))
|
|
||||||
|
|
||||||
Static files
|
|
||||||
~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Static files can be served globally, under the blueprint prefix.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# suppose bp.name == 'bp'
|
|
||||||
|
|
||||||
bp.static('/web/path', '/folder/to/serve')
|
|
||||||
# also you can pass name parameter to it for url_for
|
|
||||||
bp.static('/web/path', '/folder/to/server', name='uploads')
|
|
||||||
app.url_for('static', name='bp.uploads', filename='file.txt') == '/bp/web/path/file.txt'
|
|
||||||
|
|
||||||
Start and stop
|
|
||||||
--------------
|
|
||||||
|
|
||||||
Blueprints can run functions during the start and stop process of the server.
|
|
||||||
If running in multiprocessor mode (more than 1 worker), these are triggered
|
|
||||||
after the workers fork.
|
|
||||||
|
|
||||||
Available events are:
|
|
||||||
|
|
||||||
- `before_server_start`: Executed before the server begins to accept connections
|
|
||||||
- `after_server_start`: Executed after the server begins to accept connections
|
|
||||||
- `before_server_stop`: Executed before the server stops accepting connections
|
|
||||||
- `after_server_stop`: Executed after the server is stopped and all requests are complete
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
bp = Blueprint('my_blueprint')
|
|
||||||
|
|
||||||
@bp.listener('before_server_start')
|
|
||||||
async def setup_connection(app, loop):
|
|
||||||
global database
|
|
||||||
database = mysql.connect(host='127.0.0.1'...)
|
|
||||||
|
|
||||||
@bp.listener('after_server_stop')
|
|
||||||
async def close_connection(app, loop):
|
|
||||||
await database.close()
|
|
||||||
|
|
||||||
Use-case: API versioning
|
|
||||||
------------------------
|
|
||||||
|
|
||||||
Blueprints can be very useful for API versioning, where one blueprint may point
|
|
||||||
at `/v1/<routes>`, and another pointing at `/v2/<routes>`.
|
|
||||||
|
|
||||||
When a blueprint is initialised, it can take an optional `version` argument,
|
|
||||||
which will be prepended to all routes defined on the blueprint. This feature
|
|
||||||
can be used to implement our API versioning scheme.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# blueprints.py
|
|
||||||
from sanic.response import text
|
|
||||||
from sanic import Blueprint
|
|
||||||
|
|
||||||
blueprint_v1 = Blueprint('v1', url_prefix='/api', version="v1")
|
|
||||||
blueprint_v2 = Blueprint('v2', url_prefix='/api', version="v2")
|
|
||||||
|
|
||||||
@blueprint_v1.route('/')
|
|
||||||
async def api_v1_root(request):
|
|
||||||
return text('Welcome to version 1 of our documentation')
|
|
||||||
|
|
||||||
@blueprint_v2.route('/')
|
|
||||||
async def api_v2_root(request):
|
|
||||||
return text('Welcome to version 2 of our documentation')
|
|
||||||
|
|
||||||
When we register our blueprints on the app, the routes `/v1/api` and `/v2/api` will now
|
|
||||||
point to the individual blueprints, which allows the creation of *sub-sites*
|
|
||||||
for each API version.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# main.py
|
|
||||||
from sanic import Sanic
|
|
||||||
from blueprints import blueprint_v1, blueprint_v2
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
app.blueprint(blueprint_v1)
|
|
||||||
app.blueprint(blueprint_v2)
|
|
||||||
|
|
||||||
app.run(host='0.0.0.0', port=8000, debug=True)
|
|
||||||
|
|
||||||
URL Building with `url_for`
|
|
||||||
---------------------------
|
|
||||||
|
|
||||||
If you wish to generate a URL for a route inside of a blueprint, remember that the endpoint name
|
|
||||||
takes the format `<blueprint_name>.<handler_name>`. For example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@blueprint_v1.route('/')
|
|
||||||
async def root(request):
|
|
||||||
url = request.app.url_for('v1.post_handler', post_id=5) # --> '/v1/api/post/5'
|
|
||||||
return redirect(url)
|
|
||||||
|
|
||||||
|
|
||||||
@blueprint_v1.route('/post/<post_id>')
|
|
||||||
async def post_handler(request, post_id):
|
|
||||||
return text('Post {} in Blueprint V1'.format(post_id))
|
|
|
@ -1,4 +1,16 @@
|
||||||
Changelog
|
📜 Changelog
|
||||||
---------
|
============
|
||||||
|
|
||||||
|
| 🔶 Current release
|
||||||
|
| 🔷 In support release
|
||||||
|
|
|
||||||
|
|
||||||
|
.. mdinclude:: ./releases/23/23.6.md
|
||||||
|
.. mdinclude:: ./releases/23/23.3.md
|
||||||
|
.. mdinclude:: ./releases/22/22.12.md
|
||||||
|
.. mdinclude:: ./releases/22/22.9.md
|
||||||
|
.. mdinclude:: ./releases/22/22.6.md
|
||||||
|
.. mdinclude:: ./releases/22/22.3.md
|
||||||
|
.. mdinclude:: ./releases/21/21.12.md
|
||||||
|
.. mdinclude:: ./releases/21/21.9.md
|
||||||
.. include:: ../../CHANGELOG.rst
|
.. include:: ../../CHANGELOG.rst
|
|
@ -1,154 +0,0 @@
|
||||||
Class-Based Views
|
|
||||||
=================
|
|
||||||
|
|
||||||
Class-based views are simply classes which implement response behaviour to
|
|
||||||
requests. They provide a way to compartmentalise handling of different HTTP
|
|
||||||
request types at the same endpoint. Rather than defining and decorating three
|
|
||||||
different handler functions, one for each of an endpoint's supported request
|
|
||||||
type, the endpoint can be assigned a class-based view.
|
|
||||||
|
|
||||||
Defining views
|
|
||||||
--------------
|
|
||||||
|
|
||||||
A class-based view should subclass `HTTPMethodView`. You can then implement
|
|
||||||
class methods for every HTTP request type you want to support. If a request is
|
|
||||||
received that has no defined method, a `405: Method not allowed` response will
|
|
||||||
be generated.
|
|
||||||
|
|
||||||
To register a class-based view on an endpoint, the `app.add_route` method is
|
|
||||||
used. The first argument should be the defined class with the method `as_view`
|
|
||||||
invoked, and the second should be the URL endpoint.
|
|
||||||
|
|
||||||
The available methods are `get`, `post`, `put`, `patch`, and `delete`. A class
|
|
||||||
using all these methods would look like the following.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.views import HTTPMethodView
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
app = Sanic("class_views_example")
|
|
||||||
|
|
||||||
class SimpleView(HTTPMethodView):
|
|
||||||
|
|
||||||
def get(self, request):
|
|
||||||
return text('I am get method')
|
|
||||||
|
|
||||||
# You can also use async syntax
|
|
||||||
async def post(self, request):
|
|
||||||
return text('I am post method')
|
|
||||||
|
|
||||||
def put(self, request):
|
|
||||||
return text('I am put method')
|
|
||||||
|
|
||||||
def patch(self, request):
|
|
||||||
return text('I am patch method')
|
|
||||||
|
|
||||||
def delete(self, request):
|
|
||||||
return text('I am delete method')
|
|
||||||
|
|
||||||
app.add_route(SimpleView.as_view(), '/')
|
|
||||||
|
|
||||||
|
|
||||||
URL parameters
|
|
||||||
--------------
|
|
||||||
|
|
||||||
If you need any URL parameters, as discussed in the routing guide, include them
|
|
||||||
in the method definition.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
class NameView(HTTPMethodView):
|
|
||||||
|
|
||||||
def get(self, request, name):
|
|
||||||
return text('Hello {}'.format(name))
|
|
||||||
|
|
||||||
app.add_route(NameView.as_view(), '/<name>')
|
|
||||||
|
|
||||||
Decorators
|
|
||||||
----------
|
|
||||||
|
|
||||||
If you want to add any decorators to the class, you can set the `decorators`
|
|
||||||
class variable. These will be applied to the class when `as_view` is called.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
class ViewWithDecorator(HTTPMethodView):
|
|
||||||
decorators = [some_decorator_here]
|
|
||||||
|
|
||||||
def get(self, request, name):
|
|
||||||
return text('Hello I have a decorator')
|
|
||||||
|
|
||||||
def post(self, request, name):
|
|
||||||
return text("Hello I also have a decorator")
|
|
||||||
|
|
||||||
app.add_route(ViewWithDecorator.as_view(), '/url')
|
|
||||||
|
|
||||||
But if you just want to decorate some functions and not all functions, you can do as follows:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
class ViewWithSomeDecorator(HTTPMethodView):
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@some_decorator_here
|
|
||||||
def get(request, name):
|
|
||||||
return text("Hello I have a decorator")
|
|
||||||
|
|
||||||
def post(self, request, name):
|
|
||||||
return text("Hello I don't have any decorators")
|
|
||||||
|
|
||||||
URL Building
|
|
||||||
------------
|
|
||||||
|
|
||||||
If you wish to build a URL for an HTTPMethodView, remember that the class name will be the endpoint
|
|
||||||
that you will pass into `url_for`. For example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def index(request):
|
|
||||||
url = app.url_for('SpecialClassView')
|
|
||||||
return redirect(url)
|
|
||||||
|
|
||||||
|
|
||||||
class SpecialClassView(HTTPMethodView):
|
|
||||||
def get(self, request):
|
|
||||||
return text('Hello from the Special Class View!')
|
|
||||||
|
|
||||||
|
|
||||||
app.add_route(SpecialClassView.as_view(), '/special_class_view')
|
|
||||||
|
|
||||||
Using CompositionView
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
As an alternative to the `HTTPMethodView`, you can use `CompositionView` to
|
|
||||||
move handler functions outside of the view class.
|
|
||||||
|
|
||||||
Handler functions for each supported HTTP method are defined elsewhere in the
|
|
||||||
source, and then added to the view using the `CompositionView.add` method. The
|
|
||||||
first parameter is a list of HTTP methods to handle (e.g. `['GET', 'POST']`),
|
|
||||||
and the second is the handler function. The following example shows
|
|
||||||
`CompositionView` usage with both an external handler function and an inline
|
|
||||||
lambda:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.views import CompositionView
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
app = Sanic("composition_example")
|
|
||||||
|
|
||||||
def get_handler(request):
|
|
||||||
return text('I am a get method')
|
|
||||||
|
|
||||||
view = CompositionView()
|
|
||||||
view.add(['GET'], get_handler)
|
|
||||||
view.add(['POST', 'PUT'], lambda request: text('I am a post/put method'))
|
|
||||||
|
|
||||||
# Use the new view to handle requests to the base URL
|
|
||||||
app.add_route(view, '/')
|
|
||||||
|
|
||||||
Note: currently you cannot build a URL for a CompositionView using `url_for`.
|
|
|
@ -1,339 +0,0 @@
|
||||||
Configuration
|
|
||||||
=============
|
|
||||||
|
|
||||||
Any reasonably complex application will need configuration that is not baked into the actual code. Settings might be different for different environments or installations.
|
|
||||||
|
|
||||||
Basics
|
|
||||||
------
|
|
||||||
|
|
||||||
Sanic holds the configuration in the `config` attribute of the application object. The configuration object is merely an object that can be modified either using dot-notation or like a dictionary:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app = Sanic('myapp')
|
|
||||||
app.config.DB_NAME = 'appdb'
|
|
||||||
app.config['DB_USER'] = 'appuser'
|
|
||||||
|
|
||||||
Since the config object has a type that inherits from dictionary, you can use its ``update`` method in order to set several values at once:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
db_settings = {
|
|
||||||
'DB_HOST': 'localhost',
|
|
||||||
'DB_NAME': 'appdb',
|
|
||||||
'DB_USER': 'appuser'
|
|
||||||
}
|
|
||||||
app.config.update(db_settings)
|
|
||||||
|
|
||||||
In general the convention is to only have UPPERCASE configuration parameters. The methods described below for loading configuration only look for such uppercase parameters.
|
|
||||||
|
|
||||||
Loading Configuration
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
There are several ways how to load configuration.
|
|
||||||
|
|
||||||
From Environment Variables
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Any variables defined with the `SANIC_` prefix will be applied to the sanic config. For example, setting `SANIC_REQUEST_TIMEOUT` will be loaded by the application automatically and fed into the `REQUEST_TIMEOUT` config variable. You can pass a different prefix to Sanic:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app = Sanic(__name__, load_env='MYAPP_')
|
|
||||||
|
|
||||||
Then the above variable would be `MYAPP_REQUEST_TIMEOUT`. If you want to disable loading from environment variables you can set it to `False` instead:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app = Sanic(__name__, load_env=False)
|
|
||||||
|
|
||||||
From file, dict, or any object (having __dict__ attribute).
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
You can store app configurations in: (1) a Python file, (2) a dictionary, or (3) in some other type of custom object.
|
|
||||||
|
|
||||||
In order to load configuration from ove of those, you can use ``app.upload_config()``.
|
|
||||||
|
|
||||||
**1) From file**
|
|
||||||
|
|
||||||
|
|
||||||
Let's say you have ``my_config.py`` file that looks like this:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# my_config.py
|
|
||||||
A = 1
|
|
||||||
B = 2
|
|
||||||
|
|
||||||
Loading config from this file is as easy as:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app.update_config("/path/to/my_config.py")
|
|
||||||
|
|
||||||
You can also use environment variables in the path name here.
|
|
||||||
|
|
||||||
Let's say you have an environment variable like this:
|
|
||||||
|
|
||||||
.. code-block:: shell
|
|
||||||
|
|
||||||
$ export my_path="/path/to"
|
|
||||||
|
|
||||||
Then you can use it like this:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app.update_config("${my_path}/my_config.py")
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
Just remember that you have to provide environment variables in the format ${environment_variable} and that $environment_variable is not expanded (is treated as "plain" text).
|
|
||||||
|
|
||||||
**2) From dict**
|
|
||||||
|
|
||||||
You can also set your app config by providing a ``dict``:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
d = {"A": 1, "B": 2}
|
|
||||||
|
|
||||||
app.update_config(d)
|
|
||||||
|
|
||||||
**3) From _any_ object**
|
|
||||||
|
|
||||||
App config can be taken from an object. Internally, it uses ``__dict__`` to retrieve keys and values.
|
|
||||||
|
|
||||||
For example, pass the class:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
class C:
|
|
||||||
A = 1
|
|
||||||
B = 2
|
|
||||||
|
|
||||||
app.update_config(C)
|
|
||||||
|
|
||||||
or, it can be instantiated:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
c = C()
|
|
||||||
|
|
||||||
app.update_config(c)
|
|
||||||
|
|
||||||
- From an object (having __dict__ attribute)
|
|
||||||
|
|
||||||
|
|
||||||
From an Object
|
|
||||||
~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
Deprecated, will be removed in version 21.3.
|
|
||||||
|
|
||||||
If there are a lot of configuration values and they have sensible defaults it might be helpful to put them into a module:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
import myapp.default_settings
|
|
||||||
|
|
||||||
app = Sanic('myapp')
|
|
||||||
app.config.from_object(myapp.default_settings)
|
|
||||||
|
|
||||||
or also by path to config:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app = Sanic('myapp')
|
|
||||||
app.config.from_object('config.path.config.Class')
|
|
||||||
|
|
||||||
You could use a class or any other object as well.
|
|
||||||
|
|
||||||
From a File
|
|
||||||
~~~~~~~~~~~
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
Deprecated, will be removed in version 21.3.
|
|
||||||
|
|
||||||
Usually you will want to load configuration from a file that is not part of the distributed application. You can load configuration from a file using `from_pyfile(/path/to/config_file)`. However, that requires the program to know the path to the config file. So instead you can specify the location of the config file in an environment variable and tell Sanic to use that to find the config file:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app = Sanic('myapp')
|
|
||||||
app.config.from_envvar('MYAPP_SETTINGS')
|
|
||||||
|
|
||||||
Then you can run your application with the `MYAPP_SETTINGS` environment variable set:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
#$ MYAPP_SETTINGS=/path/to/config_file python3 myapp.py
|
|
||||||
#INFO: Goin' Fast @ http://0.0.0.0:8000
|
|
||||||
|
|
||||||
|
|
||||||
The config files are regular Python files which are executed in order to load them. This allows you to use arbitrary logic for constructing the right configuration. Only uppercase variables are added to the configuration. Most commonly the configuration consists of simple key value pairs:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# config_file
|
|
||||||
DB_HOST = 'localhost'
|
|
||||||
DB_NAME = 'appdb'
|
|
||||||
DB_USER = 'appuser'
|
|
||||||
|
|
||||||
Builtin Configuration Values
|
|
||||||
----------------------------
|
|
||||||
|
|
||||||
Out of the box there are just a few predefined values which can be overwritten when creating the application. Note that websocket configuration values will have no impact if running in ASGI mode.
|
|
||||||
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| Variable | Default | Description |
|
|
||||||
+===========================+===================+=============================================================================+
|
|
||||||
| REQUEST_MAX_SIZE | 100000000 | How big a request may be (bytes) |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| REQUEST_BUFFER_QUEUE_SIZE | 100 | Request streaming buffer queue size |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| REQUEST_TIMEOUT | 60 | How long a request can take to arrive (sec) |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| RESPONSE_TIMEOUT | 60 | How long a response can take to process (sec) |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| KEEP_ALIVE | True | Disables keep-alive when False |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| KEEP_ALIVE_TIMEOUT | 5 | How long to hold a TCP connection open (sec) |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| WEBSOCKET_MAX_SIZE | 2^20 | Maximum size for incoming messages (bytes) |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| WEBSOCKET_MAX_QUEUE | 32 | Maximum length of the queue that holds incoming messages |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| WEBSOCKET_READ_LIMIT | 2^16 | High-water limit of the buffer for incoming bytes |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| WEBSOCKET_WRITE_LIMIT | 2^16 | High-water limit of the buffer for outgoing bytes |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| WEBSOCKET_PING_INTERVAL | 20 | A Ping frame is sent every ping_interval seconds. |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| WEBSOCKET_PING_TIMEOUT | 20 | Connection is closed when Pong is not received after ping_timeout seconds |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| GRACEFUL_SHUTDOWN_TIMEOUT | 15.0 | How long to wait to force close non-idle connection (sec) |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| ACCESS_LOG | True | Disable or enable access log |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| FORWARDED_SECRET | None | Used to securely identify a specific proxy server (see below) |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| PROXIES_COUNT | None | The number of proxy servers in front of the app (e.g. nginx; see below) |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| FORWARDED_FOR_HEADER | "X-Forwarded-For" | The name of "X-Forwarded-For" HTTP header that contains client and proxy ip |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
| REAL_IP_HEADER | None | The name of "X-Real-IP" HTTP header that contains real client ip |
|
|
||||||
+---------------------------+-------------------+-----------------------------------------------------------------------------+
|
|
||||||
|
|
||||||
The different Timeout variables:
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
`REQUEST_TIMEOUT`
|
|
||||||
#################
|
|
||||||
|
|
||||||
A request timeout measures the duration of time between the instant when a new open TCP connection is passed to the
|
|
||||||
Sanic backend server, and the instant when the whole HTTP request is received. If the time taken exceeds the
|
|
||||||
`REQUEST_TIMEOUT` value (in seconds), this is considered a Client Error so Sanic generates an `HTTP 408` response
|
|
||||||
and sends that to the client. Set this parameter's value higher if your clients routinely pass very large request payloads
|
|
||||||
or upload requests very slowly.
|
|
||||||
|
|
||||||
`RESPONSE_TIMEOUT`
|
|
||||||
##################
|
|
||||||
|
|
||||||
A response timeout measures the duration of time between the instant the Sanic server passes the HTTP request to the
|
|
||||||
Sanic App, and the instant a HTTP response is sent to the client. If the time taken exceeds the `RESPONSE_TIMEOUT`
|
|
||||||
value (in seconds), this is considered a Server Error so Sanic generates an `HTTP 503` response and sends that to the
|
|
||||||
client. Set this parameter's value higher if your application is likely to have long-running process that delay the
|
|
||||||
generation of a response.
|
|
||||||
|
|
||||||
`KEEP_ALIVE_TIMEOUT`
|
|
||||||
####################
|
|
||||||
|
|
||||||
What is Keep Alive? And what does the Keep Alive Timeout value do?
|
|
||||||
******************************************************************
|
|
||||||
|
|
||||||
`Keep-Alive` is a HTTP feature introduced in `HTTP 1.1`. When sending a HTTP request, the client (usually a web browser application)
|
|
||||||
can set a `Keep-Alive` header to indicate the http server (Sanic) to not close the TCP connection after it has send the response.
|
|
||||||
This allows the client to reuse the existing TCP connection to send subsequent HTTP requests, and ensures more efficient
|
|
||||||
network traffic for both the client and the server.
|
|
||||||
|
|
||||||
The `KEEP_ALIVE` config variable is set to `True` in Sanic by default. If you don't need this feature in your application,
|
|
||||||
set it to `False` to cause all client connections to close immediately after a response is sent, regardless of
|
|
||||||
the `Keep-Alive` header on the request.
|
|
||||||
|
|
||||||
The amount of time the server holds the TCP connection open is decided by the server itself.
|
|
||||||
In Sanic, that value is configured using the `KEEP_ALIVE_TIMEOUT` value. By default, it is set to 5 seconds.
|
|
||||||
This is the same default setting as the Apache HTTP server and is a good balance between allowing enough time for
|
|
||||||
the client to send a new request, and not holding open too many connections at once. Do not exceed 75 seconds unless
|
|
||||||
you know your clients are using a browser which supports TCP connections held open for that long.
|
|
||||||
|
|
||||||
For reference:
|
|
||||||
|
|
||||||
* Apache httpd server default keepalive timeout = 5 seconds
|
|
||||||
* Nginx server default keepalive timeout = 75 seconds
|
|
||||||
* Nginx performance tuning guidelines uses keepalive = 15 seconds
|
|
||||||
* IE (5-9) client hard keepalive limit = 60 seconds
|
|
||||||
* Firefox client hard keepalive limit = 115 seconds
|
|
||||||
* Opera 11 client hard keepalive limit = 120 seconds
|
|
||||||
* Chrome 13+ client keepalive limit > 300+ seconds
|
|
||||||
|
|
||||||
|
|
||||||
Proxy configuration
|
|
||||||
~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
When you use a reverse proxy server (e.g. nginx), the value of `request.ip` will contain ip of a proxy,
|
|
||||||
typically `127.0.0.1`. Sanic may be configured to use proxy headers for determining the true client IP,
|
|
||||||
available as `request.remote_addr`. The full external URL is also constructed from header fields if available.
|
|
||||||
|
|
||||||
Without proper precautions, a malicious client may use proxy headers to spoof its own IP. To avoid such issues, Sanic does not use any proxy headers unless explicitly enabled.
|
|
||||||
|
|
||||||
Services behind reverse proxies must configure `FORWARDED_SECRET`, `REAL_IP_HEADER` and/or `PROXIES_COUNT`.
|
|
||||||
|
|
||||||
Forwarded header
|
|
||||||
################
|
|
||||||
|
|
||||||
.. Forwarded: for="1.2.3.4"; proto="https"; host="yoursite.com"; secret="Pr0xy", for="10.0.0.1"; proto="http"; host="proxy.internal"; by="_1234proxy"
|
|
||||||
|
|
||||||
* Set `FORWARDED_SECRET` to an identifier used by the proxy of interest.
|
|
||||||
|
|
||||||
The secret is used to securely identify a specific proxy server. Given the above header, secret `Pr0xy` would use the
|
|
||||||
information on the first line and secret `_1234proxy` would use the second line. The secret must exactly match the value
|
|
||||||
of `secret` or `by`. A secret in `by` must begin with an underscore and use only characters specified in
|
|
||||||
`RFC 7239 section 6.3 <https://tools.ietf.org/html/rfc7239#section-6.3>`_, while `secret` has no such restrictions.
|
|
||||||
|
|
||||||
Sanic ignores any elements without the secret key, and will not even parse the header if no secret is set.
|
|
||||||
|
|
||||||
All other proxy headers are ignored once a trusted forwarded element is found, as it already carries complete information about the client.
|
|
||||||
|
|
||||||
Traditional proxy headers
|
|
||||||
#########################
|
|
||||||
|
|
||||||
.. X-Real-IP: 1.2.3.4
|
|
||||||
X-Forwarded-For: 1.2.3.4, 10.0.0.1
|
|
||||||
X-Forwarded-Proto: https
|
|
||||||
X-Forwarded-Host: yoursite.com
|
|
||||||
|
|
||||||
|
|
||||||
* Set `REAL_IP_HEADER` to `x-real-ip`, `true-client-ip`, `cf-connecting-ip` or other name of such header.
|
|
||||||
* Set `PROXIES_COUNT` to the number of entries expected in `x-forwarded-for` (name configurable via `FORWARDED_FOR_HEADER`).
|
|
||||||
|
|
||||||
If client IP is found by one of these methods, Sanic uses the following headers for URL parts:
|
|
||||||
|
|
||||||
* `x-forwarded-proto`, `x-forwarded-host`, `x-forwarded-port`, `x-forwarded-path` and if necessary, `x-scheme`.
|
|
||||||
|
|
||||||
Proxy config if using ...
|
|
||||||
#########################
|
|
||||||
|
|
||||||
* a proxy that supports `forwarded`: set `FORWARDED_SECRET` to the value that the proxy inserts in the header
|
|
||||||
* Apache Traffic Server: `CONFIG proxy.config.http.insert_forwarded STRING for|proto|host|by=_secret`
|
|
||||||
* NGHTTPX: `nghttpx --add-forwarded=for,proto,host,by --forwarded-for=ip --forwarded-by=_secret`
|
|
||||||
* NGINX: :ref:`nginx`.
|
|
||||||
|
|
||||||
* a custom header with client IP: set `REAL_IP_HEADER` to the name of that header
|
|
||||||
* `x-forwarded-for`: set `PROXIES_COUNT` to `1` for a single proxy, or a greater number to allow Sanic to select the correct IP
|
|
||||||
* no proxies: no configuration required!
|
|
||||||
|
|
||||||
Changes in Sanic 19.9
|
|
||||||
#####################
|
|
||||||
|
|
||||||
Earlier Sanic versions had unsafe default settings. From 19.9 onwards proxy settings must be set manually, and support for negative PROXIES_COUNT has been removed.
|
|
|
@ -1 +1,4 @@
|
||||||
|
♥️ Contributing
|
||||||
|
===============
|
||||||
|
|
||||||
.. include:: ../../CONTRIBUTING.rst
|
.. include:: ../../CONTRIBUTING.rst
|
||||||
|
|
|
@ -1,87 +0,0 @@
|
||||||
Cookies
|
|
||||||
=======
|
|
||||||
|
|
||||||
Cookies are pieces of data which persist inside a user's browser. Sanic can
|
|
||||||
both read and write cookies, which are stored as key-value pairs.
|
|
||||||
|
|
||||||
.. warning::
|
|
||||||
|
|
||||||
Cookies can be freely altered by the client. Therefore you cannot just store
|
|
||||||
data such as login information in cookies as-is, as they can be freely altered
|
|
||||||
by the client. To ensure data you store in cookies is not forged or tampered
|
|
||||||
with by the client, use something like `itsdangerous`_ to cryptographically
|
|
||||||
sign the data.
|
|
||||||
|
|
||||||
|
|
||||||
Reading cookies
|
|
||||||
---------------
|
|
||||||
|
|
||||||
A user's cookies can be accessed via the ``Request`` object's ``cookies`` dictionary.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
@app.route("/cookie")
|
|
||||||
async def test(request):
|
|
||||||
test_cookie = request.cookies.get('test')
|
|
||||||
return text("Test cookie set to: {}".format(test_cookie))
|
|
||||||
|
|
||||||
Writing cookies
|
|
||||||
---------------
|
|
||||||
|
|
||||||
When returning a response, cookies can be set on the ``Response`` object.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
@app.route("/cookie")
|
|
||||||
async def test(request):
|
|
||||||
response = text("There's a cookie up in this response")
|
|
||||||
response.cookies['test'] = 'It worked!'
|
|
||||||
response.cookies['test']['domain'] = '.gotta-go-fast.com'
|
|
||||||
response.cookies['test']['httponly'] = True
|
|
||||||
return response
|
|
||||||
|
|
||||||
Deleting cookies
|
|
||||||
----------------
|
|
||||||
|
|
||||||
Cookies can be removed semantically or explicitly.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
@app.route("/cookie")
|
|
||||||
async def test(request):
|
|
||||||
response = text("Time to eat some cookies muahaha")
|
|
||||||
|
|
||||||
# This cookie will be set to expire in 0 seconds
|
|
||||||
del response.cookies['kill_me']
|
|
||||||
|
|
||||||
# This cookie will self destruct in 5 seconds
|
|
||||||
response.cookies['short_life'] = 'Glad to be here'
|
|
||||||
response.cookies['short_life']['max-age'] = 5
|
|
||||||
del response.cookies['favorite_color']
|
|
||||||
|
|
||||||
# This cookie will remain unchanged
|
|
||||||
response.cookies['favorite_color'] = 'blue'
|
|
||||||
response.cookies['favorite_color'] = 'pink'
|
|
||||||
del response.cookies['favorite_color']
|
|
||||||
|
|
||||||
return response
|
|
||||||
|
|
||||||
Response cookies can be set like dictionary values and have the following
|
|
||||||
parameters available:
|
|
||||||
|
|
||||||
- ``expires`` (datetime): The time for the cookie to expire on the client's browser.
|
|
||||||
- ``path`` (string): The subset of URLs to which this cookie applies. Defaults to /.
|
|
||||||
- ``comment`` (string): A comment (metadata).
|
|
||||||
- ``domain`` (string): Specifies the domain for which the cookie is valid. An
|
|
||||||
explicitly specified domain must always start with a dot.
|
|
||||||
- ``max-age`` (number): Number of seconds the cookie should live for.
|
|
||||||
- ``secure`` (boolean): Specifies whether the cookie will only be sent via HTTPS.
|
|
||||||
- ``httponly`` (boolean): Specifies whether the cookie cannot be read by Javascript.
|
|
||||||
|
|
||||||
.. _itsdangerous: https://pythonhosted.org/itsdangerous/
|
|
|
@ -1,76 +0,0 @@
|
||||||
Custom Protocols
|
|
||||||
================
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
This is advanced usage, and most readers will not need such functionality.
|
|
||||||
|
|
||||||
You can change the behavior of Sanic's protocol by specifying a custom
|
|
||||||
protocol, which should be a subclass
|
|
||||||
of `asyncio.protocol <https://docs.python.org/3/library/asyncio-protocol.html#protocol-classes>`_.
|
|
||||||
This protocol can then be passed as the keyword argument ``protocol`` to the ``sanic.run`` method.
|
|
||||||
|
|
||||||
The constructor of the custom protocol class receives the following keyword
|
|
||||||
arguments from Sanic.
|
|
||||||
|
|
||||||
- ``loop``: an ``asyncio``-compatible event loop.
|
|
||||||
- ``connections``: a ``set`` to store protocol objects. When Sanic receives
|
|
||||||
``SIGINT`` or ``SIGTERM``, it executes ``protocol.close_if_idle`` for all protocol
|
|
||||||
objects stored in this set.
|
|
||||||
- ``signal``: a ``sanic.server.Signal`` object with the ``stopped`` attribute. When
|
|
||||||
Sanic receives ``SIGINT`` or ``SIGTERM``, ``signal.stopped`` is assigned ``True``.
|
|
||||||
- ``request_handler``: a coroutine that takes a ``sanic.request.Request`` object
|
|
||||||
and a ``response`` callback as arguments.
|
|
||||||
- ``error_handler``: a ``sanic.exceptions.Handler`` which is called when exceptions
|
|
||||||
are raised.
|
|
||||||
- ``request_timeout``: the number of seconds before a request times out.
|
|
||||||
- ``request_max_size``: an integer specifying the maximum size of a request, in bytes.
|
|
||||||
|
|
||||||
Example
|
|
||||||
-------
|
|
||||||
|
|
||||||
An error occurs in the default protocol if a handler function does not return
|
|
||||||
an ``HTTPResponse`` object.
|
|
||||||
|
|
||||||
By overriding the ``write_response`` protocol method, if a handler returns a
|
|
||||||
string it will be converted to an ``HTTPResponse object``.
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.server import HttpProtocol
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class CustomHttpProtocol(HttpProtocol):
|
|
||||||
|
|
||||||
def __init__(self, *, loop, request_handler, error_handler,
|
|
||||||
signal, connections, request_timeout, request_max_size):
|
|
||||||
super().__init__(
|
|
||||||
loop=loop, request_handler=request_handler,
|
|
||||||
error_handler=error_handler, signal=signal,
|
|
||||||
connections=connections, request_timeout=request_timeout,
|
|
||||||
request_max_size=request_max_size)
|
|
||||||
|
|
||||||
def write_response(self, response):
|
|
||||||
if isinstance(response, str):
|
|
||||||
response = text(response)
|
|
||||||
self.transport.write(
|
|
||||||
response.output(self.request.version)
|
|
||||||
)
|
|
||||||
self.transport.close()
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
async def string(request):
|
|
||||||
return 'string'
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/1')
|
|
||||||
async def response(request):
|
|
||||||
return text('response')
|
|
||||||
|
|
||||||
app.run(host='0.0.0.0', port=8000, protocol=CustomHttpProtocol)
|
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
Debug Mode
|
|
||||||
=============
|
|
||||||
|
|
||||||
When enabling Sanic's debug mode, Sanic will provide a more verbose logging output
|
|
||||||
and by default will enable the Auto Reload feature.
|
|
||||||
|
|
||||||
.. warning::
|
|
||||||
|
|
||||||
Sanic's debug more will slow down the server's performance
|
|
||||||
and is therefore advised to enable it only in development environments.
|
|
||||||
|
|
||||||
|
|
||||||
Setting the debug mode
|
|
||||||
----------------------
|
|
||||||
|
|
||||||
By setting the ``debug`` mode a more verbose output from Sanic will be output
|
|
||||||
and the Automatic Reloader will be activated.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
async def hello_world(request):
|
|
||||||
return json({"hello": "world"})
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run(host="0.0.0.0", port=8000, debug=True)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Manually setting auto reload
|
|
||||||
----------------------------
|
|
||||||
|
|
||||||
Sanic offers a way to enable or disable the Automatic Reloader manually,
|
|
||||||
the ``auto_reload`` argument will activate or deactivate the Automatic Reloader.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
async def hello_world(request):
|
|
||||||
return json({"hello": "world"})
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run(host="0.0.0.0", port=8000, auto_reload=True)
|
|
|
@ -1,40 +0,0 @@
|
||||||
Handler Decorators
|
|
||||||
==================
|
|
||||||
|
|
||||||
Since Sanic handlers are simple Python functions, you can apply decorators to them in a similar manner to Flask. A typical use case is when you want some code to run before a handler's code is executed.
|
|
||||||
|
|
||||||
Authorization Decorator
|
|
||||||
-----------------------
|
|
||||||
|
|
||||||
Let's say you want to check that a user is authorized to access a particular endpoint. You can create a decorator that wraps a handler function, checks a request if the client is authorized to access a resource, and sends the appropriate response.
|
|
||||||
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from functools import wraps
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
def authorized():
|
|
||||||
def decorator(f):
|
|
||||||
@wraps(f)
|
|
||||||
async def decorated_function(request, *args, **kwargs):
|
|
||||||
# run some method that checks the request
|
|
||||||
# for the client's authorization status
|
|
||||||
is_authorized = check_request_for_authorization_status(request)
|
|
||||||
|
|
||||||
if is_authorized:
|
|
||||||
# the user is authorized.
|
|
||||||
# run the handler method and return the response
|
|
||||||
response = await f(request, *args, **kwargs)
|
|
||||||
return response
|
|
||||||
else:
|
|
||||||
# the user is not authorized.
|
|
||||||
return json({'status': 'not_authorized'}, 403)
|
|
||||||
return decorated_function
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
@authorized()
|
|
||||||
async def test(request):
|
|
||||||
return json({'status': 'authorized'})
|
|
|
@ -1,195 +0,0 @@
|
||||||
Deploying
|
|
||||||
=========
|
|
||||||
|
|
||||||
Sanic has three serving options: the inbuilt webserver,
|
|
||||||
an `ASGI webserver <https://asgi.readthedocs.io/en/latest/implementations.html>`_, or `gunicorn`.
|
|
||||||
|
|
||||||
Sanic's own webserver is the fastest option, and it can be securely run on
|
|
||||||
the Internet. Still, it is also very common to place Sanic behind a reverse
|
|
||||||
proxy, as shown in :ref:`nginx`.
|
|
||||||
|
|
||||||
Running via Sanic webserver
|
|
||||||
---------------------------
|
|
||||||
|
|
||||||
After defining an instance of `sanic.Sanic`, we can call the `run` method with the following
|
|
||||||
keyword arguments:
|
|
||||||
|
|
||||||
- `host` *(default `"127.0.0.1"`)*: Address to host the server on.
|
|
||||||
- `port` *(default `8000`)*: Port to host the server on.
|
|
||||||
- `unix` *(default `None`)*: Unix socket name to host the server on (instead of TCP).
|
|
||||||
- `debug` *(default `False`)*: Enables debug output (slows server).
|
|
||||||
- `ssl` *(default `None`)*: `SSLContext` for SSL encryption of worker(s).
|
|
||||||
- `sock` *(default `None`)*: Socket for the server to accept connections from.
|
|
||||||
- `workers` *(default `1`)*: Number of worker processes to spawn.
|
|
||||||
- `loop` *(default `None`)*: An `asyncio`-compatible event loop. If none is specified, Sanic creates its own event loop.
|
|
||||||
- `protocol` *(default `HttpProtocol`)*: Subclass of `asyncio.protocol <https://docs.python.org/3/library/asyncio-protocol.html#protocol-classes>`_.
|
|
||||||
- `access_log` *(default `True`)*: Enables log on handling requests (significantly slows server).
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app.run(host='0.0.0.0', port=1337, access_log=False)
|
|
||||||
|
|
||||||
In the above example, we decided to turn off the access log in order to increase performance.
|
|
||||||
|
|
||||||
Workers
|
|
||||||
~~~~~~~
|
|
||||||
|
|
||||||
By default, Sanic listens in the main process using only one CPU core. To crank
|
|
||||||
up the juice, just specify the number of workers in the `run` arguments.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app.run(host='0.0.0.0', port=1337, workers=4)
|
|
||||||
|
|
||||||
Sanic will automatically spin up multiple processes and route traffic between
|
|
||||||
them. We recommend as many workers as you have available cores.
|
|
||||||
|
|
||||||
Running via command
|
|
||||||
~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
If you like using command line arguments, you can launch a Sanic webserver by
|
|
||||||
executing the module. For example, if you initialized Sanic as `app` in a file
|
|
||||||
named `server.py`, you could run the server like so:
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
sanic server.app --host=0.0.0.0 --port=1337 --workers=4
|
|
||||||
|
|
||||||
It can also be called directly as a module.
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
python -m sanic server.app --host=0.0.0.0 --port=1337 --workers=4
|
|
||||||
|
|
||||||
With this way of running sanic, it is not necessary to invoke `app.run` in your
|
|
||||||
Python file. If you do, make sure you wrap it so that it only executes when
|
|
||||||
directly run by the interpreter.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run(host='0.0.0.0', port=1337, workers=4)
|
|
||||||
|
|
||||||
Running via ASGI
|
|
||||||
----------------
|
|
||||||
|
|
||||||
Sanic is also ASGI-compliant. This means you can use your preferred ASGI webserver
|
|
||||||
to run Sanic. The three main implementations of ASGI are
|
|
||||||
`Daphne <http://github.com/django/daphne>`_, `Uvicorn <https://www.uvicorn.org/>`_,
|
|
||||||
and `Hypercorn <https://pgjones.gitlab.io/hypercorn/index.html>`_.
|
|
||||||
|
|
||||||
Follow their documentation for the proper way to run them, but it should look
|
|
||||||
something like:
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
daphne myapp:app
|
|
||||||
uvicorn myapp:app
|
|
||||||
hypercorn myapp:app
|
|
||||||
|
|
||||||
A couple things to note when using ASGI:
|
|
||||||
|
|
||||||
1. When using the Sanic webserver, websockets will run using the `websockets <https://websockets.readthedocs.io/>`_ package.
|
|
||||||
In ASGI mode, there is no need for this package since websockets are managed in the ASGI server.
|
|
||||||
2. The ASGI `lifespan protocol <https://asgi.readthedocs.io/en/latest/specs/lifespan.html>`, supports
|
|
||||||
only two server events: startup and shutdown. Sanic has four: before startup, after startup,
|
|
||||||
before shutdown, and after shutdown. Therefore, in ASGI mode, the startup and shutdown events will
|
|
||||||
run consecutively and not actually around the server process beginning and ending (since that
|
|
||||||
is now controlled by the ASGI server). Therefore, it is best to use `after_server_start` and
|
|
||||||
`before_server_stop`.
|
|
||||||
|
|
||||||
Sanic has experimental support for running on `Trio <https://trio.readthedocs.io/en/stable/>`_ with::
|
|
||||||
|
|
||||||
hypercorn -k trio myapp:app
|
|
||||||
|
|
||||||
|
|
||||||
Running via Gunicorn
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
`Gunicorn <http://gunicorn.org/>`_ ‘Green Unicorn’ is a WSGI HTTP Server for UNIX.
|
|
||||||
It’s a pre-fork worker model ported from Ruby’s Unicorn project.
|
|
||||||
|
|
||||||
In order to run Sanic application with Gunicorn, you need to use the special `sanic.worker.GunicornWorker`
|
|
||||||
for Gunicorn `worker-class` argument:
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
gunicorn myapp:app --bind 0.0.0.0:1337 --worker-class sanic.worker.GunicornWorker
|
|
||||||
|
|
||||||
|
|
||||||
If your application suffers from memory leaks, you can configure Gunicorn to gracefully restart a worker
|
|
||||||
after it has processed a given number of requests. This can be a convenient way to help limit the effects
|
|
||||||
of the memory leak.
|
|
||||||
|
|
||||||
See the `Gunicorn Docs <http://docs.gunicorn.org/en/latest/settings.html#max-requests>`_ for more information.
|
|
||||||
|
|
||||||
Other deployment considerations
|
|
||||||
-------------------------------
|
|
||||||
|
|
||||||
Disable debug logging for performance
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
To improve the performance add `debug=False` and `access_log=False` in the `run` arguments.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app.run(host='0.0.0.0', port=1337, workers=4, debug=False, access_log=False)
|
|
||||||
|
|
||||||
Running via Gunicorn you can set Environment variable `SANIC_ACCESS_LOG="False"`
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
env SANIC_ACCESS_LOG="False" gunicorn myapp:app --bind 0.0.0.0:1337 --worker-class sanic.worker.GunicornWorker --log-level warning
|
|
||||||
|
|
||||||
Or you can rewrite app config directly
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app.config.ACCESS_LOG = False
|
|
||||||
|
|
||||||
Asynchronous support and sharing the loop
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
This is suitable if you *need* to share the Sanic process with other applications, in particular the `loop`.
|
|
||||||
However, be advised that this method does not support using multiple processes, and is not the preferred way
|
|
||||||
to run the app in general.
|
|
||||||
|
|
||||||
Here is an incomplete example (please see `run_async.py` in examples for something more practical):
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
server = app.create_server(host="0.0.0.0", port=8000, return_asyncio_server=True)
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
task = asyncio.ensure_future(server)
|
|
||||||
loop.run_forever()
|
|
||||||
|
|
||||||
Caveat: using this method, calling `app.create_server()` will trigger "before_server_start" server events, but not
|
|
||||||
"after_server_start", "before_server_stop", or "after_server_stop" server events.
|
|
||||||
|
|
||||||
For more advanced use-cases, you can trigger these events using the AsyncioServer object, returned by awaiting
|
|
||||||
the server task.
|
|
||||||
|
|
||||||
Here is an incomplete example (please see `run_async_advanced.py` in examples for something more complete):
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
serv_coro = app.create_server(host="0.0.0.0", port=8000, return_asyncio_server=True)
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
serv_task = asyncio.ensure_future(serv_coro, loop=loop)
|
|
||||||
server = loop.run_until_complete(serv_task)
|
|
||||||
server.after_start()
|
|
||||||
try:
|
|
||||||
loop.run_forever()
|
|
||||||
except KeyboardInterrupt as e:
|
|
||||||
loop.stop()
|
|
||||||
finally:
|
|
||||||
server.before_stop()
|
|
||||||
|
|
||||||
# Wait for server to close
|
|
||||||
close_task = server.close()
|
|
||||||
loop.run_until_complete(close_task)
|
|
||||||
|
|
||||||
# Complete all tasks on the loop
|
|
||||||
for connection in server.connections:
|
|
||||||
connection.close_if_idle()
|
|
||||||
server.after_stop()
|
|
|
@ -1,167 +0,0 @@
|
||||||
Examples
|
|
||||||
========
|
|
||||||
|
|
||||||
This section of the documentation is a simple collection of example code that can help you get a quick start
|
|
||||||
on your application development. Most of these examples are categorized and provide you with a link to the
|
|
||||||
working code example in the `Sanic Repository <https://github.com/huge-success/sanic/tree/master/examples>`_
|
|
||||||
|
|
||||||
|
|
||||||
Basic Examples
|
|
||||||
--------------
|
|
||||||
|
|
||||||
This section of the examples are a collection of code that provide a simple use case example of the sanic application.
|
|
||||||
|
|
||||||
Simple Apps
|
|
||||||
~~~~~~~~~~~~
|
|
||||||
|
|
||||||
A simple sanic application with a single ``async`` method with ``text`` and ``json`` type response.
|
|
||||||
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/teapot.py
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/simple_server.py
|
|
||||||
|
|
||||||
|
|
||||||
Simple App with ``Sanic Views``
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Showcasing the simple mechanism of using :class:`sanic.views.HTTPMethodView` as well as a way to extend the same
|
|
||||||
into providing a custom ``async`` behavior for ``view``.
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/simple_async_view.py
|
|
||||||
|
|
||||||
|
|
||||||
URL Redirect
|
|
||||||
~~~~~~~~~~~~
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/redirect_example.py
|
|
||||||
|
|
||||||
|
|
||||||
Named URL redirection
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
``Sanic`` provides an easy to use way of redirecting the requests via a helper method called ``url_for`` that takes a
|
|
||||||
unique url name as argument and returns you the actual route assigned for it. This will help in simplifying the
|
|
||||||
efforts required in redirecting the user between different section of the application.
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/url_for_example.py
|
|
||||||
|
|
||||||
Blueprints
|
|
||||||
~~~~~~~~~~
|
|
||||||
``Sanic`` provides an amazing feature to group your APIs and routes under a logical collection that can easily be
|
|
||||||
imported and plugged into any of your sanic application and it's called ``blueprints``
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/blueprints.py
|
|
||||||
|
|
||||||
Logging Enhancements
|
|
||||||
~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Even though ``Sanic`` comes with a battery of Logging support it allows the end users to customize the way logging
|
|
||||||
is handled in the application runtime.
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/override_logging.py
|
|
||||||
|
|
||||||
The following sample provides an example code that demonstrates the usage of :func:`sanic.app.Sanic.middleware` in order
|
|
||||||
to provide a mechanism to assign a unique request ID for each of the incoming requests and log them via
|
|
||||||
`aiotask-context <https://github.com/Skyscanner/aiotask-context>`_.
|
|
||||||
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/log_request_id.py
|
|
||||||
|
|
||||||
Sanic Streaming Support
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
``Sanic`` framework comes with in-built support for streaming large files and the following code explains the process
|
|
||||||
to setup a ``Sanic`` application with streaming support.
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/request_stream/server.py
|
|
||||||
|
|
||||||
Sample Client app to show the usage of streaming application by a client code.
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/request_stream/client.py
|
|
||||||
|
|
||||||
Sanic Concurrency Support
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
``Sanic`` supports the ability to start an app with multiple worker support. However, it's important to be able to limit
|
|
||||||
the concurrency per process/loop in order to ensure an efficient execution. The following section of the code provides a
|
|
||||||
brief example of how to limit the concurrency with the help of :class:`asyncio.Semaphore`
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/limit_concurrency.py
|
|
||||||
|
|
||||||
|
|
||||||
Sanic Deployment via Docker
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Deploying a ``sanic`` app via ``docker`` and ``docker-compose`` is an easy task to achieve and the following example
|
|
||||||
provides a deployment of the sample ``simple_server.py``
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/Dockerfile
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/docker-compose.yml
|
|
||||||
|
|
||||||
|
|
||||||
Monitoring and Error Handling
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
``Sanic`` provides an extendable bare minimum implementation of a global exception handler via
|
|
||||||
:class:`sanic.handlers.ErrorHandler`. This example shows how to extend it to enable some custom behaviors.
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/exception_monitoring.py
|
|
||||||
|
|
||||||
Monitoring using external Service Providers
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
* `LogDNA <https://logdna.com/>`_
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/logdna_example.py
|
|
||||||
|
|
||||||
* `RayGun <https://raygun.com/>`_
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/raygun_example.py
|
|
||||||
|
|
||||||
* `Rollbar <https://rollbar.com>`_
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/rollbar_example.py
|
|
||||||
|
|
||||||
* `Sentry <http://sentry.io>`_
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/sentry_example.py
|
|
||||||
|
|
||||||
|
|
||||||
Security
|
|
||||||
~~~~~~~~
|
|
||||||
|
|
||||||
The following sample code shows a simple decorator based authentication and authorization mechanism that can be setup
|
|
||||||
to secure your ``sanic`` api endpoints.
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/authorized_sanic.py
|
|
||||||
|
|
||||||
Sanic Websocket
|
|
||||||
~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
``Sanic`` provides an ability to easily add a route and map it to a ``websocket`` handlers.
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/websocket.html
|
|
||||||
.. literalinclude:: ../../examples/websocket.py
|
|
||||||
|
|
||||||
vhost Suppport
|
|
||||||
~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/vhosts.py
|
|
||||||
|
|
||||||
Unit Testing With Parallel Test Run Support
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
The following example shows you how to get up and running with unit testing ``sanic`` application with parallel test
|
|
||||||
execution support provided by the ``pytest-xdist`` plugin.
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/pytest_xdist.py
|
|
||||||
|
|
||||||
|
|
||||||
Amending Request Object
|
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
The ``request`` object in ``Sanic`` is a kind of ``dict`` object, this means that ``request`` object can be manipulated as a regular ``dict`` object.
|
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/amending_request_object.py
|
|
||||||
|
|
||||||
For more examples and useful samples please visit the `Huge-Sanic's GitHub Page <https://github.com/huge-success/sanic/tree/master/examples>`_
|
|
|
@ -1,92 +0,0 @@
|
||||||
Exceptions
|
|
||||||
==========
|
|
||||||
|
|
||||||
Exceptions can be thrown from within request handlers and will automatically be
|
|
||||||
handled by Sanic. Exceptions take a message as their first argument, and can
|
|
||||||
also take a status code to be passed back in the HTTP response.
|
|
||||||
|
|
||||||
Throwing an exception
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
To throw an exception, simply `raise` the relevant exception from the
|
|
||||||
`sanic.exceptions` module.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.exceptions import ServerError
|
|
||||||
|
|
||||||
@app.route('/killme')
|
|
||||||
async def i_am_ready_to_die(request):
|
|
||||||
raise ServerError("Something bad happened", status_code=500)
|
|
||||||
|
|
||||||
You can also use the `abort` function with the appropriate status code:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.exceptions import abort
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
@app.route('/youshallnotpass')
|
|
||||||
async def no_no(request):
|
|
||||||
abort(401)
|
|
||||||
# this won't happen
|
|
||||||
text("OK")
|
|
||||||
|
|
||||||
Handling exceptions
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
To override Sanic's default handling of an exception, the `@app.exception`
|
|
||||||
decorator is used. The decorator expects a list of exceptions to handle as
|
|
||||||
arguments. You can pass `SanicException` to catch them all! The decorated
|
|
||||||
exception handler function must take a `Request` and `Exception` object as
|
|
||||||
arguments.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import text
|
|
||||||
from sanic.exceptions import NotFound
|
|
||||||
|
|
||||||
@app.exception(NotFound)
|
|
||||||
async def ignore_404s(request, exception):
|
|
||||||
return text("Yep, I totally found the page: {}".format(request.url))
|
|
||||||
|
|
||||||
You can also add an exception handler as such:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
|
|
||||||
async def server_error_handler(request, exception):
|
|
||||||
return text("Oops, server error", status=500)
|
|
||||||
|
|
||||||
app = Sanic("error_handler_example")
|
|
||||||
app.error_handler.add(Exception, server_error_handler)
|
|
||||||
|
|
||||||
In some cases, you might want to add some more error handling
|
|
||||||
functionality to what is provided by default. In that case, you
|
|
||||||
can subclass Sanic's default error handler as such:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.handlers import ErrorHandler
|
|
||||||
|
|
||||||
class CustomErrorHandler(ErrorHandler):
|
|
||||||
def default(self, request, exception):
|
|
||||||
''' handles errors that have no error handlers assigned '''
|
|
||||||
# You custom error handling logic...
|
|
||||||
return super().default(request, exception)
|
|
||||||
|
|
||||||
app = Sanic("custom_error_handler_example")
|
|
||||||
app.error_handler = CustomErrorHandler()
|
|
||||||
|
|
||||||
Useful exceptions
|
|
||||||
-----------------
|
|
||||||
|
|
||||||
Some of the most useful exceptions are presented below:
|
|
||||||
|
|
||||||
- `NotFound`: called when a suitable route for the request isn't found.
|
|
||||||
- `ServerError`: called when something goes wrong inside the server. This
|
|
||||||
usually occurs if there is an exception raised in user code.
|
|
||||||
|
|
||||||
See the `sanic.exceptions` module for the full list of exceptions to throw.
|
|
|
@ -1,4 +0,0 @@
|
||||||
Extensions
|
|
||||||
==========
|
|
||||||
|
|
||||||
Moved to the `awesome-sanic <https://github.com/mekicha/awesome-sanic>`_ list.
|
|
|
@ -1,85 +0,0 @@
|
||||||
Getting Started
|
|
||||||
===============
|
|
||||||
|
|
||||||
Make sure you have both `pip <https://pip.pypa.io/en/stable/installing/>`_ and at
|
|
||||||
least version 3.6 of Python before starting. Sanic uses the new `async`/`await`
|
|
||||||
syntax, so earlier versions of python won't work.
|
|
||||||
|
|
||||||
1. Install Sanic
|
|
||||||
----------------
|
|
||||||
|
|
||||||
If you are running on a clean install of Fedora 28 or above, please make sure you have the ``redhat-rpm-config`` package installed in case if you want to use ``sanic`` with ``ujson`` dependency.
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
pip3 install sanic
|
|
||||||
|
|
||||||
To install sanic without `uvloop` or `ujson` using bash, you can provide either or both of these environmental variables
|
|
||||||
using any truthy string like `'y', 'yes', 't', 'true', 'on', '1'` and setting the `SANIC_NO_X` ( with`X` = `UVLOOP`/`UJSON`)
|
|
||||||
to true will stop that features installation.
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
SANIC_NO_UVLOOP=true SANIC_NO_UJSON=true pip3 install --no-binary :all: sanic
|
|
||||||
|
|
||||||
You can also install Sanic from `conda-forge <https://anaconda.org/conda-forge/sanic>`_
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
conda config --add channels conda-forge
|
|
||||||
conda install sanic
|
|
||||||
|
|
||||||
2. Create a file called `main.py`
|
|
||||||
---------------------------------
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
app = Sanic("hello_example")
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
async def test(request):
|
|
||||||
return json({"hello": "world"})
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run(host="0.0.0.0", port=8000)
|
|
||||||
|
|
||||||
3. Run the server
|
|
||||||
-----------------
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
python3 main.py
|
|
||||||
|
|
||||||
4. Check your browser
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
Open the address `http://0.0.0.0:8000 <http://0.0.0.0:8000>`_ in your web browser. You should see
|
|
||||||
the message *Hello world!*.
|
|
||||||
|
|
||||||
You now have a working Sanic server!
|
|
||||||
|
|
||||||
5. Application registry
|
|
||||||
-----------------------
|
|
||||||
|
|
||||||
When you instantiate a Sanic instance, that can be retrieved at a later time from the Sanic app registry. This can be useful, for example, if you need to access your Sanic instance from a location where it is not otherwise accessible.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# ./path/to/server.py
|
|
||||||
from sanic import Sanic
|
|
||||||
|
|
||||||
app = Sanic("my_awesome_server")
|
|
||||||
|
|
||||||
# ./path/to/somewhere_else.py
|
|
||||||
from sanic import Sanic
|
|
||||||
|
|
||||||
app = Sanic.get_app("my_awesome_server")
|
|
||||||
|
|
||||||
If you call ``Sanic.get_app("non-existing")`` on an app that does not exist, it will raise ``SanicException`` by default. You can, instead, force the method to return a new instance of ``Sanic`` with that name:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app = Sanic.get_app("my_awesome_server", force_create=True)
|
|
|
@ -1,30 +0,0 @@
|
||||||
Sanic
|
|
||||||
=================================
|
|
||||||
|
|
||||||
Sanic is a Python 3.6+ web server and web framework that's written to go fast. It allows the usage of the async/await syntax added in Python 3.5, which makes your code non-blocking and speedy.
|
|
||||||
|
|
||||||
The goal of the project is to provide a simple way to get up and running a highly performant HTTP server that is easy to build, to expand, and ultimately to scale.
|
|
||||||
|
|
||||||
Sanic is developed `on GitHub <https://github.com/channelcat/sanic/>`_. Contributions are welcome!
|
|
||||||
|
|
||||||
Sanic aspires to be simple
|
|
||||||
---------------------------
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
app = Sanic("App Name")
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
async def test(request):
|
|
||||||
return json({"hello": "world"})
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run(host="0.0.0.0", port=8000)
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
Sanic does not support Python 3.5 from version 19.6 and forward. However, version 18.12LTS is supported thru
|
|
||||||
December 2020. Official Python support for version 3.5 is set to expire in September 2020.
|
|
|
@ -1,103 +0,0 @@
|
||||||
Logging
|
|
||||||
=======
|
|
||||||
|
|
||||||
Sanic allows you to do different types of logging (access log, error
|
|
||||||
log) on the requests based on the `python3 logging API`_. You should
|
|
||||||
have some basic knowledge on python3 logging if you want to create a new
|
|
||||||
configuration.
|
|
||||||
|
|
||||||
Quick Start
|
|
||||||
~~~~~~~~~~~
|
|
||||||
|
|
||||||
A simple example using default settings would be like this:
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.log import logger
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
app = Sanic('logging_example')
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
async def test(request):
|
|
||||||
logger.info('Here is your log')
|
|
||||||
return text('Hello World!')
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run(debug=True, access_log=True)
|
|
||||||
|
|
||||||
After the server is running, you can see some messages looks like:
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
[2018-11-06 21:16:53 +0800] [24622] [INFO] Goin' Fast @ http://127.0.0.1:8000
|
|
||||||
[2018-11-06 21:16:53 +0800] [24667] [INFO] Starting worker [24667]
|
|
||||||
|
|
||||||
You can send a request to server and it will print the log messages:
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
[2018-11-06 21:18:53 +0800] [25685] [INFO] Here is your log
|
|
||||||
[2018-11-06 21:18:53 +0800] - (sanic.access)[INFO][127.0.0.1:57038]: GET http://localhost:8000/ 200 12
|
|
||||||
|
|
||||||
To use your own logging config, simply use
|
|
||||||
``logging.config.dictConfig``, or pass ``log_config`` when you
|
|
||||||
initialize ``Sanic`` app:
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
app = Sanic('logging_example', log_config=LOGGING_CONFIG)
|
|
||||||
|
|
||||||
And to close logging, simply assign access_log=False:
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run(access_log=False)
|
|
||||||
|
|
||||||
This would skip calling logging functions when handling requests. And
|
|
||||||
you could even do further in production to gain extra speed:
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
# disable debug messages
|
|
||||||
app.run(debug=False, access_log=False)
|
|
||||||
|
|
||||||
Configuration
|
|
||||||
~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
By default, ``log_config`` parameter is set to use
|
|
||||||
``sanic.log.LOGGING_CONFIG_DEFAULTS`` dictionary for configuration.
|
|
||||||
|
|
||||||
There are three ``loggers`` used in sanic, and **must be defined if you
|
|
||||||
want to create your own logging configuration**:
|
|
||||||
|
|
||||||
================ ==============================
|
|
||||||
Logger Name Usecase
|
|
||||||
================ ==============================
|
|
||||||
``sanic.root`` Used to log internal messages.
|
|
||||||
``sanic.error`` Used to log error logs.
|
|
||||||
``sanic.access`` Used to log access logs.
|
|
||||||
================ ==============================
|
|
||||||
|
|
||||||
Log format:
|
|
||||||
^^^^^^^^^^^
|
|
||||||
|
|
||||||
In addition to default parameters provided by python (``asctime``,
|
|
||||||
``levelname``, ``message``), Sanic provides additional parameters for
|
|
||||||
access logger with:
|
|
||||||
|
|
||||||
===================== ========================================== ========
|
|
||||||
Log Context Parameter Parameter Value Datatype
|
|
||||||
===================== ========================================== ========
|
|
||||||
``host`` ``request.ip`` str
|
|
||||||
``request`` ``request.method`` + " " + ``request.url`` str
|
|
||||||
``status`` ``response.status`` int
|
|
||||||
``byte`` ``len(response.body)`` int
|
|
||||||
===================== ========================================== ========
|
|
||||||
|
|
||||||
The default access log format is ``%(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: %(request)s %(message)s %(status)d %(byte)d``
|
|
||||||
|
|
||||||
.. _python3 logging API: https://docs.python.org/3/howto/logging.html
|
|
|
@ -1,188 +0,0 @@
|
||||||
Middleware And Listeners
|
|
||||||
========================
|
|
||||||
|
|
||||||
Middleware are functions which are executed before or after requests to the
|
|
||||||
server. They can be used to modify the *request to* or *response from*
|
|
||||||
user-defined handler functions.
|
|
||||||
|
|
||||||
Additionally, Sanic provides listeners which allow you to run code at various points of your application's lifecycle.
|
|
||||||
|
|
||||||
Middleware
|
|
||||||
----------
|
|
||||||
|
|
||||||
There are two types of middleware: request and response. Both are declared
|
|
||||||
using the `@app.middleware` decorator, with the decorator's parameter being a
|
|
||||||
string representing its type: `'request'` or `'response'`.
|
|
||||||
|
|
||||||
* Request middleware receives only the `request` as an argument and are executed in the order they were added.
|
|
||||||
* Response middleware receives both the `request` and `response` and are executed in *reverse* order.
|
|
||||||
|
|
||||||
The simplest middleware doesn't modify the request or response at all:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@app.middleware('request')
|
|
||||||
async def print_on_request(request):
|
|
||||||
print("I print when a request is received by the server")
|
|
||||||
|
|
||||||
@app.middleware('response')
|
|
||||||
async def print_on_response(request, response):
|
|
||||||
print("I print when a response is returned by the server")
|
|
||||||
|
|
||||||
Modifying the request or response
|
|
||||||
---------------------------------
|
|
||||||
|
|
||||||
Middleware can modify the request or response parameter it is given, *as long
|
|
||||||
as it does not return it*. The following example shows a practical use-case for
|
|
||||||
this.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
@app.middleware('request')
|
|
||||||
async def add_key(request):
|
|
||||||
# Arbitrary data may be stored in request context:
|
|
||||||
request.ctx.foo = 'bar'
|
|
||||||
|
|
||||||
|
|
||||||
@app.middleware('response')
|
|
||||||
async def custom_banner(request, response):
|
|
||||||
response.headers["Server"] = "Fake-Server"
|
|
||||||
|
|
||||||
|
|
||||||
@app.middleware('response')
|
|
||||||
async def prevent_xss(request, response):
|
|
||||||
response.headers["x-xss-protection"] = "1; mode=block"
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
|
||||||
async def index(request):
|
|
||||||
return sanic.response.text(request.ctx.foo)
|
|
||||||
|
|
||||||
|
|
||||||
app.run(host="0.0.0.0", port=8000)
|
|
||||||
|
|
||||||
The three middlewares are executed in the following order:
|
|
||||||
|
|
||||||
1. The first request middleware **add_key** adds a new key `foo` into request context.
|
|
||||||
2. Request is routed to handler **index**, which gets the key from context and returns a text response.
|
|
||||||
3. The second response middleware **prevent_xss** adds the HTTP header for preventing Cross-Site-Scripting (XSS) attacks.
|
|
||||||
4. The first response middleware **custom_banner** changes the HTTP response header *Server* to say *Fake-Server*
|
|
||||||
|
|
||||||
Responding early
|
|
||||||
----------------
|
|
||||||
|
|
||||||
If middleware returns a `HTTPResponse` object, the request will stop processing
|
|
||||||
and the response will be returned. If this occurs to a request before the
|
|
||||||
relevant user route handler is reached, the handler will never be called.
|
|
||||||
Returning a response will also prevent any further middleware from running.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@app.middleware('request')
|
|
||||||
async def halt_request(request):
|
|
||||||
return text('I halted the request')
|
|
||||||
|
|
||||||
@app.middleware('response')
|
|
||||||
async def halt_response(request, response):
|
|
||||||
return text('I halted the response')
|
|
||||||
|
|
||||||
Custom context
|
|
||||||
--------------
|
|
||||||
|
|
||||||
Arbitrary data may be stored in `request.ctx`. A typical use case
|
|
||||||
would be to store the user object acquired from database in an authentication
|
|
||||||
middleware. Keys added are accessible to all later middleware as well as
|
|
||||||
the handler over the duration of the request.
|
|
||||||
|
|
||||||
Custom context is reserved for applications and extensions. Sanic itself makes
|
|
||||||
no use of it.
|
|
||||||
|
|
||||||
Listeners
|
|
||||||
---------
|
|
||||||
|
|
||||||
If you want to execute startup/teardown code as your server starts or closes, you can use the following listeners:
|
|
||||||
|
|
||||||
- `before_server_start`
|
|
||||||
- `after_server_start`
|
|
||||||
- `before_server_stop`
|
|
||||||
- `after_server_stop`
|
|
||||||
|
|
||||||
These listeners are implemented as decorators on functions which accept the app object as well as the asyncio loop.
|
|
||||||
|
|
||||||
For example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@app.listener('before_server_start')
|
|
||||||
async def setup_db(app, loop):
|
|
||||||
app.db = await db_setup()
|
|
||||||
|
|
||||||
@app.listener('after_server_start')
|
|
||||||
async def notify_server_started(app, loop):
|
|
||||||
print('Server successfully started!')
|
|
||||||
|
|
||||||
@app.listener('before_server_stop')
|
|
||||||
async def notify_server_stopping(app, loop):
|
|
||||||
print('Server shutting down!')
|
|
||||||
|
|
||||||
@app.listener('after_server_stop')
|
|
||||||
async def close_db(app, loop):
|
|
||||||
await app.db.close()
|
|
||||||
|
|
||||||
Note:
|
|
||||||
|
|
||||||
The listeners are deconstructed in the reverse order of being constructed.
|
|
||||||
|
|
||||||
For example:
|
|
||||||
|
|
||||||
If the first listener in before_server_start handler setups a database connection,
|
|
||||||
ones registered after it can rely on that connection being alive both when they are started
|
|
||||||
and stopped, because stopping is done in reverse order, and the database connection is
|
|
||||||
torn down last.
|
|
||||||
|
|
||||||
It's also possible to register a listener using the `register_listener` method.
|
|
||||||
This may be useful if you define your listeners in another module besides
|
|
||||||
the one you instantiate your app in.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
async def setup_db(app, loop):
|
|
||||||
app.db = await db_setup()
|
|
||||||
|
|
||||||
app.register_listener(setup_db, 'before_server_start')
|
|
||||||
|
|
||||||
If you want to schedule a background task to run after the loop has started,
|
|
||||||
Sanic provides the `add_task` method to easily do so.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
async def notify_server_started_after_five_seconds():
|
|
||||||
await asyncio.sleep(5)
|
|
||||||
print('Server successfully started!')
|
|
||||||
|
|
||||||
app.add_task(notify_server_started_after_five_seconds())
|
|
||||||
|
|
||||||
Sanic will attempt to automatically inject the app, passing it as an argument to the task:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
async def notify_server_started_after_five_seconds(app):
|
|
||||||
await asyncio.sleep(5)
|
|
||||||
print(app.name)
|
|
||||||
|
|
||||||
app.add_task(notify_server_started_after_five_seconds)
|
|
||||||
|
|
||||||
Or you can pass the app explicitly for the same effect:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
async def notify_server_started_after_five_seconds(app):
|
|
||||||
await asyncio.sleep(5)
|
|
||||||
print(app.name)
|
|
||||||
|
|
||||||
app.add_task(notify_server_started_after_five_seconds(app))
|
|
|
@ -1,222 +0,0 @@
|
||||||
|
|
||||||
.. _nginx:
|
|
||||||
|
|
||||||
Nginx Deployment
|
|
||||||
================
|
|
||||||
|
|
||||||
Introduction
|
|
||||||
~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Although Sanic can be run directly on Internet, it may be useful to use a proxy
|
|
||||||
server such as Nginx in front of it. This is particularly useful for running
|
|
||||||
multiple virtual hosts on the same IP, serving NodeJS or other services beside
|
|
||||||
a single Sanic app, and it also allows for efficient serving of static files.
|
|
||||||
SSL and HTTP/2 are also easily implemented on such proxy.
|
|
||||||
|
|
||||||
We are setting the Sanic app to serve only locally at `127.0.0.1:8000`, while the
|
|
||||||
Nginx installation is responsible for providing the service to public Internet
|
|
||||||
on domain `example.com`. Static files will be served from `/var/www/`.
|
|
||||||
|
|
||||||
|
|
||||||
Proxied Sanic app
|
|
||||||
~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
The app needs to be setup with a secret key used to identify a trusted proxy,
|
|
||||||
so that real client IP and other information can be identified. This protects
|
|
||||||
against anyone on the Internet sending fake headers to spoof their IP addresses
|
|
||||||
and other details. Choose any random string and configure it both on the app
|
|
||||||
and in Nginx config.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
app = Sanic("proxied_example")
|
|
||||||
app.config.FORWARDED_SECRET = "YOUR SECRET"
|
|
||||||
|
|
||||||
@app.get("/")
|
|
||||||
def index(request):
|
|
||||||
# This should display external (public) addresses:
|
|
||||||
return text(
|
|
||||||
f"{request.remote_addr} connected to {request.url_for('index')}\n"
|
|
||||||
f"Forwarded: {request.forwarded}\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run(host='127.0.0.1', port=8000, workers=8, access_log=False)
|
|
||||||
|
|
||||||
Since this is going to be a system service, save your code to
|
|
||||||
`/srv/sanicexample/sanicexample.py`.
|
|
||||||
|
|
||||||
For testing, run your app in a terminal.
|
|
||||||
|
|
||||||
|
|
||||||
Nginx configuration
|
|
||||||
~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Quite much configuration is required to allow fast transparent proxying, but
|
|
||||||
for the most part these don't need to be modified, so bear with me.
|
|
||||||
|
|
||||||
Upstream servers need to be configured in a separate `upstream` block to enable
|
|
||||||
HTTP keep-alive, which can drastically improve performance, so we use this
|
|
||||||
instead of directly providing an upstream address in `proxy_pass` directive. In
|
|
||||||
this example, the upstream section is named by `server_name`, i.e. the public
|
|
||||||
domain name, which then also gets passed to Sanic in the `Host` header. You may
|
|
||||||
change the naming as you see fit. Multiple servers may also be provided for
|
|
||||||
load balancing and failover.
|
|
||||||
|
|
||||||
Change the two occurrences of `example.com` to your true domain name, and
|
|
||||||
instead of `YOUR SECRET` use the secret you chose for your app.
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
upstream example.com {
|
|
||||||
keepalive 100;
|
|
||||||
server 127.0.0.1:8000;
|
|
||||||
#server unix:/tmp/sanic.sock;
|
|
||||||
}
|
|
||||||
|
|
||||||
server {
|
|
||||||
server_name example.com;
|
|
||||||
listen 443 ssl http2 default_server;
|
|
||||||
listen [::]:443 ssl http2 default_server;
|
|
||||||
# Serve static files if found, otherwise proxy to Sanic
|
|
||||||
location / {
|
|
||||||
root /var/www;
|
|
||||||
try_files $uri @sanic;
|
|
||||||
}
|
|
||||||
location @sanic {
|
|
||||||
proxy_pass http://$server_name;
|
|
||||||
# Allow fast streaming HTTP/1.1 pipes (keep-alive, unbuffered)
|
|
||||||
proxy_http_version 1.1;
|
|
||||||
proxy_request_buffering off;
|
|
||||||
proxy_buffering off;
|
|
||||||
# Proxy forwarding (password configured in app.config.FORWARDED_SECRET)
|
|
||||||
proxy_set_header forwarded "$proxy_forwarded;secret=\"YOUR SECRET\"";
|
|
||||||
# Allow websockets
|
|
||||||
proxy_set_header connection "upgrade";
|
|
||||||
proxy_set_header upgrade $http_upgrade;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
To avoid cookie visibility issues and inconsistent addresses on search engines,
|
|
||||||
it is a good idea to redirect all visitors to one true domain, always using
|
|
||||||
HTTPS:
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
# Redirect all HTTP to HTTPS with no-WWW
|
|
||||||
server {
|
|
||||||
listen 80 default_server;
|
|
||||||
listen [::]:80 default_server;
|
|
||||||
server_name ~^(?:www\.)?(.*)$;
|
|
||||||
return 301 https://$1$request_uri;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Redirect WWW to no-WWW
|
|
||||||
server {
|
|
||||||
listen 443 ssl http2;
|
|
||||||
listen [::]:443 ssl http2;
|
|
||||||
server_name ~^www\.(.*)$;
|
|
||||||
return 301 $scheme://$1$request_uri;
|
|
||||||
}
|
|
||||||
|
|
||||||
The above config sections may be placed in `/etc/nginx/sites-available/default`
|
|
||||||
or in other site configs (be sure to symlink them to `sites-enabled` if you
|
|
||||||
create new ones).
|
|
||||||
|
|
||||||
Make sure that your SSL certificates are configured in the main config, or
|
|
||||||
add the `ssl_certificate` and `ssl_certificate_key` directives to each
|
|
||||||
`server` section that listens on SSL.
|
|
||||||
|
|
||||||
Additionally, copy&paste all of this into `nginx/conf.d/forwarded.conf`:
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
# RFC 7239 Forwarded header for Nginx proxy_pass
|
|
||||||
|
|
||||||
# Add within your server or location block:
|
|
||||||
# proxy_set_header forwarded "$proxy_forwarded;secret=\"YOUR SECRET\"";
|
|
||||||
|
|
||||||
# Configure your upstream web server to identify this proxy by that password
|
|
||||||
# because otherwise anyone on the Internet could spoof these headers and fake
|
|
||||||
# their real IP address and other information to your service.
|
|
||||||
|
|
||||||
|
|
||||||
# Provide the full proxy chain in $proxy_forwarded
|
|
||||||
map $proxy_add_forwarded $proxy_forwarded {
|
|
||||||
default "$proxy_add_forwarded;by=\"_$hostname\";proto=$scheme;host=\"$http_host\";path=\"$request_uri\"";
|
|
||||||
}
|
|
||||||
|
|
||||||
# The following mappings are based on
|
|
||||||
# https://www.nginx.com/resources/wiki/start/topics/examples/forwarded/
|
|
||||||
|
|
||||||
map $remote_addr $proxy_forwarded_elem {
|
|
||||||
# IPv4 addresses can be sent as-is
|
|
||||||
~^[0-9.]+$ "for=$remote_addr";
|
|
||||||
|
|
||||||
# IPv6 addresses need to be bracketed and quoted
|
|
||||||
~^[0-9A-Fa-f:.]+$ "for=\"[$remote_addr]\"";
|
|
||||||
|
|
||||||
# Unix domain socket names cannot be represented in RFC 7239 syntax
|
|
||||||
default "for=unknown";
|
|
||||||
}
|
|
||||||
|
|
||||||
map $http_forwarded $proxy_add_forwarded {
|
|
||||||
# If the incoming Forwarded header is syntactically valid, append to it
|
|
||||||
"~^(,[ \\t]*)*([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?(;([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?)*([ \\t]*,([ \\t]*([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?(;([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|\"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*\"))?)*)?)*$" "$http_forwarded, $proxy_forwarded_elem";
|
|
||||||
|
|
||||||
# Otherwise, replace it
|
|
||||||
default "$proxy_forwarded_elem";
|
|
||||||
}
|
|
||||||
|
|
||||||
For installs that don't use `conf.d` and `sites-available`, all of the above
|
|
||||||
configs may also be placed inside the `http` section of the main `nginx.conf`.
|
|
||||||
|
|
||||||
Reload Nginx config after changes:
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
sudo nginx -s reload
|
|
||||||
|
|
||||||
Now you should be able to connect your app on `https://example.com/`. Any 404
|
|
||||||
errors and such will be handled by Sanic's error pages, and whenever a static
|
|
||||||
file is present at a given path, it will be served by Nginx.
|
|
||||||
|
|
||||||
|
|
||||||
SSL certificates
|
|
||||||
~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
If you haven't already configured valid certificates on your server, now is a
|
|
||||||
good time to do so. Install `certbot` and `python3-certbot-nginx`, then run
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
certbot --nginx -d example.com -d www.example.com
|
|
||||||
|
|
||||||
`<https://www.nginx.com/blog/using-free-ssltls-certificates-from-lets-encrypt-with-nginx/>`_
|
|
||||||
|
|
||||||
Running as a service
|
|
||||||
~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
This part is for Linux distributions based on `systemd`. Create a unit file
|
|
||||||
`/etc/systemd/system/sanicexample.service`::
|
|
||||||
|
|
||||||
[Unit]
|
|
||||||
Description=Sanic Example
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
User=nobody
|
|
||||||
WorkingDirectory=/srv/sanicexample
|
|
||||||
ExecStart=/usr/bin/env python3 sanicexample.py
|
|
||||||
Restart=always
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
|
||||||
|
|
||||||
Then reload service files, start your service and enable it on boot::
|
|
||||||
|
|
||||||
sudo systemctl daemon-reload
|
|
||||||
sudo systemctl start sanicexample
|
|
||||||
sudo systemctl enable sanicexample
|
|
66
docs/sanic/releases/21/21.12.md
Normal file
66
docs/sanic/releases/21/21.12.md
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
## Version 21.12.1 🔷
|
||||||
|
|
||||||
|
_Current LTS version_
|
||||||
|
|
||||||
|
- [#2349](https://github.com/sanic-org/sanic/pull/2349) Only display MOTD on startup
|
||||||
|
- [#2354](https://github.com/sanic-org/sanic/pull/2354) Ignore name argument in Python 3.7
|
||||||
|
- [#2355](https://github.com/sanic-org/sanic/pull/2355) Add config.update support for all config values
|
||||||
|
|
||||||
|
## Version 21.12.0 🔹
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- [#2260](https://github.com/sanic-org/sanic/pull/2260) Allow early Blueprint registrations to still apply later added objects
|
||||||
|
- [#2262](https://github.com/sanic-org/sanic/pull/2262) Noisy exceptions - force logging of all exceptions
|
||||||
|
- [#2264](https://github.com/sanic-org/sanic/pull/2264) Optional `uvloop` by configuration
|
||||||
|
- [#2270](https://github.com/sanic-org/sanic/pull/2270) Vhost support using multiple TLS certificates
|
||||||
|
- [#2277](https://github.com/sanic-org/sanic/pull/2277) Change signal routing for increased consistency
|
||||||
|
- *BREAKING CHANGE*: If you were manually routing signals there is a breaking change. The signal router's `get` is no longer 100% determinative. There is now an additional step to loop thru the returned signals for proper matching on the requirements. If signals are being dispatched using `app.dispatch` or `bp.dispatch`, there is no change.
|
||||||
|
- [#2290](https://github.com/sanic-org/sanic/pull/2290) Add contextual exceptions
|
||||||
|
- [#2291](https://github.com/sanic-org/sanic/pull/2291) Increase join concat performance
|
||||||
|
- [#2295](https://github.com/sanic-org/sanic/pull/2295), [#2316](https://github.com/sanic-org/sanic/pull/2316), [#2331](https://github.com/sanic-org/sanic/pull/2331) Restructure of CLI and application state with new displays and more command parity with `app.run`
|
||||||
|
- [#2302](https://github.com/sanic-org/sanic/pull/2302) Add route context at definition time
|
||||||
|
- [#2304](https://github.com/sanic-org/sanic/pull/2304) Named tasks and new API for managing background tasks
|
||||||
|
- [#2307](https://github.com/sanic-org/sanic/pull/2307) On app auto-reload, provide insight of changed files
|
||||||
|
- [#2308](https://github.com/sanic-org/sanic/pull/2308) Auto extend application with [Sanic Extensions](https://sanicframework.org/en/plugins/sanic-ext/getting-started.html) if it is installed, and provide first class support for accessing the extensions
|
||||||
|
- [#2309](https://github.com/sanic-org/sanic/pull/2309) Builtin signals changed to `Enum`
|
||||||
|
- [#2313](https://github.com/sanic-org/sanic/pull/2313) Support additional config implementation use case
|
||||||
|
- [#2321](https://github.com/sanic-org/sanic/pull/2321) Refactor environment variable hydration logic
|
||||||
|
- [#2327](https://github.com/sanic-org/sanic/pull/2327) Prevent sending multiple or mixed responses on a single request
|
||||||
|
- [#2330](https://github.com/sanic-org/sanic/pull/2330) Custom type casting on environment variables
|
||||||
|
- [#2332](https://github.com/sanic-org/sanic/pull/2332) Make all deprecation notices consistent
|
||||||
|
- [#2335](https://github.com/sanic-org/sanic/pull/2335) Allow underscore to start instance names
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
- [#2273](https://github.com/sanic-org/sanic/pull/2273) Replace assignation by typing for `websocket_handshake`
|
||||||
|
- [#2285](https://github.com/sanic-org/sanic/pull/2285) Fix IPv6 display in startup logs
|
||||||
|
- [#2299](https://github.com/sanic-org/sanic/pull/2299) Dispatch `http.lifecyle.response` from exception handler
|
||||||
|
|
||||||
|
### Deprecations and Removals
|
||||||
|
- [#2306](https://github.com/sanic-org/sanic/pull/2306) Removal of deprecated items
|
||||||
|
- `Sanic` and `Blueprint` may no longer have arbitrary properties attached to them
|
||||||
|
- `Sanic` and `Blueprint` forced to have compliant names
|
||||||
|
- alphanumeric + `_` + `-`
|
||||||
|
- must start with letter or `_`
|
||||||
|
- `load_env` keyword argument of `Sanic`
|
||||||
|
- `sanic.exceptions.abort`
|
||||||
|
- `sanic.views.CompositionView`
|
||||||
|
- `sanic.response.StreamingHTTPResponse`
|
||||||
|
- *NOTE:* the `stream()` response method (where you pass a callable streaming function) has been deprecated and will be removed in v22.6. You should upgrade all streaming responses to the new style: https://sanicframework.org/en/guide/advanced/streaming.html#response-streaming
|
||||||
|
- [#2320](https://github.com/sanic-org/sanic/pull/2320) Remove app instance from Config for error handler setting
|
||||||
|
|
||||||
|
### Developer infrastructure
|
||||||
|
- [#2251](https://github.com/sanic-org/sanic/pull/2251) Change dev install command
|
||||||
|
- [#2286](https://github.com/sanic-org/sanic/pull/2286) Change codeclimate complexity threshold from 5 to 10
|
||||||
|
- [#2287](https://github.com/sanic-org/sanic/pull/2287) Update host test function names so they are not overwritten
|
||||||
|
- [#2292](https://github.com/sanic-org/sanic/pull/2292) Fail CI on error
|
||||||
|
- [#2311](https://github.com/sanic-org/sanic/pull/2311), [#2324](https://github.com/sanic-org/sanic/pull/2324) Do not run tests for draft PRs
|
||||||
|
- [#2336](https://github.com/sanic-org/sanic/pull/2336) Remove paths from coverage checks
|
||||||
|
- [#2338](https://github.com/sanic-org/sanic/pull/2338) Cleanup ports on tests
|
||||||
|
|
||||||
|
### Improved Documentation
|
||||||
|
- [#2269](https://github.com/sanic-org/sanic/pull/2269), [#2329](https://github.com/sanic-org/sanic/pull/2329), [#2333](https://github.com/sanic-org/sanic/pull/2333) Cleanup typos and fix language
|
||||||
|
|
||||||
|
### Miscellaneous
|
||||||
|
- [#2257](https://github.com/sanic-org/sanic/pull/2257), [#2294](https://github.com/sanic-org/sanic/pull/2294), [#2341](https://github.com/sanic-org/sanic/pull/2341) Add Python 3.10 support
|
||||||
|
- [#2279](https://github.com/sanic-org/sanic/pull/2279), [#2317](https://github.com/sanic-org/sanic/pull/2317), [#2322](https://github.com/sanic-org/sanic/pull/2322) Add/correct missing type annotations
|
||||||
|
- [#2305](https://github.com/sanic-org/sanic/pull/2305) Fix examples to use modern implementations
|
50
docs/sanic/releases/21/21.9.md
Normal file
50
docs/sanic/releases/21/21.9.md
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
## Version 21.9.3
|
||||||
|
*Rerelease of v21.9.2 with some cleanup*
|
||||||
|
|
||||||
|
## Version 21.9.2
|
||||||
|
- [#2268](https://github.com/sanic-org/sanic/pull/2268) Make HTTP connections start in IDLE stage, avoiding delays and error messages
|
||||||
|
- [#2310](https://github.com/sanic-org/sanic/pull/2310) More consistent config setting with post-FALLBACK_ERROR_FORMAT apply
|
||||||
|
|
||||||
|
## Version 21.9.1
|
||||||
|
- [#2259](https://github.com/sanic-org/sanic/pull/2259) Allow non-conforming ErrorHandlers
|
||||||
|
|
||||||
|
## Version 21.9.0
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- [#2158](https://github.com/sanic-org/sanic/pull/2158), [#2248](https://github.com/sanic-org/sanic/pull/2248) Complete overhaul of I/O to websockets
|
||||||
|
- [#2160](https://github.com/sanic-org/sanic/pull/2160) Add new 17 signals into server and request lifecycles
|
||||||
|
- [#2162](https://github.com/sanic-org/sanic/pull/2162) Smarter `auto` fallback formatting upon exception
|
||||||
|
- [#2184](https://github.com/sanic-org/sanic/pull/2184) Introduce implementation for copying a Blueprint
|
||||||
|
- [#2200](https://github.com/sanic-org/sanic/pull/2200) Accept header parsing
|
||||||
|
- [#2207](https://github.com/sanic-org/sanic/pull/2207) Log remote address if available
|
||||||
|
- [#2209](https://github.com/sanic-org/sanic/pull/2209) Add convenience methods to BP groups
|
||||||
|
- [#2216](https://github.com/sanic-org/sanic/pull/2216) Add default messages to SanicExceptions
|
||||||
|
- [#2225](https://github.com/sanic-org/sanic/pull/2225) Type annotation convenience for annotated handlers with path parameters
|
||||||
|
- [#2236](https://github.com/sanic-org/sanic/pull/2236) Allow Falsey (but not-None) responses from route handlers
|
||||||
|
- [#2238](https://github.com/sanic-org/sanic/pull/2238) Add `exception` decorator to Blueprint Groups
|
||||||
|
- [#2244](https://github.com/sanic-org/sanic/pull/2244) Explicit static directive for serving file or dir (ex: `static(..., resource_type="file")`)
|
||||||
|
- [#2245](https://github.com/sanic-org/sanic/pull/2245) Close HTTP loop when connection task cancelled
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
- [#2188](https://github.com/sanic-org/sanic/pull/2188) Fix the handling of the end of a chunked request
|
||||||
|
- [#2195](https://github.com/sanic-org/sanic/pull/2195) Resolve unexpected error handling on static requests
|
||||||
|
- [#2208](https://github.com/sanic-org/sanic/pull/2208) Make blueprint-based exceptions attach and trigger in a more intuitive manner
|
||||||
|
- [#2211](https://github.com/sanic-org/sanic/pull/2211) Fixed for handling exceptions of asgi app call
|
||||||
|
- [#2213](https://github.com/sanic-org/sanic/pull/2213) Fix bug where ws exceptions not being logged
|
||||||
|
- [#2231](https://github.com/sanic-org/sanic/pull/2231) Cleaner closing of tasks by using `abort()` in strategic places to avoid dangling sockets
|
||||||
|
- [#2247](https://github.com/sanic-org/sanic/pull/2247) Fix logging of auto-reload status in debug mode
|
||||||
|
- [#2246](https://github.com/sanic-org/sanic/pull/2246) Account for BP with exception handler but no routes
|
||||||
|
|
||||||
|
### Developer infrastructure
|
||||||
|
- [#2194](https://github.com/sanic-org/sanic/pull/2194) HTTP unit tests with raw client
|
||||||
|
- [#2199](https://github.com/sanic-org/sanic/pull/2199) Switch to codeclimate
|
||||||
|
- [#2214](https://github.com/sanic-org/sanic/pull/2214) Try Reopening Windows Tests
|
||||||
|
- [#2229](https://github.com/sanic-org/sanic/pull/2229) Refactor `HttpProtocol` into a base class
|
||||||
|
- [#2230](https://github.com/sanic-org/sanic/pull/2230) Refactor `server.py` into multi-file module
|
||||||
|
|
||||||
|
### Miscellaneous
|
||||||
|
- [#2173](https://github.com/sanic-org/sanic/pull/2173) Remove Duplicated Dependencies and PEP 517 Support
|
||||||
|
- [#2193](https://github.com/sanic-org/sanic/pull/2193), [#2196](https://github.com/sanic-org/sanic/pull/2196), [#2217](https://github.com/sanic-org/sanic/pull/2217) Type annotation changes
|
||||||
|
|
||||||
|
|
||||||
|
|
55
docs/sanic/releases/22/22.12.md
Normal file
55
docs/sanic/releases/22/22.12.md
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
## Version 22.12.0 🔷
|
||||||
|
|
||||||
|
_Current version_
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- [#2569](https://github.com/sanic-org/sanic/pull/2569) Add `JSONResponse` class with some convenient methods when updating a response object
|
||||||
|
- [#2598](https://github.com/sanic-org/sanic/pull/2598) Change `uvloop` requirement to `>=0.15.0`
|
||||||
|
- [#2609](https://github.com/sanic-org/sanic/pull/2609) Add compatibility with `websockets` v11.0
|
||||||
|
- [#2610](https://github.com/sanic-org/sanic/pull/2610) Kill server early on worker error
|
||||||
|
- Raise deadlock timeout to 30s
|
||||||
|
- [#2617](https://github.com/sanic-org/sanic/pull/2617) Scale number of running server workers
|
||||||
|
- [#2621](https://github.com/sanic-org/sanic/pull/2621) [#2634](https://github.com/sanic-org/sanic/pull/2634) Send `SIGKILL` on subsequent `ctrl+c` to force worker exit
|
||||||
|
- [#2622](https://github.com/sanic-org/sanic/pull/2622) Add API to restart all workers from the multiplexer
|
||||||
|
- [#2624](https://github.com/sanic-org/sanic/pull/2624) Default to `spawn` for all subprocesses unless specifically set:
|
||||||
|
```python
|
||||||
|
from sanic import Sanic
|
||||||
|
|
||||||
|
Sanic.start_method = "fork"
|
||||||
|
```
|
||||||
|
- [#2625](https://github.com/sanic-org/sanic/pull/2625) Filename normalisation of form-data/multipart file uploads
|
||||||
|
- [#2626](https://github.com/sanic-org/sanic/pull/2626) Move to HTTP Inspector:
|
||||||
|
- Remote access to inspect running Sanic instances
|
||||||
|
- TLS support for encrypted calls to Inspector
|
||||||
|
- Authentication to Inspector with API key
|
||||||
|
- Ability to extend Inspector with custom commands
|
||||||
|
- [#2632](https://github.com/sanic-org/sanic/pull/2632) Control order of restart operations
|
||||||
|
- [#2633](https://github.com/sanic-org/sanic/pull/2633) Move reload interval to class variable
|
||||||
|
- [#2636](https://github.com/sanic-org/sanic/pull/2636) Add `priority` to `register_middleware` method
|
||||||
|
- [#2639](https://github.com/sanic-org/sanic/pull/2639) Add `unquote` to `add_route` method
|
||||||
|
- [#2640](https://github.com/sanic-org/sanic/pull/2640) ASGI websockets to receive `text` or `bytes`
|
||||||
|
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
|
||||||
|
- [#2607](https://github.com/sanic-org/sanic/pull/2607) Force socket shutdown before close to allow rebinding
|
||||||
|
- [#2590](https://github.com/sanic-org/sanic/pull/2590) Use actual `StrEnum` in Python 3.11+
|
||||||
|
- [#2615](https://github.com/sanic-org/sanic/pull/2615) Ensure middleware executes only once per request timeout
|
||||||
|
- [#2627](https://github.com/sanic-org/sanic/pull/2627) Crash ASGI application on lifespan failure
|
||||||
|
- [#2635](https://github.com/sanic-org/sanic/pull/2635) Resolve error with low-level server creation on Windows
|
||||||
|
|
||||||
|
|
||||||
|
### Deprecations and Removals
|
||||||
|
|
||||||
|
- [#2608](https://github.com/sanic-org/sanic/pull/2608) [#2630](https://github.com/sanic-org/sanic/pull/2630) Signal conditions and triggers saved on `signal.extra`
|
||||||
|
- [#2626](https://github.com/sanic-org/sanic/pull/2626) Move to HTTP Inspector
|
||||||
|
- 🚨 *BREAKING CHANGE*: Moves the Inspector to a Sanic app from a simple TCP socket with a custom protocol
|
||||||
|
- *DEPRECATE*: The `--inspect*` commands have been deprecated in favor of `inspect ...` commands
|
||||||
|
- [#2628](https://github.com/sanic-org/sanic/pull/2628) Replace deprecated `distutils.strtobool`
|
||||||
|
|
||||||
|
|
||||||
|
### Developer infrastructure
|
||||||
|
|
||||||
|
- [#2612](https://github.com/sanic-org/sanic/pull/2612) Add CI testing for Python 3.11
|
||||||
|
|
52
docs/sanic/releases/22/22.3.md
Normal file
52
docs/sanic/releases/22/22.3.md
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
## Version 22.3.0
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- [#2347](https://github.com/sanic-org/sanic/pull/2347) API for multi-application server
|
||||||
|
- 🚨 *BREAKING CHANGE*: The old `sanic.worker.GunicornWorker` has been **removed**. To run Sanic with `gunicorn`, you should use it thru `uvicorn` [as described in their docs](https://www.uvicorn.org/#running-with-gunicorn).
|
||||||
|
- 🧁 *SIDE EFFECT*: Named background tasks are now supported, even in Python 3.7
|
||||||
|
- [#2357](https://github.com/sanic-org/sanic/pull/2357) Parse `Authorization` header as `Request.credentials`
|
||||||
|
- [#2361](https://github.com/sanic-org/sanic/pull/2361) Add config option to skip `Touchup` step in application startup
|
||||||
|
- [#2372](https://github.com/sanic-org/sanic/pull/2372) Updates to CLI help messaging
|
||||||
|
- [#2382](https://github.com/sanic-org/sanic/pull/2382) Downgrade warnings to backwater debug messages
|
||||||
|
- [#2396](https://github.com/sanic-org/sanic/pull/2396) Allow for `multidict` v0.6
|
||||||
|
- [#2401](https://github.com/sanic-org/sanic/pull/2401) Upgrade CLI catching for alternative application run types
|
||||||
|
- [#2402](https://github.com/sanic-org/sanic/pull/2402) Conditionally inject CLI arguments into factory
|
||||||
|
- [#2413](https://github.com/sanic-org/sanic/pull/2413) Add new start and stop event listeners to reloader process
|
||||||
|
- [#2414](https://github.com/sanic-org/sanic/pull/2414) Remove loop as required listener arg
|
||||||
|
- [#2415](https://github.com/sanic-org/sanic/pull/2415) Better exception for bad URL parsing
|
||||||
|
- [sanic-routing#47](https://github.com/sanic-org/sanic-routing/pull/47) Add a new extention parameter type: `<file:ext>`, `<file:ext=jpg>`, `<file:ext=jpg|png|gif|svg>`, `<file=int:ext>`, `<file=int:ext=jpg|png|gif|svg>`, `<file=float:ext=tar.gz>`
|
||||||
|
- 👶 *BETA FEATURE*: This feature will not work with `path` type matching, and is being released as a beta feature only.
|
||||||
|
- [sanic-routing#57](https://github.com/sanic-org/sanic-routing/pull/57) Change `register_pattern` to accept a `str` or `Pattern`
|
||||||
|
- [sanic-routing#58](https://github.com/sanic-org/sanic-routing/pull/58) Default matching on non-empty strings only, and new `strorempty` pattern type
|
||||||
|
- 🚨 *BREAKING CHANGE*: Previously a route with a dynamic string parameter (`/<foo>` or `/<foo:str>`) would match on any string, including empty strings. It will now **only** match a non-empty string. To retain the old behavior, you should use the new parameter type: `/<foo:strorempty>`.
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
- [#2373](https://github.com/sanic-org/sanic/pull/2373) Remove `error_logger` on websockets
|
||||||
|
- [#2381](https://github.com/sanic-org/sanic/pull/2381) Fix newly assigned `None` in task registry
|
||||||
|
- [sanic-routing#52](https://github.com/sanic-org/sanic-routing/pull/52) Add type casting to regex route matching
|
||||||
|
- [sanic-routing#60](https://github.com/sanic-org/sanic-routing/pull/60) Add requirements check on regex routes (this resolves, for example, multiple static directories with differing `host` values)
|
||||||
|
|
||||||
|
### Deprecations and Removals
|
||||||
|
- [#2362](https://github.com/sanic-org/sanic/pull/2362) 22.3 Deprecations and changes
|
||||||
|
1. `debug=True` and `--debug` do _NOT_ automatically run `auto_reload`
|
||||||
|
2. Default error render is with plain text (browsers still get HTML by default because `auto` looks at headers)
|
||||||
|
3. `config` is required for `ErrorHandler.finalize`
|
||||||
|
4. `ErrorHandler.lookup` requires two positional args
|
||||||
|
5. Unused websocket protocol args removed
|
||||||
|
- [#2344](https://github.com/sanic-org/sanic/pull/2344) Deprecate loading of lowercase environment variables
|
||||||
|
|
||||||
|
### Developer infrastructure
|
||||||
|
- [#2363](https://github.com/sanic-org/sanic/pull/2363) Revert code coverage back to Codecov
|
||||||
|
- [#2405](https://github.com/sanic-org/sanic/pull/2405) Upgrade tests for `sanic-routing` changes
|
||||||
|
- [sanic-testing#35](https://github.com/sanic-org/sanic-testing/pull/35) Allow for httpx v0.22
|
||||||
|
|
||||||
|
### Improved Documentation
|
||||||
|
- [#2350](https://github.com/sanic-org/sanic/pull/2350) Fix link in README for ASGI
|
||||||
|
- [#2398](https://github.com/sanic-org/sanic/pull/2398) Document middleware on_request and on_response
|
||||||
|
- [#2409](https://github.com/sanic-org/sanic/pull/2409) Add missing documentation for `Request.respond`
|
||||||
|
|
||||||
|
### Miscellaneous
|
||||||
|
- [#2376](https://github.com/sanic-org/sanic/pull/2376) Fix typing for `ListenerMixin.listener`
|
||||||
|
- [#2383](https://github.com/sanic-org/sanic/pull/2383) Clear deprecation warning in `asyncio.wait`
|
||||||
|
- [#2387](https://github.com/sanic-org/sanic/pull/2387) Cleanup `__slots__` implementations
|
||||||
|
- [#2390](https://github.com/sanic-org/sanic/pull/2390) Clear deprecation warning in `asyncio.get_event_loop`
|
54
docs/sanic/releases/22/22.6.md
Normal file
54
docs/sanic/releases/22/22.6.md
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
## Version 22.6.2
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
|
||||||
|
- [#2522](https://github.com/sanic-org/sanic/pull/2522) Always show server location in ASGI
|
||||||
|
|
||||||
|
## Version 22.6.1
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
|
||||||
|
- [#2477](https://github.com/sanic-org/sanic/pull/2477) Sanic static directory fails when folder name ends with ".."
|
||||||
|
|
||||||
|
|
||||||
|
## Version 22.6.0
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- [#2378](https://github.com/sanic-org/sanic/pull/2378) Introduce HTTP/3 and autogeneration of TLS certificates in `DEBUG` mode
|
||||||
|
- 👶 *EARLY RELEASE FEATURE*: Serving Sanic over HTTP/3 is an early release feature. It does not yet fully cover the HTTP/3 spec, but instead aims for feature parity with Sanic's existing HTTP/1.1 server. Websockets, WebTransport, push responses are examples of some features not yet implemented.
|
||||||
|
- 📦 *EXTRA REQUIREMENT*: Not all HTTP clients are capable of interfacing with HTTP/3 servers. You may need to install a [HTTP/3 capable client](https://curl.se/docs/http3.html).
|
||||||
|
- 📦 *EXTRA REQUIREMENT*: In order to use TLS autogeneration, you must install either [mkcert](https://github.com/FiloSottile/mkcert) or [trustme](https://github.com/python-trio/trustme).
|
||||||
|
- [#2416](https://github.com/sanic-org/sanic/pull/2416) Add message to `task.cancel`
|
||||||
|
- [#2420](https://github.com/sanic-org/sanic/pull/2420) Add exception aliases for more consistent naming with standard HTTP response types (`BadRequest`, `MethodNotAllowed`, `RangeNotSatisfiable`)
|
||||||
|
- [#2432](https://github.com/sanic-org/sanic/pull/2432) Expose ASGI `scope` as a property on the `Request` object
|
||||||
|
- [#2438](https://github.com/sanic-org/sanic/pull/2438) Easier access to websocket class for annotation: `from sanic import Websocket`
|
||||||
|
- [#2439](https://github.com/sanic-org/sanic/pull/2439) New API for reading form values with options: `Request.get_form`
|
||||||
|
- [#2445](https://github.com/sanic-org/sanic/pull/2445) Add custom `loads` function
|
||||||
|
- [#2447](https://github.com/sanic-org/sanic/pull/2447), [#2486](https://github.com/sanic-org/sanic/pull/2486) Improved API to support setting cache control headers
|
||||||
|
- [#2453](https://github.com/sanic-org/sanic/pull/2453) Move verbosity filtering to logger
|
||||||
|
- [#2475](https://github.com/sanic-org/sanic/pull/2475) Expose getter for current request using `Request.get_current()`
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
- [#2448](https://github.com/sanic-org/sanic/pull/2448) Fix to allow running with `pythonw.exe` or places where there is no `sys.stdout`
|
||||||
|
- [#2451](https://github.com/sanic-org/sanic/pull/2451) Trigger `http.lifecycle.request` signal in ASGI mode
|
||||||
|
- [#2455](https://github.com/sanic-org/sanic/pull/2455) Resolve typing of stacked route definitions
|
||||||
|
- [#2463](https://github.com/sanic-org/sanic/pull/2463) Properly catch websocket CancelledError in websocket handler in Python 3.7
|
||||||
|
|
||||||
|
### Deprecations and Removals
|
||||||
|
- [#2487](https://github.com/sanic-org/sanic/pull/2487) v22.6 deprecations and changes
|
||||||
|
1. Optional application registry
|
||||||
|
1. Execution of custom handlers after some part of response was sent
|
||||||
|
1. Configuring fallback handlers on the `ErrorHandler`
|
||||||
|
1. Custom `LOGO` setting
|
||||||
|
1. `sanic.response.stream`
|
||||||
|
1. `AsyncioServer.init`
|
||||||
|
|
||||||
|
### Developer infrastructure
|
||||||
|
- [#2449](https://github.com/sanic-org/sanic/pull/2449) Clean up `black` and `isort` config
|
||||||
|
- [#2479](https://github.com/sanic-org/sanic/pull/2479) Fix some flappy tests
|
||||||
|
|
||||||
|
### Improved Documentation
|
||||||
|
- [#2461](https://github.com/sanic-org/sanic/pull/2461) Update example to match current application naming standards
|
||||||
|
- [#2466](https://github.com/sanic-org/sanic/pull/2466) Better type annotation for `Extend`
|
||||||
|
- [#2485](https://github.com/sanic-org/sanic/pull/2485) Improved help messages in CLI
|
||||||
|
|
74
docs/sanic/releases/22/22.9.md
Normal file
74
docs/sanic/releases/22/22.9.md
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
## Version 22.9.1
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- [#2585](https://github.com/sanic-org/sanic/pull/2585) Improved error message when no applications have been registered
|
||||||
|
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
|
||||||
|
- [#2578](https://github.com/sanic-org/sanic/pull/2578) Add certificate loader for in process certificate creation
|
||||||
|
- [#2591](https://github.com/sanic-org/sanic/pull/2591) Do not use sentinel identity for `spawn` compatibility
|
||||||
|
- [#2592](https://github.com/sanic-org/sanic/pull/2592) Fix properties in nested blueprint groups
|
||||||
|
- [#2595](https://github.com/sanic-org/sanic/pull/2595) Introduce sleep interval on new worker reloader
|
||||||
|
|
||||||
|
|
||||||
|
### Deprecations and Removals
|
||||||
|
|
||||||
|
|
||||||
|
### Developer infrastructure
|
||||||
|
|
||||||
|
- [#2588](https://github.com/sanic-org/sanic/pull/2588) Markdown templates on issue forms
|
||||||
|
|
||||||
|
|
||||||
|
### Improved Documentation
|
||||||
|
|
||||||
|
- [#2556](https://github.com/sanic-org/sanic/pull/2556) v22.9 documentation
|
||||||
|
- [#2582](https://github.com/sanic-org/sanic/pull/2582) Cleanup documentation on Windows support
|
||||||
|
|
||||||
|
|
||||||
|
## Version 22.9.0
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- [#2445](https://github.com/sanic-org/sanic/pull/2445) Add custom loads function
|
||||||
|
- [#2490](https://github.com/sanic-org/sanic/pull/2490) Make `WebsocketImplProtocol` async iterable
|
||||||
|
- [#2499](https://github.com/sanic-org/sanic/pull/2499) Sanic Server WorkerManager refactor
|
||||||
|
- [#2506](https://github.com/sanic-org/sanic/pull/2506) Use `pathlib` for path resolution (for static file serving)
|
||||||
|
- [#2508](https://github.com/sanic-org/sanic/pull/2508) Use `path.parts` instead of `match` (for static file serving)
|
||||||
|
- [#2513](https://github.com/sanic-org/sanic/pull/2513) Better request cancel handling
|
||||||
|
- [#2516](https://github.com/sanic-org/sanic/pull/2516) Add request properties for HTTP method info:
|
||||||
|
- `request.is_safe`
|
||||||
|
- `request.is_idempotent`
|
||||||
|
- `request.is_cacheable`
|
||||||
|
- *See* [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) *for more information about when these apply*
|
||||||
|
- [#2522](https://github.com/sanic-org/sanic/pull/2522) Always show server location in ASGI
|
||||||
|
- [#2526](https://github.com/sanic-org/sanic/pull/2526) Cache control support for static files for returning 304 when appropriate
|
||||||
|
- [#2533](https://github.com/sanic-org/sanic/pull/2533) Refactor `_static_request_handler`
|
||||||
|
- [#2540](https://github.com/sanic-org/sanic/pull/2540) Add signals before and after handler execution
|
||||||
|
- `http.handler.before`
|
||||||
|
- `http.handler.after`
|
||||||
|
- [#2542](https://github.com/sanic-org/sanic/pull/2542) Add *[redacted]* to CLI :)
|
||||||
|
- [#2546](https://github.com/sanic-org/sanic/pull/2546) Add deprecation warning filter
|
||||||
|
- [#2550](https://github.com/sanic-org/sanic/pull/2550) Middleware priority and performance enhancements
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
|
||||||
|
- [#2495](https://github.com/sanic-org/sanic/pull/2495) Prevent directory traversion with static files
|
||||||
|
- [#2515](https://github.com/sanic-org/sanic/pull/2515) Do not apply double slash to paths in certain static dirs in Blueprints
|
||||||
|
|
||||||
|
### Deprecations and Removals
|
||||||
|
|
||||||
|
- [#2525](https://github.com/sanic-org/sanic/pull/2525) Warn on duplicate route names, will be prevented outright in v23.3
|
||||||
|
- [#2537](https://github.com/sanic-org/sanic/pull/2537) Raise warning and deprecation notice on duplicate exceptions, will be prevented outright in v23.3
|
||||||
|
|
||||||
|
### Developer infrastructure
|
||||||
|
|
||||||
|
- [#2504](https://github.com/sanic-org/sanic/pull/2504) Cleanup test suite
|
||||||
|
- [#2505](https://github.com/sanic-org/sanic/pull/2505) Replace Unsupported Python Version Number from the Contributing Doc
|
||||||
|
- [#2530](https://github.com/sanic-org/sanic/pull/2530) Do not include tests folder in installed package resolver
|
||||||
|
|
||||||
|
### Improved Documentation
|
||||||
|
|
||||||
|
- [#2502](https://github.com/sanic-org/sanic/pull/2502) Fix a few typos
|
||||||
|
- [#2517](https://github.com/sanic-org/sanic/pull/2517) [#2536](https://github.com/sanic-org/sanic/pull/2536) Add some type hints
|
53
docs/sanic/releases/23/23.3.md
Normal file
53
docs/sanic/releases/23/23.3.md
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
## Version 23.3.0
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- [#2545](https://github.com/sanic-org/sanic/pull/2545) Standardize init of exceptions for more consistent control of HTTP responses using exceptions
|
||||||
|
- [#2606](https://github.com/sanic-org/sanic/pull/2606) Decode headers as UTF-8 also in ASGI
|
||||||
|
- [#2646](https://github.com/sanic-org/sanic/pull/2646) Separate ASGI request and lifespan callables
|
||||||
|
- [#2659](https://github.com/sanic-org/sanic/pull/2659) Use ``FALLBACK_ERROR_FORMAT`` for handlers that return ``empty()``
|
||||||
|
- [#2662](https://github.com/sanic-org/sanic/pull/2662) Add basic file browser (HTML page) and auto-index serving
|
||||||
|
- [#2667](https://github.com/sanic-org/sanic/pull/2667) Nicer traceback formatting (HTML page)
|
||||||
|
- [#2668](https://github.com/sanic-org/sanic/pull/2668) Smarter error page rendering format selection; more reliant upon header and "common sense" defaults
|
||||||
|
- [#2680](https://github.com/sanic-org/sanic/pull/2680) Check the status of socket before shutting down with ``SHUT_RDWR``
|
||||||
|
- [#2687](https://github.com/sanic-org/sanic/pull/2687) Refresh ``Request.accept`` functionality to be more performant and spec-compliant
|
||||||
|
- [#2696](https://github.com/sanic-org/sanic/pull/2696) Add header accessors as properties
|
||||||
|
```
|
||||||
|
Example-Field: Foo, Bar
|
||||||
|
Example-Field: Baz
|
||||||
|
```
|
||||||
|
```python
|
||||||
|
request.headers.example_field == "Foo, Bar,Baz"
|
||||||
|
```
|
||||||
|
- [#2700](https://github.com/sanic-org/sanic/pull/2700) Simpler CLI targets
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ sanic path.to.module:app # global app instance
|
||||||
|
$ sanic path.to.module:create_app # factory pattern
|
||||||
|
$ sanic ./path/to/directory/ # simple serve
|
||||||
|
```
|
||||||
|
- [#2701](https://github.com/sanic-org/sanic/pull/2701) API to define a number of workers in managed processes
|
||||||
|
- [#2704](https://github.com/sanic-org/sanic/pull/2704) Add convenience for dynamic changes to routing
|
||||||
|
- [#2706](https://github.com/sanic-org/sanic/pull/2706) Add convenience methods for cookie creation and deletion
|
||||||
|
|
||||||
|
```python
|
||||||
|
response = text("...")
|
||||||
|
response.add_cookie("test", "It worked!", domain=".yummy-yummy-cookie.com")
|
||||||
|
```
|
||||||
|
- [#2707](https://github.com/sanic-org/sanic/pull/2707) Simplified ``parse_content_header`` escaping to be RFC-compliant and remove outdated FF hack
|
||||||
|
- [#2710](https://github.com/sanic-org/sanic/pull/2710) Stricter charset handling and escaping of request URLs
|
||||||
|
- [#2711](https://github.com/sanic-org/sanic/pull/2711) Consume body on ``DELETE`` by default
|
||||||
|
- [#2719](https://github.com/sanic-org/sanic/pull/2719) Allow ``password`` to be passed to TLS context
|
||||||
|
- [#2720](https://github.com/sanic-org/sanic/pull/2720) Skip middleware on ``RequestCancelled``
|
||||||
|
- [#2721](https://github.com/sanic-org/sanic/pull/2721) Change access logging format to ``%s``
|
||||||
|
- [#2722](https://github.com/sanic-org/sanic/pull/2722) Add ``CertLoader`` as application option for directly controlling ``SSLContext`` objects
|
||||||
|
- [#2725](https://github.com/sanic-org/sanic/pull/2725) Worker sync state tolerance on race condition
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
- [#2651](https://github.com/sanic-org/sanic/pull/2651) ASGI websocket to pass thru bytes as is
|
||||||
|
- [#2697](https://github.com/sanic-org/sanic/pull/2697) Fix comparison between datetime aware and naive in ``file`` when using ``If-Modified-Since``
|
||||||
|
|
||||||
|
### Deprecations and Removals
|
||||||
|
- [#2666](https://github.com/sanic-org/sanic/pull/2666) Remove deprecated ``__blueprintname__`` property
|
||||||
|
|
||||||
|
### Improved Documentation
|
||||||
|
- [#2712](https://github.com/sanic-org/sanic/pull/2712) Improved example using ``'https'`` to create the redirect
|
33
docs/sanic/releases/23/23.6.md
Normal file
33
docs/sanic/releases/23/23.6.md
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
## Version 23.6.0 🔶
|
||||||
|
|
||||||
|
### Features
|
||||||
|
- [#2670](https://github.com/sanic-org/sanic/pull/2670) Increase `KEEP_ALIVE_TIMEOUT` default to 120 seconds
|
||||||
|
- [#2716](https://github.com/sanic-org/sanic/pull/2716) Adding allow route overwrite option in blueprint
|
||||||
|
- [#2724](https://github.com/sanic-org/sanic/pull/2724) and [#2792](https://github.com/sanic-org/sanic/pull/2792) Add a new exception signal for ALL exceptions raised anywhere in application
|
||||||
|
- [#2727](https://github.com/sanic-org/sanic/pull/2727) Add name prefixing to BP groups
|
||||||
|
- [#2754](https://github.com/sanic-org/sanic/pull/2754) Update request type on middleware types
|
||||||
|
- [#2770](https://github.com/sanic-org/sanic/pull/2770) Better exception message on startup time application induced import error
|
||||||
|
- [#2776](https://github.com/sanic-org/sanic/pull/2776) Set multiprocessing start method early
|
||||||
|
- [#2785](https://github.com/sanic-org/sanic/pull/2785) Add custom typing to config and ctx objects
|
||||||
|
- [#2790](https://github.com/sanic-org/sanic/pull/2790) Add `request.client_ip`
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
- [#2728](https://github.com/sanic-org/sanic/pull/2728) Fix traversals for intended results
|
||||||
|
- [#2729](https://github.com/sanic-org/sanic/pull/2729) Handle case when headers argument of ResponseStream constructor is None
|
||||||
|
- [#2737](https://github.com/sanic-org/sanic/pull/2737) Fix type annotation for `JSONREsponse` default content type
|
||||||
|
- [#2740](https://github.com/sanic-org/sanic/pull/2740) Use Sanic's serializer for JSON responses in the Inspector
|
||||||
|
- [#2760](https://github.com/sanic-org/sanic/pull/2760) Support for `Request.get_current` in ASGI mode
|
||||||
|
- [#2773](https://github.com/sanic-org/sanic/pull/2773) Alow Blueprint routes to explicitly define error_format
|
||||||
|
- [#2774](https://github.com/sanic-org/sanic/pull/2774) Resolve headers on different renderers
|
||||||
|
- [#2782](https://github.com/sanic-org/sanic/pull/2782) Resolve pypy compatibility issues
|
||||||
|
|
||||||
|
### Deprecations and Removals
|
||||||
|
- [#2777](https://github.com/sanic-org/sanic/pull/2777) Remove Python 3.7 support
|
||||||
|
|
||||||
|
### Developer infrastructure
|
||||||
|
- [#2766](https://github.com/sanic-org/sanic/pull/2766) Unpin setuptools version
|
||||||
|
- [#2779](https://github.com/sanic-org/sanic/pull/2779) Run keep alive tests in loop to get available port
|
||||||
|
|
||||||
|
### Improved Documentation
|
||||||
|
- [#2741](https://github.com/sanic-org/sanic/pull/2741) Better documentation examples about running Sanic
|
||||||
|
From that list, the items to highlight in the release notes:
|
|
@ -1,270 +0,0 @@
|
||||||
Request Data
|
|
||||||
============
|
|
||||||
|
|
||||||
When an endpoint receives a HTTP request, the route function is passed a
|
|
||||||
`Request` object.
|
|
||||||
|
|
||||||
The following variables are accessible as properties on `Request` objects:
|
|
||||||
|
|
||||||
- `json` (any) - JSON body
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
@app.route("/json")
|
|
||||||
def post_json(request):
|
|
||||||
return json({ "received": True, "message": request.json })
|
|
||||||
|
|
||||||
|
|
||||||
- `args` (dict) - Query string variables. A query string is the section of a
|
|
||||||
URL that resembles ``?key1=value1&key2=value2``.
|
|
||||||
If that URL were to be parsed, the `args` dictionary would look like `{'key1': ['value1'], 'key2': ['value2']}`.
|
|
||||||
The request's `query_string` variable holds the unparsed string value. Property is providing the default parsing
|
|
||||||
strategy. If you would like to change it look to the section below (`Changing the default parsing rules of the queryset`).
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
@app.route("/query_string")
|
|
||||||
def query_string(request):
|
|
||||||
return json({ "parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string })
|
|
||||||
|
|
||||||
- `query_args` (list) - On many cases you would need to access the url arguments in
|
|
||||||
a less packed form. `query_args` is the list of `(key, value)` tuples.
|
|
||||||
Property is providing the default parsing strategy. If you would like to change it look to the section below
|
|
||||||
(`Changing the default parsing rules of the queryset`). For the same previous URL queryset `?key1=value1&key2=value2`,
|
|
||||||
the `query_args` list would look like `[('key1', 'value1'), ('key2', 'value2')]`. And in case of the multiple params
|
|
||||||
with the same key like `?key1=value1&key2=value2&key1=value3` the `query_args` list would look like
|
|
||||||
`[('key1', 'value1'), ('key2', 'value2'), ('key1', 'value3')]`.
|
|
||||||
|
|
||||||
The difference between Request.args and Request.query_args for the queryset `?key1=value1&key2=value2&key1=value3`
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/test_request_args")
|
|
||||||
async def test_request_args(request):
|
|
||||||
return json({
|
|
||||||
"parsed": True,
|
|
||||||
"url": request.url,
|
|
||||||
"query_string": request.query_string,
|
|
||||||
"args": request.args,
|
|
||||||
"query_args": request.query_args,
|
|
||||||
})
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run(host="0.0.0.0", port=8000)
|
|
||||||
|
|
||||||
Output
|
|
||||||
|
|
||||||
.. code-block:: json
|
|
||||||
|
|
||||||
{
|
|
||||||
"parsed":true,
|
|
||||||
"url":"http:\/\/0.0.0.0:8000\/test_request_args?key1=value1&key2=value2&key1=value3",
|
|
||||||
"query_string":"key1=value1&key2=value2&key1=value3",
|
|
||||||
"args":{"key1":["value1","value3"],"key2":["value2"]},
|
|
||||||
"query_args":[["key1","value1"],["key2","value2"],["key1","value3"]]
|
|
||||||
}
|
|
||||||
|
|
||||||
- `files` (dictionary of `File` objects) - List of files that have a name, body, and type
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
@app.route("/files")
|
|
||||||
def post_json(request):
|
|
||||||
test_file = request.files.get('test')
|
|
||||||
|
|
||||||
file_parameters = {
|
|
||||||
'body': test_file.body,
|
|
||||||
'name': test_file.name,
|
|
||||||
'type': test_file.type,
|
|
||||||
}
|
|
||||||
|
|
||||||
return json({ "received": True, "file_names": request.files.keys(), "test_file_parameters": file_parameters })
|
|
||||||
|
|
||||||
- `form` (dict) - Posted form variables.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
@app.route("/form")
|
|
||||||
def post_json(request):
|
|
||||||
return json({ "received": True, "form_data": request.form, "test": request.form.get('test') })
|
|
||||||
|
|
||||||
- `body` (bytes) - Posted raw body. This property allows retrieval of the
|
|
||||||
request's raw data, regardless of content type.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
@app.route("/users", methods=["POST",])
|
|
||||||
def create_user(request):
|
|
||||||
return text("You are trying to create a user with the following POST: %s" % request.body)
|
|
||||||
|
|
||||||
|
|
||||||
- `headers` (dict) - A case-insensitive dictionary that contains the request headers.
|
|
||||||
|
|
||||||
- `method` (str) - HTTP method of the request (ie `GET`, `POST`).
|
|
||||||
|
|
||||||
- `ip` (str) - IP address of the requester.
|
|
||||||
|
|
||||||
- `port` (str) - Port address of the requester.
|
|
||||||
|
|
||||||
- `socket` (tuple) - (IP, port) of the requester.
|
|
||||||
|
|
||||||
- `app` - a reference to the Sanic application object that is handling this request. This is useful when inside blueprints or other handlers in modules that do not have access to the global `app` object.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import json
|
|
||||||
from sanic import Blueprint
|
|
||||||
|
|
||||||
bp = Blueprint('my_blueprint')
|
|
||||||
|
|
||||||
@bp.route('/')
|
|
||||||
async def bp_root(request):
|
|
||||||
if request.app.config['DEBUG']:
|
|
||||||
return json({'status': 'debug'})
|
|
||||||
else:
|
|
||||||
return json({'status': 'production'})
|
|
||||||
|
|
||||||
- `url`: The full URL of the request, ie: `http://localhost:8000/posts/1/?foo=bar`
|
|
||||||
- `scheme`: The URL scheme associated with the request: 'http|https|ws|wss' or arbitrary value given by the headers.
|
|
||||||
- `host`: The host associated with the request(which in the `Host` header): `localhost:8080`
|
|
||||||
- `server_name`: The hostname of the server, without port number. the value is seeked in this order: `config.SERVER_NAME`, `x-forwarded-host` header, :func:`Request.host`
|
|
||||||
- `server_port`: Like `server_name`. Seeked in this order: `x-forwarded-port` header, :func:`Request.host`, actual port used by the transport layer socket.
|
|
||||||
- `path`: The path of the request: `/posts/1/`
|
|
||||||
- `query_string`: The query string of the request: `foo=bar` or a blank string `''`
|
|
||||||
- `uri_template`: Template for matching route handler: `/posts/<id>/`
|
|
||||||
- `token`: The value of Authorization header: `Basic YWRtaW46YWRtaW4=`
|
|
||||||
|
|
||||||
- `url_for`: Just like `sanic.Sanic.url_for`, but automatically determine `scheme` and `netloc` base on the request. Since this method is aiming to generate correct schema & netloc, `_external` is implied.
|
|
||||||
|
|
||||||
|
|
||||||
Changing the default parsing rules of the queryset
|
|
||||||
--------------------------------------------------
|
|
||||||
|
|
||||||
The default parameters that are using internally in `args` and `query_args` properties to parse queryset:
|
|
||||||
|
|
||||||
- `keep_blank_values` (bool): `False` - flag indicating whether blank values in
|
|
||||||
percent-encoded queries should be treated as blank strings.
|
|
||||||
A true value indicates that blanks should be retained as blank
|
|
||||||
strings. The default false value indicates that blank values
|
|
||||||
are to be ignored and treated as if they were not included.
|
|
||||||
- `strict_parsing` (bool): `False` - flag indicating what to do with parsing errors. If
|
|
||||||
false (the default), errors are silently ignored. If true,
|
|
||||||
errors raise a ValueError exception.
|
|
||||||
- `encoding` and `errors` (str): 'utf-8' and 'replace' - specify how to decode percent-encoded sequences
|
|
||||||
into Unicode characters, as accepted by the bytes.decode() method.
|
|
||||||
|
|
||||||
If you would like to change that default parameters you could call `get_args` and `get_query_args` methods
|
|
||||||
with the new values.
|
|
||||||
|
|
||||||
For the queryset `/?test1=value1&test2=&test3=value3`:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
@app.route("/query_string")
|
|
||||||
def query_string(request):
|
|
||||||
args_with_blank_values = request.get_args(keep_blank_values=True)
|
|
||||||
return json({
|
|
||||||
"parsed": True,
|
|
||||||
"url": request.url,
|
|
||||||
"args_with_blank_values": args_with_blank_values,
|
|
||||||
"query_string": request.query_string
|
|
||||||
})
|
|
||||||
|
|
||||||
The output will be:
|
|
||||||
|
|
||||||
.. code-block:: JSON
|
|
||||||
|
|
||||||
{
|
|
||||||
"parsed": true,
|
|
||||||
"url": "http:\/\/0.0.0.0:8000\/query_string?test1=value1&test2=&test3=value3",
|
|
||||||
"args_with_blank_values": {"test1": ["value1"], "test2": "", "test3": ["value3"]},
|
|
||||||
"query_string": "test1=value1&test2=&test3=value3"
|
|
||||||
}
|
|
||||||
|
|
||||||
Accessing values using `get` and `getlist`
|
|
||||||
------------------------------------------
|
|
||||||
|
|
||||||
The `request.args` returns a subclass of `dict` called `RequestParameters`.
|
|
||||||
The key difference when using this object is the distinction between the `get` and `getlist` methods.
|
|
||||||
|
|
||||||
- `get(key, default=None)` operates as normal, except that when the value of
|
|
||||||
the given key is a list, *only the first item is returned*.
|
|
||||||
- `getlist(key, default=None)` operates as normal, *returning the entire list*.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.request import RequestParameters
|
|
||||||
|
|
||||||
args = RequestParameters()
|
|
||||||
args['titles'] = ['Post 1', 'Post 2']
|
|
||||||
|
|
||||||
args.get('titles') # => 'Post 1'
|
|
||||||
|
|
||||||
args.getlist('titles') # => ['Post 1', 'Post 2']
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
def get_handler(request):
|
|
||||||
return json({
|
|
||||||
"p1": request.args.getlist("p1")
|
|
||||||
})
|
|
||||||
|
|
||||||
Accessing the handler name with the request.endpoint attribute
|
|
||||||
--------------------------------------------------------------
|
|
||||||
|
|
||||||
The `request.endpoint` attribute holds the handler's name. For instance, the below
|
|
||||||
route will return "hello".
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import text
|
|
||||||
from sanic import Sanic
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
@app.get("/")
|
|
||||||
def hello(request):
|
|
||||||
return text(request.endpoint)
|
|
||||||
|
|
||||||
Or, with a blueprint it will be include both, separated by a period. For example, the below route would return foo.bar:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic import Blueprint
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
blueprint = Blueprint('foo')
|
|
||||||
|
|
||||||
@blueprint.get('/')
|
|
||||||
async def bar(request):
|
|
||||||
return text(request.endpoint)
|
|
||||||
|
|
||||||
app.blueprint(blueprint)
|
|
||||||
|
|
||||||
app.run(host="0.0.0.0", port=8000, debug=True)
|
|
|
@ -1,139 +0,0 @@
|
||||||
Response
|
|
||||||
========
|
|
||||||
|
|
||||||
Use functions in `sanic.response` module to create responses.
|
|
||||||
|
|
||||||
Plain Text
|
|
||||||
----------
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/text')
|
|
||||||
def handle_request(request):
|
|
||||||
return response.text('Hello world!')
|
|
||||||
|
|
||||||
HTML
|
|
||||||
----
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/html')
|
|
||||||
def handle_request(request):
|
|
||||||
return response.html('<p>Hello world!</p>')
|
|
||||||
|
|
||||||
JSON
|
|
||||||
----
|
|
||||||
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/json')
|
|
||||||
def handle_request(request):
|
|
||||||
return response.json({'message': 'Hello world!'})
|
|
||||||
|
|
||||||
File
|
|
||||||
----
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/file')
|
|
||||||
async def handle_request(request):
|
|
||||||
return await response.file('/srv/www/whatever.png')
|
|
||||||
|
|
||||||
Streaming
|
|
||||||
---------
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
@app.route("/streaming")
|
|
||||||
async def index(request):
|
|
||||||
async def streaming_fn(response):
|
|
||||||
await response.write('foo')
|
|
||||||
await response.write('bar')
|
|
||||||
return response.stream(streaming_fn, content_type='text/plain')
|
|
||||||
|
|
||||||
See `Streaming <streaming.html>`_ for more information.
|
|
||||||
|
|
||||||
File Streaming
|
|
||||||
--------------
|
|
||||||
|
|
||||||
For large files, a combination of File and Streaming above
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
@app.route('/big_file.png')
|
|
||||||
async def handle_request(request):
|
|
||||||
return await response.file_stream('/srv/www/whatever.png')
|
|
||||||
|
|
||||||
Redirect
|
|
||||||
--------
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/redirect')
|
|
||||||
def handle_request(request):
|
|
||||||
return response.redirect('/json')
|
|
||||||
|
|
||||||
Raw
|
|
||||||
---
|
|
||||||
|
|
||||||
Response without encoding the body
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/raw')
|
|
||||||
def handle_request(request):
|
|
||||||
return response.raw(b'raw data')
|
|
||||||
|
|
||||||
Empty
|
|
||||||
--------------
|
|
||||||
|
|
||||||
For responding with an empty message as defined by `RFC 2616 <https://tools.ietf.org/search/rfc2616#section-7.2.1>`_
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
@app.route('/empty')
|
|
||||||
async def handle_request(request):
|
|
||||||
return response.empty()
|
|
||||||
|
|
||||||
Modify headers or status
|
|
||||||
------------------------
|
|
||||||
|
|
||||||
To modify headers or status code, pass the `headers` or `status` argument to those functions:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/json')
|
|
||||||
def handle_request(request):
|
|
||||||
return response.json(
|
|
||||||
{'message': 'Hello world!'},
|
|
||||||
headers={'X-Served-By': 'sanic'},
|
|
||||||
status=200
|
|
||||||
)
|
|
|
@ -1,433 +0,0 @@
|
||||||
Routing
|
|
||||||
-------
|
|
||||||
|
|
||||||
Routing allows the user to specify handler functions for different URL endpoints.
|
|
||||||
|
|
||||||
A basic route looks like the following, where `app` is an instance of the
|
|
||||||
`Sanic` class:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
async def test(request):
|
|
||||||
return json({ "hello": "world" })
|
|
||||||
|
|
||||||
When the url `http://server.url/` is accessed (the base url of the server), the
|
|
||||||
final `/` is matched by the router to the handler function, `test`, which then
|
|
||||||
returns a JSON object.
|
|
||||||
|
|
||||||
Sanic handler functions must be defined using the `async def` syntax, as they
|
|
||||||
are asynchronous functions.
|
|
||||||
|
|
||||||
Request parameters
|
|
||||||
==================
|
|
||||||
|
|
||||||
Sanic comes with a basic router that supports request parameters.
|
|
||||||
|
|
||||||
To specify a parameter, surround it with angle quotes like so: `<PARAM>`.
|
|
||||||
Request parameters will be passed to the route handler functions as keyword
|
|
||||||
arguments.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
@app.route('/tag/<tag>')
|
|
||||||
async def tag_handler(request, tag):
|
|
||||||
return text('Tag - {}'.format(tag))
|
|
||||||
|
|
||||||
To specify a type for the parameter, add a `:type` after the parameter name,
|
|
||||||
inside the quotes. If the parameter does not match the specified type, Sanic
|
|
||||||
will throw a `NotFound` exception, resulting in a `404: Page not found` error
|
|
||||||
on the URL.
|
|
||||||
|
|
||||||
Supported types
|
|
||||||
~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
* `string`
|
|
||||||
* "Bob"
|
|
||||||
* "Python 3"
|
|
||||||
* `int`
|
|
||||||
* 10
|
|
||||||
* 20
|
|
||||||
* 30
|
|
||||||
* -10
|
|
||||||
* (No floats work here)
|
|
||||||
* `number`
|
|
||||||
* 1
|
|
||||||
* 1.5
|
|
||||||
* 10
|
|
||||||
* -10
|
|
||||||
* `alpha`
|
|
||||||
* "Bob"
|
|
||||||
* "Python"
|
|
||||||
* (If it contains a symbol or a non alphanumeric character it will fail)
|
|
||||||
* `path`
|
|
||||||
* "hello"
|
|
||||||
* "hello.text"
|
|
||||||
* "hello world"
|
|
||||||
* `uuid`
|
|
||||||
* 123a123a-a12a-1a1a-a1a1-1a12a1a12345 (UUIDv4 Support)
|
|
||||||
* `regex expression`
|
|
||||||
|
|
||||||
If no type is set then a string is expected. The argument given to the function will always be a string, independent of the type.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
@app.route('/string/<string_arg:string>')
|
|
||||||
async def string_handler(request, string_arg):
|
|
||||||
return text('String - {}'.format(string_arg))
|
|
||||||
|
|
||||||
@app.route('/int/<integer_arg:int>')
|
|
||||||
async def integer_handler(request, integer_arg):
|
|
||||||
return text('Integer - {}'.format(integer_arg))
|
|
||||||
|
|
||||||
@app.route('/number/<number_arg:number>')
|
|
||||||
async def number_handler(request, number_arg):
|
|
||||||
return text('Number - {}'.format(number_arg))
|
|
||||||
|
|
||||||
@app.route('/alpha/<alpha_arg:alpha>')
|
|
||||||
async def number_handler(request, alpha_arg):
|
|
||||||
return text('Alpha - {}'.format(alpha_arg))
|
|
||||||
|
|
||||||
@app.route('/path/<path_arg:path>')
|
|
||||||
async def number_handler(request, path_arg):
|
|
||||||
return text('Path - {}'.format(path_arg))
|
|
||||||
|
|
||||||
@app.route('/uuid/<uuid_arg:uuid>')
|
|
||||||
async def number_handler(request, uuid_arg):
|
|
||||||
return text('Uuid - {}'.format(uuid_arg))
|
|
||||||
|
|
||||||
@app.route('/person/<name:[A-z]+>')
|
|
||||||
async def person_handler(request, name):
|
|
||||||
return text('Person - {}'.format(name))
|
|
||||||
|
|
||||||
@app.route('/folder/<folder_id:[A-z0-9]{0,4}>')
|
|
||||||
async def folder_handler(request, folder_id):
|
|
||||||
return text('Folder - {}'.format(folder_id))
|
|
||||||
|
|
||||||
.. warning::
|
|
||||||
|
|
||||||
`str` is not a valid type tag. If you want `str` recognition then you must use `string`
|
|
||||||
|
|
||||||
HTTP request types
|
|
||||||
==================
|
|
||||||
|
|
||||||
By default, a route defined on a URL will be available for only GET requests to that URL.
|
|
||||||
However, the `@app.route` decorator accepts an optional parameter, `methods`,
|
|
||||||
which allows the handler function to work with any of the HTTP methods in the list.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
@app.route('/post', methods=['POST'])
|
|
||||||
async def post_handler(request):
|
|
||||||
return text('POST request - {}'.format(request.json))
|
|
||||||
|
|
||||||
@app.route('/get', methods=['GET'])
|
|
||||||
async def get_handler(request):
|
|
||||||
return text('GET request - {}'.format(request.args))
|
|
||||||
|
|
||||||
There is also an optional `host` argument (which can be a list or a string). This restricts a route to the host or hosts provided. If there is also a route with no host, it will be the default.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@app.route('/get', methods=['GET'], host='example.com')
|
|
||||||
async def get_handler(request):
|
|
||||||
return text('GET request - {}'.format(request.args))
|
|
||||||
|
|
||||||
# if the host header doesn't match example.com, this route will be used
|
|
||||||
@app.route('/get', methods=['GET'])
|
|
||||||
async def get_handler(request):
|
|
||||||
return text('GET request in default - {}'.format(request.args))
|
|
||||||
|
|
||||||
There are also shorthand method decorators:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
@app.post('/post')
|
|
||||||
async def post_handler(request):
|
|
||||||
return text('POST request - {}'.format(request.json))
|
|
||||||
|
|
||||||
@app.get('/get')
|
|
||||||
async def get_handler(request):
|
|
||||||
return text('GET request - {}'.format(request.args))
|
|
||||||
|
|
||||||
The `add_route` method
|
|
||||||
======================
|
|
||||||
|
|
||||||
As we have seen, routes are often specified using the `@app.route` decorator.
|
|
||||||
However, this decorator is really just a wrapper for the `app.add_route`
|
|
||||||
method, which is used as follows:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
# Define the handler functions
|
|
||||||
async def handler1(request):
|
|
||||||
return text('OK')
|
|
||||||
|
|
||||||
async def handler2(request, name):
|
|
||||||
return text('Folder - {}'.format(name))
|
|
||||||
|
|
||||||
async def person_handler2(request, name):
|
|
||||||
return text('Person - {}'.format(name))
|
|
||||||
|
|
||||||
# Add each handler function as a route
|
|
||||||
app.add_route(handler1, '/test')
|
|
||||||
app.add_route(handler2, '/folder/<name>')
|
|
||||||
app.add_route(person_handler2, '/person/<name:[A-z]>', methods=['GET'])
|
|
||||||
|
|
||||||
URL building with `url_for`
|
|
||||||
===========================
|
|
||||||
|
|
||||||
Sanic provides a `url_for` method, to generate URLs based on the handler method name. This is useful if you want to avoid hardcoding url paths into your app; instead, you can just reference the handler name. For example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic.response import redirect
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
async def index(request):
|
|
||||||
# generate a URL for the endpoint `post_handler`
|
|
||||||
url = app.url_for('post_handler', post_id=5)
|
|
||||||
# the URL is `/posts/5`, redirect to it
|
|
||||||
return redirect(url)
|
|
||||||
|
|
||||||
@app.route('/posts/<post_id>')
|
|
||||||
async def post_handler(request, post_id):
|
|
||||||
return text('Post - {}'.format(post_id))
|
|
||||||
|
|
||||||
Other things to keep in mind when using `url_for`:
|
|
||||||
|
|
||||||
- Keyword arguments passed to `url_for` that are not request parameters will be included in the URL's query string. For example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
url = app.url_for('post_handler', post_id=5, arg_one='one', arg_two='two')
|
|
||||||
# /posts/5?arg_one=one&arg_two=two
|
|
||||||
|
|
||||||
- Multivalue argument can be passed to `url_for`. For example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'])
|
|
||||||
# /posts/5?arg_one=one&arg_one=two
|
|
||||||
|
|
||||||
- Also some special arguments (`_anchor`, `_external`, `_scheme`, `_method`, `_server`) passed to `url_for` will have special url building (`_method` is not supported now and will be ignored). For example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
url = app.url_for('post_handler', post_id=5, arg_one='one', _anchor='anchor')
|
|
||||||
# /posts/5?arg_one=one#anchor
|
|
||||||
|
|
||||||
url = app.url_for('post_handler', post_id=5, arg_one='one', _external=True)
|
|
||||||
# //server/posts/5?arg_one=one
|
|
||||||
# _external requires you to pass an argument _server or set SERVER_NAME in app.config if not url will be same as no _external
|
|
||||||
|
|
||||||
url = app.url_for('post_handler', post_id=5, arg_one='one', _scheme='http', _external=True)
|
|
||||||
# http://server/posts/5?arg_one=one
|
|
||||||
# when specifying _scheme, _external must be True
|
|
||||||
|
|
||||||
# you can pass all special arguments at once
|
|
||||||
url = app.url_for('post_handler', post_id=5, arg_one=['one', 'two'], arg_two=2, _anchor='anchor', _scheme='http', _external=True, _server='another_server:8888')
|
|
||||||
# http://another_server:8888/posts/5?arg_one=one&arg_one=two&arg_two=2#anchor
|
|
||||||
|
|
||||||
- All valid parameters must be passed to `url_for` to build a URL. If a parameter is not supplied, or if a parameter does not match the specified type, a `URLBuildError` will be raised.
|
|
||||||
|
|
||||||
WebSocket routes
|
|
||||||
================
|
|
||||||
|
|
||||||
Routes for the WebSocket protocol can be defined with the `@app.websocket`
|
|
||||||
decorator:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@app.websocket('/feed')
|
|
||||||
async def feed(request, ws):
|
|
||||||
while True:
|
|
||||||
data = 'hello!'
|
|
||||||
print('Sending: ' + data)
|
|
||||||
await ws.send(data)
|
|
||||||
data = await ws.recv()
|
|
||||||
print('Received: ' + data)
|
|
||||||
|
|
||||||
Alternatively, the `app.add_websocket_route` method can be used instead of the
|
|
||||||
decorator:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
async def feed(request, ws):
|
|
||||||
pass
|
|
||||||
|
|
||||||
app.add_websocket_route(my_websocket_handler, '/feed')
|
|
||||||
|
|
||||||
Handlers to a WebSocket route are invoked with the request as first argument, and a
|
|
||||||
WebSocket protocol object as second argument. The protocol object has `send`
|
|
||||||
and `recv` methods to send and receive data respectively.
|
|
||||||
|
|
||||||
WebSocket support requires the `websockets <https://github.com/aaugustin/websockets>`_
|
|
||||||
package by Aymeric Augustin.
|
|
||||||
|
|
||||||
|
|
||||||
About `strict_slashes`
|
|
||||||
======================
|
|
||||||
|
|
||||||
You can make `routes` strict to trailing slash or not, it's configurable.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# provide default strict_slashes value for all routes
|
|
||||||
app = Sanic('test_route_strict_slash', strict_slashes=True)
|
|
||||||
|
|
||||||
# you can also overwrite strict_slashes value for specific route
|
|
||||||
@app.get('/get', strict_slashes=False)
|
|
||||||
def handler(request):
|
|
||||||
return text('OK')
|
|
||||||
|
|
||||||
# It also works for blueprints
|
|
||||||
bp = Blueprint('test_bp_strict_slash', strict_slashes=True)
|
|
||||||
|
|
||||||
@bp.get('/bp/get', strict_slashes=False)
|
|
||||||
def handler(request):
|
|
||||||
return text('OK')
|
|
||||||
|
|
||||||
app.blueprint(bp)
|
|
||||||
|
|
||||||
The behavior of how the `strict_slashes` flag follows a defined hierarchy which decides if a specific route
|
|
||||||
falls under the `strict_slashes` behavior.
|
|
||||||
|
|
||||||
| Route/
|
|
||||||
| ├──Blueprint/
|
|
||||||
| ├──Application/
|
|
||||||
|
|
||||||
Above hierarchy defines how the `strict_slashes` flag will behave. The first non `None` value of the `strict_slashes`
|
|
||||||
found in the above order will be applied to the route in question.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic, Blueprint
|
|
||||||
from sanic.response import text
|
|
||||||
|
|
||||||
app = Sanic("sample_strict_slashes", strict_slashes=True)
|
|
||||||
|
|
||||||
@app.get("/r1")
|
|
||||||
def r1(request):
|
|
||||||
return text("strict_slashes is applicable from App level")
|
|
||||||
|
|
||||||
@app.get("/r2", strict_slashes=False)
|
|
||||||
def r2(request):
|
|
||||||
return text("strict_slashes is not applicable due to False value set in route level")
|
|
||||||
|
|
||||||
bp = Blueprint("bp", strict_slashes=False)
|
|
||||||
|
|
||||||
@bp.get("/r3", strict_slashes=True)
|
|
||||||
def r3(request):
|
|
||||||
return text("strict_slashes applicable from blueprint route level")
|
|
||||||
|
|
||||||
bp1 = Blueprint("bp1", strict_slashes=True)
|
|
||||||
|
|
||||||
@bp.get("/r4")
|
|
||||||
def r3(request):
|
|
||||||
return text("strict_slashes applicable from blueprint level")
|
|
||||||
|
|
||||||
User defined route name
|
|
||||||
=======================
|
|
||||||
|
|
||||||
A custom route name can be used by passing a `name` argument while registering the route which will
|
|
||||||
override the default route name generated using the `handler.__name__` attribute.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app = Sanic('test_named_route')
|
|
||||||
|
|
||||||
@app.get('/get', name='get_handler')
|
|
||||||
def handler(request):
|
|
||||||
return text('OK')
|
|
||||||
|
|
||||||
# then you need use `app.url_for('get_handler')`
|
|
||||||
# instead of # `app.url_for('handler')`
|
|
||||||
|
|
||||||
# It also works for blueprints
|
|
||||||
bp = Blueprint('test_named_bp')
|
|
||||||
|
|
||||||
@bp.get('/bp/get', name='get_handler')
|
|
||||||
def handler(request):
|
|
||||||
return text('OK')
|
|
||||||
|
|
||||||
app.blueprint(bp)
|
|
||||||
|
|
||||||
# then you need use `app.url_for('test_named_bp.get_handler')`
|
|
||||||
# instead of `app.url_for('test_named_bp.handler')`
|
|
||||||
|
|
||||||
# different names can be used for same url with different methods
|
|
||||||
|
|
||||||
@app.get('/test', name='route_test')
|
|
||||||
def handler(request):
|
|
||||||
return text('OK')
|
|
||||||
|
|
||||||
@app.post('/test', name='route_post')
|
|
||||||
def handler2(request):
|
|
||||||
return text('OK POST')
|
|
||||||
|
|
||||||
@app.put('/test', name='route_put')
|
|
||||||
def handler3(request):
|
|
||||||
return text('OK PUT')
|
|
||||||
|
|
||||||
# below url are the same, you can use any of them
|
|
||||||
# '/test'
|
|
||||||
app.url_for('route_test')
|
|
||||||
# app.url_for('route_post')
|
|
||||||
# app.url_for('route_put')
|
|
||||||
|
|
||||||
# for same handler name with different methods
|
|
||||||
# you need specify the name (it's url_for issue)
|
|
||||||
@app.get('/get')
|
|
||||||
def handler(request):
|
|
||||||
return text('OK')
|
|
||||||
|
|
||||||
@app.post('/post', name='post_handler')
|
|
||||||
def handler(request):
|
|
||||||
return text('OK')
|
|
||||||
|
|
||||||
# then
|
|
||||||
# app.url_for('handler') == '/get'
|
|
||||||
# app.url_for('post_handler') == '/post'
|
|
||||||
|
|
||||||
Build URL for static files
|
|
||||||
==========================
|
|
||||||
|
|
||||||
Sanic supports using `url_for` method to build static file urls. In case if the static url
|
|
||||||
is pointing to a directory, `filename` parameter to the `url_for` can be ignored.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
app = Sanic('test_static')
|
|
||||||
app.static('/static', './static')
|
|
||||||
app.static('/uploads', './uploads', name='uploads')
|
|
||||||
app.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
|
|
||||||
|
|
||||||
bp = Blueprint('bp', url_prefix='bp')
|
|
||||||
bp.static('/static', './static')
|
|
||||||
bp.static('/uploads', './uploads', name='uploads')
|
|
||||||
bp.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
|
|
||||||
app.blueprint(bp)
|
|
||||||
|
|
||||||
# then build the url
|
|
||||||
app.url_for('static', filename='file.txt') == '/static/file.txt'
|
|
||||||
app.url_for('static', name='static', filename='file.txt') == '/static/file.txt'
|
|
||||||
app.url_for('static', name='uploads', filename='file.txt') == '/uploads/file.txt'
|
|
||||||
app.url_for('static', name='best_png') == '/the_best.png'
|
|
||||||
|
|
||||||
# blueprint url building
|
|
||||||
app.url_for('static', name='bp.static', filename='file.txt') == '/bp/static/file.txt'
|
|
||||||
app.url_for('static', name='bp.uploads', filename='file.txt') == '/bp/uploads/file.txt'
|
|
||||||
app.url_for('static', name='bp.best_png') == '/bp/static/the_best.png'
|
|
|
@ -1,66 +0,0 @@
|
||||||
Sockets
|
|
||||||
=======
|
|
||||||
|
|
||||||
Sanic can use the python
|
|
||||||
`socket module <https://docs.python.org/3/library/socket.html>`_ to accommodate
|
|
||||||
non IPv4 sockets.
|
|
||||||
|
|
||||||
IPv6 example:
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import json
|
|
||||||
import socket
|
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
|
||||||
sock.bind(('::', 7777))
|
|
||||||
|
|
||||||
app = Sanic("ipv6_example")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
async def test(request):
|
|
||||||
return json({"hello": "world"})
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run(sock=sock)
|
|
||||||
|
|
||||||
to test IPv6 ``curl -g -6 "http://[::1]:7777/"``
|
|
||||||
|
|
||||||
|
|
||||||
UNIX socket example:
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
import signal
|
|
||||||
import sys
|
|
||||||
import socket
|
|
||||||
import os
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import json
|
|
||||||
|
|
||||||
|
|
||||||
server_socket = '/tmp/sanic.sock'
|
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
||||||
sock.bind(server_socket)
|
|
||||||
|
|
||||||
app = Sanic("unix_socket_example")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
async def test(request):
|
|
||||||
return json({"hello": "world"})
|
|
||||||
|
|
||||||
|
|
||||||
def signal_handler(sig, frame):
|
|
||||||
print('Exiting')
|
|
||||||
os.unlink(server_socket)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run(sock=sock)
|
|
||||||
|
|
||||||
to test UNIX: ``curl -v --unix-socket /tmp/sanic.sock http://localhost/hello``
|
|
|
@ -1,20 +0,0 @@
|
||||||
SSL Example
|
|
||||||
-----------
|
|
||||||
|
|
||||||
Optionally pass in an SSLContext:
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
import ssl
|
|
||||||
context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
|
|
||||||
context.load_cert_chain("/path/to/cert", keyfile="/path/to/keyfile")
|
|
||||||
|
|
||||||
app.run(host="0.0.0.0", port=8443, ssl=context)
|
|
||||||
|
|
||||||
You can also pass in the locations of a certificate and key as a dictionary:
|
|
||||||
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
ssl = {'cert': "/path/to/cert", 'key': "/path/to/keyfile"}
|
|
||||||
app.run(host="0.0.0.0", port=8443, ssl=ssl)
|
|
|
@ -1,92 +0,0 @@
|
||||||
Static Files
|
|
||||||
============
|
|
||||||
|
|
||||||
Static files and directories, such as an image file, are served by Sanic when
|
|
||||||
registered with the `app.static()` method. The method takes an endpoint URL and a
|
|
||||||
filename. The file specified will then be accessible via the given endpoint.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.blueprints import Blueprint
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
# Serves files from the static folder to the URL /static
|
|
||||||
app.static('/static', './static')
|
|
||||||
# use url_for to build the url, name defaults to 'static' and can be ignored
|
|
||||||
app.url_for('static', filename='file.txt') == '/static/file.txt'
|
|
||||||
app.url_for('static', name='static', filename='file.txt') == '/static/file.txt'
|
|
||||||
|
|
||||||
# Serves the file /home/ubuntu/test.png when the URL /the_best.png
|
|
||||||
# is requested
|
|
||||||
app.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
|
|
||||||
|
|
||||||
# you can use url_for to build the static file url
|
|
||||||
# you can ignore name and filename parameters if you don't define it
|
|
||||||
app.url_for('static', name='best_png') == '/the_best.png'
|
|
||||||
app.url_for('static', name='best_png', filename='any') == '/the_best.png'
|
|
||||||
|
|
||||||
# you need define the name for other static files
|
|
||||||
app.static('/another.png', '/home/ubuntu/another.png', name='another')
|
|
||||||
app.url_for('static', name='another') == '/another.png'
|
|
||||||
app.url_for('static', name='another', filename='any') == '/another.png'
|
|
||||||
|
|
||||||
# also, you can use static for blueprint
|
|
||||||
bp = Blueprint('bp', url_prefix='/bp')
|
|
||||||
bp.static('/static', './static')
|
|
||||||
|
|
||||||
# specify a different content_type for your files
|
|
||||||
# such as adding 'charset'
|
|
||||||
app.static('/', '/public/index.html', content_type="text/html; charset=utf-8")
|
|
||||||
|
|
||||||
# servers the file directly
|
|
||||||
bp.static('/the_best.png', '/home/ubuntu/test.png', name='best_png')
|
|
||||||
app.blueprint(bp)
|
|
||||||
|
|
||||||
app.url_for('static', name='bp.static', filename='file.txt') == '/bp/static/file.txt'
|
|
||||||
app.url_for('static', name='bp.best_png') == '/bp/test_best.png'
|
|
||||||
|
|
||||||
app.run(host="0.0.0.0", port=8000)
|
|
||||||
|
|
||||||
> **Note:** Sanic does not provide directory index when you serve a static directory.
|
|
||||||
|
|
||||||
Virtual Host
|
|
||||||
------------
|
|
||||||
|
|
||||||
The `app.static()` method also support **virtual host**. You can serve your static files with specific **virtual host** with `host` argument. For example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
app.static('/static', './static')
|
|
||||||
app.static('/example_static', './example_static', host='www.example.com')
|
|
||||||
|
|
||||||
Streaming Large File
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
In some cases, you might server large file(ex: videos, images, etc.) with Sanic. You can choose to use **streaming file** rather than download directly.
|
|
||||||
|
|
||||||
Here is an example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
app.static('/large_video.mp4', '/home/ubuntu/large_video.mp4', stream_large_files=True)
|
|
||||||
|
|
||||||
When `stream_large_files` is `True`, Sanic will use `file_stream()` instead of `file()` to serve static files. This will use **1KB** as the default chunk size. And, if needed, you can also use a custom chunk size. For example:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
chunk_size = 1024 * 1024 * 8 # Set chunk size to 8MiB
|
|
||||||
app.static('/large_video.mp4', '/home/ubuntu/large_video.mp4', stream_large_files=chunk_size)
|
|
|
@ -1,147 +0,0 @@
|
||||||
Streaming
|
|
||||||
=========
|
|
||||||
|
|
||||||
Request Streaming
|
|
||||||
-----------------
|
|
||||||
|
|
||||||
Sanic allows you to get request data by stream, as below. When the request ends, `await request.stream.read()` returns `None`. Only post, put and patch decorator have stream argument.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.views import CompositionView
|
|
||||||
from sanic.views import HTTPMethodView
|
|
||||||
from sanic.views import stream as stream_decorator
|
|
||||||
from sanic.blueprints import Blueprint
|
|
||||||
from sanic.response import stream, text
|
|
||||||
|
|
||||||
bp = Blueprint('blueprint_request_stream')
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class SimpleView(HTTPMethodView):
|
|
||||||
|
|
||||||
@stream_decorator
|
|
||||||
async def post(self, request):
|
|
||||||
result = ''
|
|
||||||
while True:
|
|
||||||
body = await request.stream.read()
|
|
||||||
if body is None:
|
|
||||||
break
|
|
||||||
result += body.decode('utf-8')
|
|
||||||
return text(result)
|
|
||||||
|
|
||||||
|
|
||||||
@app.post('/stream', stream=True)
|
|
||||||
async def handler(request):
|
|
||||||
async def streaming(response):
|
|
||||||
while True:
|
|
||||||
body = await request.stream.read()
|
|
||||||
if body is None:
|
|
||||||
break
|
|
||||||
body = body.decode('utf-8').replace('1', 'A')
|
|
||||||
await response.write(body)
|
|
||||||
return stream(streaming)
|
|
||||||
|
|
||||||
|
|
||||||
@bp.put('/bp_stream', stream=True)
|
|
||||||
async def bp_put_handler(request):
|
|
||||||
result = ''
|
|
||||||
while True:
|
|
||||||
body = await request.stream.read()
|
|
||||||
if body is None:
|
|
||||||
break
|
|
||||||
result += body.decode('utf-8').replace('1', 'A')
|
|
||||||
return text(result)
|
|
||||||
|
|
||||||
|
|
||||||
# You can also use `bp.add_route()` with stream argument
|
|
||||||
async def bp_post_handler(request):
|
|
||||||
result = ''
|
|
||||||
while True:
|
|
||||||
body = await request.stream.read()
|
|
||||||
if body is None:
|
|
||||||
break
|
|
||||||
result += body.decode('utf-8').replace('1', 'A')
|
|
||||||
return text(result)
|
|
||||||
|
|
||||||
bp.add_route(bp_post_handler, '/bp_stream', methods=['POST'], stream=True)
|
|
||||||
|
|
||||||
|
|
||||||
async def post_handler(request):
|
|
||||||
result = ''
|
|
||||||
while True:
|
|
||||||
body = await request.stream.read()
|
|
||||||
if body is None:
|
|
||||||
break
|
|
||||||
result += body.decode('utf-8')
|
|
||||||
return text(result)
|
|
||||||
|
|
||||||
app.blueprint(bp)
|
|
||||||
app.add_route(SimpleView.as_view(), '/method_view')
|
|
||||||
view = CompositionView()
|
|
||||||
view.add(['POST'], post_handler, stream=True)
|
|
||||||
app.add_route(view, '/composition_view')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run(host='127.0.0.1', port=8000)
|
|
||||||
|
|
||||||
Response Streaming
|
|
||||||
------------------
|
|
||||||
|
|
||||||
Sanic allows you to stream content to the client with the `stream` method. This method accepts a coroutine callback which is passed a `StreamingHTTPResponse` object that is written to. A simple example is like follows:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import stream
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
async def test(request):
|
|
||||||
async def sample_streaming_fn(response):
|
|
||||||
await response.write('foo,')
|
|
||||||
await response.write('bar')
|
|
||||||
|
|
||||||
return stream(sample_streaming_fn, content_type='text/csv')
|
|
||||||
|
|
||||||
This is useful in situations where you want to stream content to the client that originates in an external service, like a database. For example, you can stream database records to the client with the asynchronous cursor that `asyncpg` provides:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
async def index(request):
|
|
||||||
async def stream_from_db(response):
|
|
||||||
conn = await asyncpg.connect(database='test')
|
|
||||||
async with conn.transaction():
|
|
||||||
async for record in conn.cursor('SELECT generate_series(0, 10)'):
|
|
||||||
await response.write(record[0])
|
|
||||||
|
|
||||||
return stream(stream_from_db)
|
|
||||||
|
|
||||||
If a client supports HTTP/1.1, Sanic will use `chunked transfer encoding <https://en.wikipedia.org/wiki/Chunked_transfer_encoding>`_; you can explicitly enable or disable it using `chunked` option of the `stream` function.
|
|
||||||
|
|
||||||
File Streaming
|
|
||||||
--------------
|
|
||||||
|
|
||||||
Sanic provides `sanic.response.file_stream` function that is useful when you want to send a large file. It returns a `StreamingHTTPResponse` object and will use chunked transfer encoding by default; for this reason Sanic doesn't add `Content-Length` HTTP header in the response. If you want to use this header, you can disable chunked transfer encoding and add it manually:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from aiofiles import os as async_os
|
|
||||||
from sanic.response import file_stream
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
async def index(request):
|
|
||||||
file_path = "/srv/www/whatever.png"
|
|
||||||
|
|
||||||
file_stat = await async_os.stat(file_path)
|
|
||||||
headers = {"Content-Length": str(file_stat.st_size)}
|
|
||||||
|
|
||||||
return await file_stream(
|
|
||||||
file_path,
|
|
||||||
headers=headers,
|
|
||||||
chunked=False,
|
|
||||||
)
|
|
|
@ -1,171 +0,0 @@
|
||||||
Testing
|
|
||||||
=======
|
|
||||||
|
|
||||||
Sanic endpoints can be tested locally using the `test_client` object, which
|
|
||||||
depends on an additional package: `httpx <https://www.encode.io/httpx/>`_
|
|
||||||
library, which implements an API that mirrors the `requests` library.
|
|
||||||
|
|
||||||
The `test_client` exposes `get`, `post`, `put`, `delete`, `patch`, `head` and `options` methods
|
|
||||||
for you to run against your application. A simple example (using pytest) is like follows:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# Import the Sanic app, usually created with Sanic(__name__)
|
|
||||||
from external_server import app
|
|
||||||
|
|
||||||
def test_index_returns_200():
|
|
||||||
request, response = app.test_client.get('/')
|
|
||||||
assert response.status == 200
|
|
||||||
|
|
||||||
def test_index_put_not_allowed():
|
|
||||||
request, response = app.test_client.put('/')
|
|
||||||
assert response.status == 405
|
|
||||||
|
|
||||||
Internally, each time you call one of the `test_client` methods, the Sanic app is run at `127.0.0.1:42101` and
|
|
||||||
your test request is executed against your application, using `httpx`.
|
|
||||||
|
|
||||||
The `test_client` methods accept the following arguments and keyword arguments:
|
|
||||||
|
|
||||||
- `uri` *(default `'/'`)* A string representing the URI to test.
|
|
||||||
- `gather_request` *(default `True`)* A boolean which determines whether the
|
|
||||||
original request will be returned by the function. If set to `True`, the
|
|
||||||
return value is a tuple of `(request, response)`, if `False` only the
|
|
||||||
response is returned.
|
|
||||||
- `server_kwargs` *(default `{}`)* a dict of additional arguments to pass into `app.run` before the test request is run.
|
|
||||||
- `debug` *(default `False`)* A boolean which determines whether to run the server in debug mode.
|
|
||||||
|
|
||||||
The function further takes the `*request_args` and `**request_kwargs`, which are passed directly to the request.
|
|
||||||
|
|
||||||
For example, to supply data to a GET request, you would do the following:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
def test_get_request_includes_data():
|
|
||||||
params = {'key1': 'value1', 'key2': 'value2'}
|
|
||||||
request, response = app.test_client.get('/', params=params)
|
|
||||||
assert request.args.get('key1') == 'value1'
|
|
||||||
|
|
||||||
And to supply data to a JSON POST request:
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
def test_post_json_request_includes_data():
|
|
||||||
data = {'key1': 'value1', 'key2': 'value2'}
|
|
||||||
request, response = app.test_client.post('/', data=json.dumps(data))
|
|
||||||
assert request.json.get('key1') == 'value1'
|
|
||||||
|
|
||||||
More information about
|
|
||||||
the available arguments to `httpx` can be found
|
|
||||||
[in the documentation for `httpx <https://www.encode.io/httpx/>`_.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_index_returns_200():
|
|
||||||
request, response = await app.asgi_client.put('/')
|
|
||||||
assert response.status == 200
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
Whenever one of the test clients run, you can test your app instance to determine if it is in testing mode:
|
|
||||||
`app.test_mode`.
|
|
||||||
|
|
||||||
Additionally, Sanic has an asynchronous testing client. The difference is that the async client will not stand up an
|
|
||||||
instance of your application, but will instead reach inside it using ASGI. All listeners and middleware are still
|
|
||||||
executed.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_index_returns_200():
|
|
||||||
request, response = await app.asgi_client.put('/')
|
|
||||||
assert response.status == 200
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
Whenever one of the test clients run, you can test your app instance to determine if it is in testing mode:
|
|
||||||
`app.test_mode`.
|
|
||||||
|
|
||||||
|
|
||||||
Using a random port
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
If you need to test using a free unpriveleged port chosen by the kernel
|
|
||||||
instead of the default with `SanicTestClient`, you can do so by specifying
|
|
||||||
`port=None`. On most systems the port will be in the range 1024 to 65535.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
# Import the Sanic app, usually created with Sanic(__name__)
|
|
||||||
from external_server import app
|
|
||||||
from sanic.testing import SanicTestClient
|
|
||||||
|
|
||||||
def test_index_returns_200():
|
|
||||||
request, response = SanicTestClient(app, port=None).get('/')
|
|
||||||
assert response.status == 200
|
|
||||||
|
|
||||||
pytest-sanic
|
|
||||||
------------
|
|
||||||
|
|
||||||
`pytest-sanic <https://github.com/yunstanford/pytest-sanic>`_ is a pytest plugin, it helps you to test your code asynchronously.
|
|
||||||
Just write tests like,
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
async def test_sanic_db_find_by_id(app):
|
|
||||||
"""
|
|
||||||
Let's assume that, in db we have,
|
|
||||||
{
|
|
||||||
"id": "123",
|
|
||||||
"name": "Kobe Bryant",
|
|
||||||
"team": "Lakers",
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
doc = await app.db["players"].find_by_id("123")
|
|
||||||
assert doc.name == "Kobe Bryant"
|
|
||||||
assert doc.team == "Lakers"
|
|
||||||
|
|
||||||
`pytest-sanic <https://github.com/yunstanford/pytest-sanic>`_ also provides some useful fixtures, like loop, unused_port,
|
|
||||||
test_server, test_client.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
|
||||||
def app():
|
|
||||||
app = Sanic("test_sanic_app")
|
|
||||||
|
|
||||||
@app.route("/test_get", methods=['GET'])
|
|
||||||
async def test_get(request):
|
|
||||||
return response.json({"GET": True})
|
|
||||||
|
|
||||||
@app.route("/test_post", methods=['POST'])
|
|
||||||
async def test_post(request):
|
|
||||||
return response.json({"POST": True})
|
|
||||||
|
|
||||||
yield app
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def test_cli(loop, app, test_client):
|
|
||||||
return loop.run_until_complete(test_client(app, protocol=WebSocketProtocol))
|
|
||||||
|
|
||||||
|
|
||||||
#########
|
|
||||||
# Tests #
|
|
||||||
#########
|
|
||||||
|
|
||||||
async def test_fixture_test_client_get(test_cli):
|
|
||||||
"""
|
|
||||||
GET request
|
|
||||||
"""
|
|
||||||
resp = await test_cli.get('/test_get')
|
|
||||||
assert resp.status == 200
|
|
||||||
resp_json = await resp.json()
|
|
||||||
assert resp_json == {"GET": True}
|
|
||||||
|
|
||||||
async def test_fixture_test_client_post(test_cli):
|
|
||||||
"""
|
|
||||||
POST request
|
|
||||||
"""
|
|
||||||
resp = await test_cli.post('/test_post')
|
|
||||||
assert resp.status == 200
|
|
||||||
resp_json = await resp.json()
|
|
||||||
assert resp_json == {"POST": True}
|
|
|
@ -1,54 +0,0 @@
|
||||||
Versioning
|
|
||||||
==========
|
|
||||||
|
|
||||||
You can pass the `version` keyword to the route decorators, or to a blueprint initializer. It will result in the `v{version}` url prefix where `{version}` is the version number.
|
|
||||||
|
|
||||||
Per route
|
|
||||||
---------
|
|
||||||
|
|
||||||
You can pass a version number to the routes directly.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/text', version=1)
|
|
||||||
def handle_request(request):
|
|
||||||
return response.text('Hello world! Version 1')
|
|
||||||
|
|
||||||
@app.route('/text', version=2)
|
|
||||||
def handle_request(request):
|
|
||||||
return response.text('Hello world! Version 2')
|
|
||||||
|
|
||||||
app.run(port=80)
|
|
||||||
|
|
||||||
Then with curl:
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
curl localhost/v1/text
|
|
||||||
curl localhost/v2/text
|
|
||||||
|
|
||||||
|
|
||||||
Global blueprint version
|
|
||||||
------------------------
|
|
||||||
|
|
||||||
You can also pass a version number to the blueprint, which will apply to all routes.
|
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
from sanic import response
|
|
||||||
from sanic.blueprints import Blueprint
|
|
||||||
|
|
||||||
bp = Blueprint('test', version=1)
|
|
||||||
|
|
||||||
@bp.route('/html')
|
|
||||||
def handle_request(request):
|
|
||||||
return response.html('<p>Hello world!</p>')
|
|
||||||
|
|
||||||
Then with curl:
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
curl localhost/v1/html
|
|
|
@ -1,59 +0,0 @@
|
||||||
WebSocket
|
|
||||||
=========
|
|
||||||
|
|
||||||
Sanic provides an easy to use abstraction on top of `websockets`.
|
|
||||||
Sanic Supports websocket versions 7 and 8.
|
|
||||||
|
|
||||||
To setup a WebSocket:
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import json
|
|
||||||
from sanic.websocket import WebSocketProtocol
|
|
||||||
|
|
||||||
app = Sanic("websocket_example")
|
|
||||||
|
|
||||||
@app.websocket('/feed')
|
|
||||||
async def feed(request, ws):
|
|
||||||
while True:
|
|
||||||
data = 'hello!'
|
|
||||||
print('Sending: ' + data)
|
|
||||||
await ws.send(data)
|
|
||||||
data = await ws.recv()
|
|
||||||
print('Received: ' + data)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run(host="0.0.0.0", port=8000, protocol=WebSocketProtocol)
|
|
||||||
|
|
||||||
|
|
||||||
Alternatively, the ``app.add_websocket_route`` method can be used instead of the
|
|
||||||
decorator:
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
async def feed(request, ws):
|
|
||||||
pass
|
|
||||||
|
|
||||||
app.add_websocket_route(feed, '/feed')
|
|
||||||
|
|
||||||
|
|
||||||
Handlers for a WebSocket route is invoked with the request as first argument, and a
|
|
||||||
WebSocket protocol object as second argument. The protocol object has ``send``
|
|
||||||
and ``recv`` methods to send and receive data respectively.
|
|
||||||
|
|
||||||
|
|
||||||
You could setup your own WebSocket configuration through ``app.config``, like
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
app.config.WEBSOCKET_MAX_SIZE = 2 ** 20
|
|
||||||
app.config.WEBSOCKET_MAX_QUEUE = 32
|
|
||||||
app.config.WEBSOCKET_READ_LIMIT = 2 ** 16
|
|
||||||
app.config.WEBSOCKET_WRITE_LIMIT = 2 ** 16
|
|
||||||
app.config.WEBSOCKET_PING_INTERVAL = 20
|
|
||||||
app.config.WEBSOCKET_PING_TIMEOUT = 20
|
|
||||||
|
|
||||||
These settings will have no impact if running in ASGI mode.
|
|
||||||
|
|
||||||
Find more in ``Configuration`` section.
|
|
|
@ -1,11 +1,8 @@
|
||||||
FROM python:3.5
|
FROM sanicframework/sanic:LTS
|
||||||
MAINTAINER Channel Cat <channelcat@gmail.com>
|
|
||||||
|
|
||||||
ADD . /code
|
RUN mkdir /srv
|
||||||
RUN pip3 install git+https://github.com/channelcat/sanic
|
COPY . /srv
|
||||||
|
|
||||||
EXPOSE 8000
|
WORKDIR /srv
|
||||||
|
|
||||||
WORKDIR /code
|
CMD ["sanic", "simple_server.app"]
|
||||||
|
|
||||||
CMD ["python", "simple_server.py"]
|
|
||||||
|
|
|
@ -4,12 +4,14 @@ import asyncio
|
||||||
|
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
|
|
||||||
app = Sanic()
|
|
||||||
|
app = Sanic("Example")
|
||||||
|
|
||||||
|
|
||||||
async def notify_server_started_after_five_seconds():
|
async def notify_server_started_after_five_seconds():
|
||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
print('Server successfully started!')
|
print("Server successfully started!")
|
||||||
|
|
||||||
|
|
||||||
app.add_task(notify_server_started_after_five_seconds())
|
app.add_task(notify_server_started_after_five_seconds())
|
||||||
|
|
||||||
|
|
|
@ -1,30 +1,29 @@
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import text
|
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
app = Sanic()
|
from sanic import Sanic
|
||||||
|
from sanic.response import text
|
||||||
|
|
||||||
|
|
||||||
@app.middleware('request')
|
app = Sanic("Example")
|
||||||
|
|
||||||
|
|
||||||
|
@app.middleware("request")
|
||||||
def append_request(request):
|
def append_request(request):
|
||||||
# Add new key with random value
|
request.ctx.num = randint(0, 100)
|
||||||
request['num'] = randint(0, 100)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get('/pop')
|
@app.get("/pop")
|
||||||
def pop_handler(request):
|
def pop_handler(request):
|
||||||
# Pop key from request object
|
return text(request.ctx.num)
|
||||||
num = request.pop('num')
|
|
||||||
return text(num)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get('/key_exist')
|
@app.get("/key_exist")
|
||||||
def key_exist_handler(request):
|
def key_exist_handler(request):
|
||||||
# Check the key is exist or not
|
# Check the key is exist or not
|
||||||
if 'num' in request:
|
if hasattr(request.ctx, "num"):
|
||||||
return text('num exist in request')
|
return text("num exist in request")
|
||||||
|
|
||||||
return text('num does not exist in reqeust')
|
return text("num does not exist in request")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=8000, debug=True)
|
app.run(host="0.0.0.0", port=8000, debug=True)
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from sanic import Sanic
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
|
from sanic import Sanic
|
||||||
from sanic.response import json
|
from sanic.response import json
|
||||||
|
|
||||||
app = Sanic()
|
|
||||||
|
app = Sanic("Example")
|
||||||
|
|
||||||
|
|
||||||
def check_request_for_authorization_status(request):
|
def check_request_for_authorization_status(request):
|
||||||
|
@ -27,14 +29,16 @@ def authorized(f):
|
||||||
return response
|
return response
|
||||||
else:
|
else:
|
||||||
# the user is not authorized.
|
# the user is not authorized.
|
||||||
return json({'status': 'not_authorized'}, 403)
|
return json({"status": "not_authorized"}, 403)
|
||||||
|
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
@authorized
|
@authorized
|
||||||
async def test(request):
|
async def test(request):
|
||||||
return json({'status': 'authorized'})
|
return json({"status": "authorized"})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=8000)
|
app.run(host="0.0.0.0", port=8000)
|
||||||
|
|
|
@ -1,43 +1,54 @@
|
||||||
from sanic import Sanic, Blueprint
|
from sanic import Blueprint, Sanic
|
||||||
from sanic.response import text
|
from sanic.response import text
|
||||||
'''
|
|
||||||
|
|
||||||
|
"""
|
||||||
Demonstrates that blueprint request middleware are executed in the order they
|
Demonstrates that blueprint request middleware are executed in the order they
|
||||||
are added. And blueprint response middleware are executed in _reverse_ order.
|
are added. And blueprint response middleware are executed in _reverse_ order.
|
||||||
On a valid request, it should print "1 2 3 6 5 4" to terminal
|
On a valid request, it should print "1 2 3 6 5 4" to terminal
|
||||||
'''
|
"""
|
||||||
|
|
||||||
app = Sanic(__name__)
|
app = Sanic("Example")
|
||||||
|
|
||||||
bp = Blueprint("bp_"+__name__)
|
bp = Blueprint("bp_example")
|
||||||
|
|
||||||
@bp.middleware('request')
|
|
||||||
|
@bp.on_request
|
||||||
def request_middleware_1(request):
|
def request_middleware_1(request):
|
||||||
print('1')
|
print("1")
|
||||||
|
|
||||||
@bp.middleware('request')
|
|
||||||
|
@bp.on_request
|
||||||
def request_middleware_2(request):
|
def request_middleware_2(request):
|
||||||
print('2')
|
print("2")
|
||||||
|
|
||||||
@bp.middleware('request')
|
|
||||||
|
@bp.on_request
|
||||||
def request_middleware_3(request):
|
def request_middleware_3(request):
|
||||||
print('3')
|
print("3")
|
||||||
|
|
||||||
@bp.middleware('response')
|
|
||||||
|
@bp.on_response
|
||||||
def resp_middleware_4(request, response):
|
def resp_middleware_4(request, response):
|
||||||
print('4')
|
print("4")
|
||||||
|
|
||||||
@bp.middleware('response')
|
|
||||||
|
@bp.on_response
|
||||||
def resp_middleware_5(request, response):
|
def resp_middleware_5(request, response):
|
||||||
print('5')
|
print("5")
|
||||||
|
|
||||||
@bp.middleware('response')
|
|
||||||
|
@bp.on_response
|
||||||
def resp_middleware_6(request, response):
|
def resp_middleware_6(request, response):
|
||||||
print('6')
|
print("6")
|
||||||
|
|
||||||
@bp.route('/')
|
|
||||||
|
@bp.route("/")
|
||||||
def pop_handler(request):
|
def pop_handler(request):
|
||||||
return text('hello world')
|
return text("hello world")
|
||||||
|
|
||||||
app.blueprint(bp, url_prefix='/bp')
|
|
||||||
|
|
||||||
app.run(host="0.0.0.0", port=8000, debug=True, auto_reload=False)
|
app.blueprint(bp, url_prefix="/bp")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(host="0.0.0.0", port=8000, debug=True, auto_reload=False)
|
||||||
|
|
|
@ -1,38 +1,41 @@
|
||||||
from sanic import Blueprint, Sanic
|
from sanic import Blueprint, Sanic
|
||||||
from sanic.response import file, json
|
from sanic.response import file, json
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
blueprint = Blueprint('name', url_prefix='/my_blueprint')
|
app = Sanic("Example")
|
||||||
blueprint2 = Blueprint('name2', url_prefix='/my_blueprint2')
|
blueprint = Blueprint("bp_example", url_prefix="/my_blueprint")
|
||||||
blueprint3 = Blueprint('name3', url_prefix='/my_blueprint3')
|
blueprint2 = Blueprint("bp_example2", url_prefix="/my_blueprint2")
|
||||||
|
blueprint3 = Blueprint("bp_example3", url_prefix="/my_blueprint3")
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/foo')
|
@blueprint.route("/foo")
|
||||||
async def foo(request):
|
async def foo(request):
|
||||||
return json({'msg': 'hi from blueprint'})
|
return json({"msg": "hi from blueprint"})
|
||||||
|
|
||||||
|
|
||||||
@blueprint2.route('/foo')
|
@blueprint2.route("/foo")
|
||||||
async def foo2(request):
|
async def foo2(request):
|
||||||
return json({'msg': 'hi from blueprint2'})
|
return json({"msg": "hi from blueprint2"})
|
||||||
|
|
||||||
|
|
||||||
@blueprint3.route('/foo')
|
@blueprint3.route("/foo")
|
||||||
async def index(request):
|
async def index(request):
|
||||||
return await file('websocket.html')
|
return await file("websocket.html")
|
||||||
|
|
||||||
|
|
||||||
@app.websocket('/feed')
|
@app.websocket("/feed")
|
||||||
async def foo3(request, ws):
|
async def foo3(request, ws):
|
||||||
while True:
|
while True:
|
||||||
data = 'hello!'
|
data = "hello!"
|
||||||
print('Sending: ' + data)
|
print("Sending: " + data)
|
||||||
await ws.send(data)
|
await ws.send(data)
|
||||||
data = await ws.recv()
|
data = await ws.recv()
|
||||||
print('Received: ' + data)
|
print("Received: " + data)
|
||||||
|
|
||||||
|
|
||||||
app.blueprint(blueprint)
|
app.blueprint(blueprint)
|
||||||
app.blueprint(blueprint2)
|
app.blueprint(blueprint2)
|
||||||
app.blueprint(blueprint3)
|
app.blueprint(blueprint3)
|
||||||
|
|
||||||
app.run(host="0.0.0.0", port=8000, debug=True)
|
if __name__ == "__main__":
|
||||||
|
app.run(host="0.0.0.0", port=9999, debug=True)
|
||||||
|
|
|
@ -2,17 +2,21 @@ from asyncio import sleep
|
||||||
|
|
||||||
from sanic import Sanic, response
|
from sanic import Sanic, response
|
||||||
|
|
||||||
app = Sanic(__name__, strict_slashes=True)
|
|
||||||
|
app = Sanic("DelayedResponseApp", strict_slashes=True)
|
||||||
|
app.config.AUTO_EXTEND = False
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def handler(request):
|
async def handler(request):
|
||||||
return response.redirect("/sleep/3")
|
return response.redirect("/sleep/3")
|
||||||
|
|
||||||
@app.get("/sleep/<t:number>")
|
|
||||||
|
@app.get("/sleep/<t:float>")
|
||||||
async def handler2(request, t=0.3):
|
async def handler2(request, t=0.3):
|
||||||
await sleep(t)
|
await sleep(t)
|
||||||
return response.text(f"Slept {t:.1f} seconds.\n")
|
return response.text(f"Slept {t:.1f} seconds.\n")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=8000)
|
app.run(host="0.0.0.0", port=8000)
|
||||||
|
|
|
@ -7,8 +7,10 @@ and pass in an instance of it when we create our Sanic instance. Inside this
|
||||||
class' default handler, we can do anything including sending exceptions to
|
class' default handler, we can do anything including sending exceptions to
|
||||||
an external service.
|
an external service.
|
||||||
"""
|
"""
|
||||||
from sanic.handlers import ErrorHandler
|
|
||||||
from sanic.exceptions import SanicException
|
from sanic.exceptions import SanicException
|
||||||
|
from sanic.handlers import ErrorHandler
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Imports and code relevant for our CustomHandler class
|
Imports and code relevant for our CustomHandler class
|
||||||
(Ordinarily this would be in a separate file)
|
(Ordinarily this would be in a separate file)
|
||||||
|
@ -16,7 +18,6 @@ Imports and code relevant for our CustomHandler class
|
||||||
|
|
||||||
|
|
||||||
class CustomHandler(ErrorHandler):
|
class CustomHandler(ErrorHandler):
|
||||||
|
|
||||||
def default(self, request, exception):
|
def default(self, request, exception):
|
||||||
# Here, we have access to the exception object
|
# Here, we have access to the exception object
|
||||||
# and can do anything with it (log, send to external service, etc)
|
# and can do anything with it (log, send to external service, etc)
|
||||||
|
@ -38,17 +39,17 @@ server's error_handler to an instance of our CustomHandler
|
||||||
|
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
handler = CustomHandler()
|
handler = CustomHandler()
|
||||||
app.error_handler = handler
|
app = Sanic("Example", error_handler=handler)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
async def test(request):
|
async def test(request):
|
||||||
# Here, something occurs which causes an unexpected exception
|
# Here, something occurs which causes an unexpected exception
|
||||||
# This exception will flow to our custom handler.
|
# This exception will flow to our custom handler.
|
||||||
raise SanicException('You Broke It!')
|
raise SanicException("You Broke It!")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=8000, debug=True)
|
app.run(host="0.0.0.0", port=8000, debug=True)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from sanic import Sanic
|
from sanic import Sanic, response
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
app = Sanic("Example")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
|
@ -9,5 +9,5 @@ async def test(request):
|
||||||
return response.json({"test": True})
|
return response.json({"test": True})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=8000)
|
app.run(host="0.0.0.0", port=8000)
|
73
examples/http_redirect.py
Normal file
73
examples/http_redirect.py
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
from sanic import Sanic, response, text
|
||||||
|
from sanic.handlers import ErrorHandler
|
||||||
|
from sanic.server.async_server import AsyncioServer
|
||||||
|
|
||||||
|
|
||||||
|
HTTP_PORT = 9999
|
||||||
|
HTTPS_PORT = 8888
|
||||||
|
|
||||||
|
http = Sanic("http")
|
||||||
|
http.config.SERVER_NAME = f"localhost:{HTTP_PORT}"
|
||||||
|
https = Sanic("https")
|
||||||
|
https.config.SERVER_NAME = f"localhost:{HTTPS_PORT}"
|
||||||
|
|
||||||
|
|
||||||
|
@https.get("/foo")
|
||||||
|
def foo(request):
|
||||||
|
return text("foo")
|
||||||
|
|
||||||
|
|
||||||
|
@https.get("/bar")
|
||||||
|
def bar(request):
|
||||||
|
return text("bar")
|
||||||
|
|
||||||
|
|
||||||
|
@http.get("/<path:path>")
|
||||||
|
def proxy(request, path):
|
||||||
|
url = request.app.url_for(
|
||||||
|
"proxy",
|
||||||
|
path=path,
|
||||||
|
_server=https.config.SERVER_NAME,
|
||||||
|
_external=True,
|
||||||
|
_scheme="https",
|
||||||
|
)
|
||||||
|
return response.redirect(url)
|
||||||
|
|
||||||
|
|
||||||
|
@https.main_process_start
|
||||||
|
async def start(app, _):
|
||||||
|
http_server = await http.create_server(
|
||||||
|
port=HTTP_PORT, return_asyncio_server=True
|
||||||
|
)
|
||||||
|
app.add_task(runner(http, http_server))
|
||||||
|
app.ctx.http_server = http_server
|
||||||
|
app.ctx.http = http
|
||||||
|
|
||||||
|
|
||||||
|
@https.main_process_stop
|
||||||
|
async def stop(app, _):
|
||||||
|
await app.ctx.http_server.before_stop()
|
||||||
|
await app.ctx.http_server.close()
|
||||||
|
for connection in app.ctx.http_server.connections:
|
||||||
|
connection.close_if_idle()
|
||||||
|
await app.ctx.http_server.after_stop()
|
||||||
|
app.ctx.http = False
|
||||||
|
|
||||||
|
|
||||||
|
async def runner(app: Sanic, app_server: AsyncioServer):
|
||||||
|
app.is_running = True
|
||||||
|
try:
|
||||||
|
app.signalize()
|
||||||
|
app.finalize()
|
||||||
|
ErrorHandler.finalize(app.error_handler)
|
||||||
|
app_server.init = True
|
||||||
|
|
||||||
|
await app_server.before_start()
|
||||||
|
await app_server.after_start()
|
||||||
|
await app_server.serve_forever()
|
||||||
|
finally:
|
||||||
|
app.is_running = False
|
||||||
|
app.is_stopping = True
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
https.run(port=HTTPS_PORT, debug=True)
|
|
@ -1,26 +1,30 @@
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.response import json
|
from sanic.response import json
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import aiohttp
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
app = Sanic("Example")
|
||||||
|
|
||||||
sem = None
|
sem = None
|
||||||
|
|
||||||
|
|
||||||
@app.listener('before_server_start')
|
@app.before_server_start
|
||||||
def init(sanic, loop):
|
def init(sanic, _):
|
||||||
global sem
|
global sem
|
||||||
concurrency_per_worker = 4
|
concurrency_per_worker = 4
|
||||||
sem = asyncio.Semaphore(concurrency_per_worker, loop=loop)
|
sem = asyncio.Semaphore(concurrency_per_worker)
|
||||||
|
|
||||||
|
|
||||||
async def bounded_fetch(session, url):
|
async def bounded_fetch(session, url):
|
||||||
"""
|
"""
|
||||||
Use session object to perform 'get' request on url
|
Use session object to perform 'get' request on url
|
||||||
"""
|
"""
|
||||||
async with sem, session.get(url) as response:
|
async with sem:
|
||||||
return await response.json()
|
response = await session.get(url)
|
||||||
|
return response.json()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
|
@ -28,11 +32,12 @@ async def test(request):
|
||||||
"""
|
"""
|
||||||
Download and serve example JSON
|
Download and serve example JSON
|
||||||
"""
|
"""
|
||||||
url = "https://api.github.com/repos/channelcat/sanic"
|
url = "https://api.github.com/repos/sanic-org/sanic"
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with httpx.AsyncClient() as session:
|
||||||
response = await bounded_fetch(session, url)
|
response = await bounded_fetch(session, url)
|
||||||
return json(response)
|
return json(response)
|
||||||
|
|
||||||
|
|
||||||
app.run(host="0.0.0.0", port=8000, workers=2)
|
if __name__ == "__main__":
|
||||||
|
app.run(host="0.0.0.0", port=8000, workers=2)
|
||||||
|
|
|
@ -1,86 +1,74 @@
|
||||||
'''
|
|
||||||
Based on example from https://github.com/Skyscanner/aiotask-context
|
|
||||||
and `examples/{override_logging,run_async}.py`.
|
|
||||||
|
|
||||||
Needs https://github.com/Skyscanner/aiotask-context/tree/52efbc21e2e1def2d52abb9a8e951f3ce5e6f690 or newer
|
|
||||||
|
|
||||||
$ pip install git+https://github.com/Skyscanner/aiotask-context.git
|
|
||||||
'''
|
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import uuid
|
|
||||||
import logging
|
import logging
|
||||||
from signal import signal, SIGINT
|
|
||||||
|
|
||||||
from sanic import Sanic
|
from contextvars import ContextVar
|
||||||
from sanic import response
|
|
||||||
|
from sanic import Sanic, response
|
||||||
|
|
||||||
import uvloop
|
|
||||||
import aiotask_context as context
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class RequestIdFilter(logging.Filter):
|
class RequestIdFilter(logging.Filter):
|
||||||
def filter(self, record):
|
def filter(self, record):
|
||||||
record.request_id = context.get('X-Request-ID')
|
try:
|
||||||
|
record.request_id = app.ctx.request_id.get(None) or "n/a"
|
||||||
|
except AttributeError:
|
||||||
|
record.request_id = "n/a"
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
LOG_SETTINGS = {
|
LOG_SETTINGS = {
|
||||||
'version': 1,
|
"version": 1,
|
||||||
'disable_existing_loggers': False,
|
"disable_existing_loggers": False,
|
||||||
'handlers': {
|
"handlers": {
|
||||||
'console': {
|
"console": {
|
||||||
'class': 'logging.StreamHandler',
|
"class": "logging.StreamHandler",
|
||||||
'level': 'DEBUG',
|
"level": "DEBUG",
|
||||||
'formatter': 'default',
|
"formatter": "default",
|
||||||
'filters': ['requestid'],
|
"filters": ["requestid"],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'filters': {
|
"filters": {
|
||||||
'requestid': {
|
"requestid": {
|
||||||
'()': RequestIdFilter,
|
"()": RequestIdFilter,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'formatters': {
|
"formatters": {
|
||||||
'default': {
|
"default": {
|
||||||
'format': '%(asctime)s %(levelname)s %(name)s:%(lineno)d %(request_id)s | %(message)s',
|
"format": "%(asctime)s %(levelname)s %(name)s:%(lineno)d %(request_id)s | %(message)s",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'loggers': {
|
"loggers": {
|
||||||
'': {
|
"": {"level": "DEBUG", "handlers": ["console"], "propagate": True},
|
||||||
'level': 'DEBUG',
|
|
||||||
'handlers': ['console'],
|
|
||||||
'propagate': True
|
|
||||||
},
|
},
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
app = Sanic(__name__, log_config=LOG_SETTINGS)
|
app = Sanic("Example", log_config=LOG_SETTINGS)
|
||||||
|
|
||||||
|
|
||||||
@app.middleware('request')
|
@app.on_request
|
||||||
async def set_request_id(request):
|
async def set_request_id(request):
|
||||||
request_id = request.headers.get('X-Request-ID') or str(uuid.uuid4())
|
request.app.ctx.request_id.set(request.id)
|
||||||
context.set("X-Request-ID", request_id)
|
log.info(f"Setting {request.id=}")
|
||||||
|
|
||||||
|
|
||||||
|
@app.on_response
|
||||||
|
async def set_request_header(request, response):
|
||||||
|
response.headers["X-Request-ID"] = request.id
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
async def test(request):
|
async def test(request):
|
||||||
log.debug('X-Request-ID: %s', context.get('X-Request-ID'))
|
log.debug("X-Request-ID: %s", request.id)
|
||||||
log.info('Hello from test!')
|
log.info("Hello from test!")
|
||||||
return response.json({"test": True})
|
return response.json({"test": True})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
@app.before_server_start
|
||||||
asyncio.set_event_loop(uvloop.new_event_loop())
|
def setup(app, loop):
|
||||||
server = app.create_server(host="0.0.0.0", port=8000, return_asyncio_server=True)
|
app.ctx.request_id = ContextVar("request_id")
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
loop.set_task_factory(context.task_factory)
|
|
||||||
task = asyncio.ensure_future(server)
|
if __name__ == "__main__":
|
||||||
try:
|
app.run(port=9999, debug=True)
|
||||||
loop.run_forever()
|
|
||||||
except:
|
|
||||||
loop.stop()
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
from os import getenv
|
from os import getenv
|
||||||
from platform import node
|
from platform import node
|
||||||
from uuid import getnode as get_mac
|
from uuid import getnode as get_mac
|
||||||
|
@ -7,10 +8,11 @@ from uuid import getnode as get_mac
|
||||||
from logdna import LogDNAHandler
|
from logdna import LogDNAHandler
|
||||||
|
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.response import json
|
|
||||||
from sanic.request import Request
|
from sanic.request import Request
|
||||||
|
from sanic.response import json
|
||||||
|
|
||||||
log = logging.getLogger('logdna')
|
|
||||||
|
log = logging.getLogger("logdna")
|
||||||
log.setLevel(logging.INFO)
|
log.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,16 +32,18 @@ logdna_options = {
|
||||||
"index_meta": True,
|
"index_meta": True,
|
||||||
"hostname": node(),
|
"hostname": node(),
|
||||||
"ip": get_my_ip_address(),
|
"ip": get_my_ip_address(),
|
||||||
"mac": get_mac_address()
|
"mac": get_mac_address(),
|
||||||
}
|
}
|
||||||
|
|
||||||
logdna_handler = LogDNAHandler(getenv("LOGDNA_API_KEY"), options=logdna_options)
|
logdna_handler = LogDNAHandler(
|
||||||
|
getenv("LOGDNA_API_KEY"), options=logdna_options
|
||||||
|
)
|
||||||
|
|
||||||
logdna = logging.getLogger(__name__)
|
logdna = logging.getLogger(__name__)
|
||||||
logdna.setLevel(logging.INFO)
|
logdna.setLevel(logging.INFO)
|
||||||
logdna.addHandler(logdna_handler)
|
logdna.addHandler(logdna_handler)
|
||||||
|
|
||||||
app = Sanic(__name__)
|
app = Sanic("Example")
|
||||||
|
|
||||||
|
|
||||||
@app.middleware
|
@app.middleware
|
||||||
|
@ -49,13 +53,8 @@ def log_request(request: Request):
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def default(request):
|
def default(request):
|
||||||
return json({
|
return json({"response": "I was here"})
|
||||||
"response": "I was here"
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(
|
app.run(host="0.0.0.0", port=getenv("PORT", 8080))
|
||||||
host="0.0.0.0",
|
|
||||||
port=getenv("PORT", 8080)
|
|
||||||
)
|
|
||||||
|
|
|
@ -2,27 +2,29 @@
|
||||||
Modify header or status in response
|
Modify header or status in response
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from sanic import Sanic
|
from sanic import Sanic, response
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
app = Sanic("Example")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
def handle_request(request):
|
def handle_request(request):
|
||||||
return response.json(
|
return response.json(
|
||||||
{'message': 'Hello world!'},
|
{"message": "Hello world!"},
|
||||||
headers={'X-Served-By': 'sanic'},
|
headers={"X-Served-By": "sanic"},
|
||||||
status=200
|
status=200,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/unauthorized')
|
@app.route("/unauthorized")
|
||||||
def handle_request(request):
|
def handle_request(request):
|
||||||
return response.json(
|
return response.json(
|
||||||
{'message': 'You are not authorized'},
|
{"message": "You are not authorized"},
|
||||||
headers={'X-Served-By': 'sanic'},
|
headers={"X-Served-By": "sanic"},
|
||||||
status=404
|
status=404,
|
||||||
)
|
)
|
||||||
|
|
||||||
app.run(host="0.0.0.0", port=8000, debug=True)
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(host="0.0.0.0", port=8000, debug=True)
|
||||||
|
|
|
@ -1,24 +1,24 @@
|
||||||
from sanic import Sanic
|
|
||||||
from sanic import response
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from sanic import Sanic, text
|
||||||
|
|
||||||
|
|
||||||
logging_format = "[%(asctime)s] %(process)d-%(levelname)s "
|
logging_format = "[%(asctime)s] %(process)d-%(levelname)s "
|
||||||
logging_format += "%(module)s::%(funcName)s():l%(lineno)d: "
|
logging_format += "%(module)s::%(funcName)s():l%(lineno)d: "
|
||||||
logging_format += "%(message)s"
|
logging_format += "%(message)s"
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(format=logging_format, level=logging.DEBUG)
|
||||||
format=logging_format,
|
|
||||||
level=logging.DEBUG
|
|
||||||
)
|
|
||||||
log = logging.getLogger()
|
log = logging.getLogger()
|
||||||
|
|
||||||
# Set logger to override default basicConfig
|
# Set logger to override default basicConfig
|
||||||
sanic = Sanic()
|
app = Sanic("app")
|
||||||
|
|
||||||
|
|
||||||
@sanic.route("/")
|
@app.route("/")
|
||||||
def test(request):
|
def test(request):
|
||||||
log.info("received request; responding with 'hey'")
|
log.info("received request; responding with 'hey'")
|
||||||
return response.text("hey")
|
return text("hey")
|
||||||
|
|
||||||
sanic.run(host="0.0.0.0", port=8000)
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(host="0.0.0.0", port=8000)
|
||||||
|
|
|
@ -9,15 +9,19 @@ Run with xdist params:
|
||||||
$ pytest examples/pytest_xdist.py -n 8 # 8 workers
|
$ pytest examples/pytest_xdist.py -n 8 # 8 workers
|
||||||
"""
|
"""
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from sanic_testing import SanicTestClient
|
||||||
|
from sanic_testing.testing import PORT as PORT_BASE
|
||||||
|
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.response import text
|
from sanic.response import text
|
||||||
from sanic.testing import PORT as PORT_BASE, SanicTestClient
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def test_port(worker_id):
|
def test_port(worker_id):
|
||||||
m = re.search(r'[0-9]+', worker_id)
|
m = re.search(r"[0-9]+", worker_id)
|
||||||
if m:
|
if m:
|
||||||
num_id = m.group(0)
|
num_id = m.group(0)
|
||||||
else:
|
else:
|
||||||
|
@ -28,11 +32,11 @@ def test_port(worker_id):
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def app():
|
def app():
|
||||||
app = Sanic()
|
app = Sanic("Example")
|
||||||
|
|
||||||
@app.route('/')
|
@app.route("/")
|
||||||
async def index(request):
|
async def index(request):
|
||||||
return text('OK')
|
return text("OK")
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
@ -42,8 +46,8 @@ def client(app, test_port):
|
||||||
return SanicTestClient(app, test_port)
|
return SanicTestClient(app, test_port)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('run_id', range(100))
|
@pytest.mark.parametrize("run_id", range(100))
|
||||||
def test_index(client, run_id):
|
def test_index(client, run_id):
|
||||||
request, response = client._sanic_endpoint_test('get', '/')
|
request, response = client._sanic_endpoint_test("get", "/")
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
assert response.text == 'OK'
|
assert response.text == "OK"
|
||||||
|
|
|
@ -8,7 +8,6 @@ from sanic.handlers import ErrorHandler
|
||||||
|
|
||||||
|
|
||||||
class RaygunExceptionReporter(ErrorHandler):
|
class RaygunExceptionReporter(ErrorHandler):
|
||||||
|
|
||||||
def __init__(self, raygun_api_key=None):
|
def __init__(self, raygun_api_key=None):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
if raygun_api_key is None:
|
if raygun_api_key is None:
|
||||||
|
@ -22,16 +21,13 @@ class RaygunExceptionReporter(ErrorHandler):
|
||||||
|
|
||||||
|
|
||||||
raygun_error_reporter = RaygunExceptionReporter()
|
raygun_error_reporter = RaygunExceptionReporter()
|
||||||
app = Sanic(__name__, error_handler=raygun_error_reporter)
|
app = Sanic("Example", error_handler=raygun_error_reporter)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/raise")
|
@app.route("/raise")
|
||||||
async def test(request):
|
async def test(request):
|
||||||
raise SanicException('You Broke It!')
|
raise SanicException("You Broke It!")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
app.run(
|
app.run(host="0.0.0.0", port=getenv("PORT", 8080))
|
||||||
host="0.0.0.0",
|
|
||||||
port=getenv("PORT", 8080)
|
|
||||||
)
|
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
from sanic import Sanic
|
from sanic import Sanic, response
|
||||||
from sanic import response
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
app = Sanic("Example")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
def handle_request(request):
|
def handle_request(request):
|
||||||
return response.redirect('/redirect')
|
return response.redirect("/redirect")
|
||||||
|
|
||||||
|
|
||||||
@app.route('/redirect')
|
@app.route("/redirect")
|
||||||
async def test(request):
|
async def test(request):
|
||||||
return response.json({"Redirected": True})
|
return response.json({"Redirected": True})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", port=8000)
|
app.run(host="0.0.0.0", port=8000)
|
|
@ -6,5 +6,5 @@ data = ""
|
||||||
for i in range(1, 250000):
|
for i in range(1, 250000):
|
||||||
data += str(i)
|
data += str(i)
|
||||||
|
|
||||||
r = requests.post('http://0.0.0.0:8000/stream', data=data)
|
r = requests.post("http://0.0.0.0:8000/stream", data=data)
|
||||||
print(r.text)
|
print(r.text)
|
||||||
|
|
|
@ -1,65 +1,63 @@
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.views import CompositionView
|
|
||||||
from sanic.views import HTTPMethodView
|
|
||||||
from sanic.views import stream as stream_decorator
|
|
||||||
from sanic.blueprints import Blueprint
|
from sanic.blueprints import Blueprint
|
||||||
from sanic.response import stream, text
|
from sanic.response import stream, text
|
||||||
|
from sanic.views import HTTPMethodView
|
||||||
|
from sanic.views import stream as stream_decorator
|
||||||
|
|
||||||
bp = Blueprint('blueprint_request_stream')
|
|
||||||
app = Sanic('request_stream')
|
bp = Blueprint("bp_example")
|
||||||
|
app = Sanic("Example")
|
||||||
|
|
||||||
|
|
||||||
class SimpleView(HTTPMethodView):
|
class SimpleView(HTTPMethodView):
|
||||||
|
|
||||||
@stream_decorator
|
@stream_decorator
|
||||||
async def post(self, request):
|
async def post(self, request):
|
||||||
result = ''
|
result = ""
|
||||||
while True:
|
while True:
|
||||||
body = await request.stream.get()
|
body = await request.stream.get()
|
||||||
if body is None:
|
if body is None:
|
||||||
break
|
break
|
||||||
result += body.decode('utf-8')
|
result += body.decode("utf-8")
|
||||||
return text(result)
|
return text(result)
|
||||||
|
|
||||||
|
|
||||||
@app.post('/stream', stream=True)
|
@app.post("/stream", stream=True)
|
||||||
async def handler(request):
|
async def handler(request):
|
||||||
async def streaming(response):
|
async def streaming(response):
|
||||||
while True:
|
while True:
|
||||||
body = await request.stream.get()
|
body = await request.stream.get()
|
||||||
if body is None:
|
if body is None:
|
||||||
break
|
break
|
||||||
body = body.decode('utf-8').replace('1', 'A')
|
body = body.decode("utf-8").replace("1", "A")
|
||||||
await response.write(body)
|
await response.write(body)
|
||||||
|
|
||||||
return stream(streaming)
|
return stream(streaming)
|
||||||
|
|
||||||
|
|
||||||
@bp.put('/bp_stream', stream=True)
|
@bp.put("/bp_stream", stream=True)
|
||||||
async def bp_handler(request):
|
async def bp_handler(request):
|
||||||
result = ''
|
result = ""
|
||||||
while True:
|
while True:
|
||||||
body = await request.stream.get()
|
body = await request.stream.get()
|
||||||
if body is None:
|
if body is None:
|
||||||
break
|
break
|
||||||
result += body.decode('utf-8').replace('1', 'A')
|
result += body.decode("utf-8").replace("1", "A")
|
||||||
return text(result)
|
return text(result)
|
||||||
|
|
||||||
|
|
||||||
async def post_handler(request):
|
async def post_handler(request):
|
||||||
result = ''
|
result = ""
|
||||||
while True:
|
while True:
|
||||||
body = await request.stream.get()
|
body = await request.stream.get()
|
||||||
if body is None:
|
if body is None:
|
||||||
break
|
break
|
||||||
result += body.decode('utf-8')
|
result += body.decode("utf-8")
|
||||||
return text(result)
|
return text(result)
|
||||||
|
|
||||||
|
|
||||||
app.blueprint(bp)
|
app.blueprint(bp)
|
||||||
app.add_route(SimpleView.as_view(), '/method_view')
|
app.add_route(SimpleView.as_view(), "/method_view")
|
||||||
view = CompositionView()
|
|
||||||
view.add(['POST'], post_handler, stream=True)
|
|
||||||
app.add_route(view, '/composition_view')
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
app.run(host='0.0.0.0', port=8000)
|
app.run(host="0.0.0.0", port=8000)
|
||||||
|
|
|
@ -1,21 +1,24 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
from sanic import Sanic
|
|
||||||
from sanic import response
|
from sanic import Sanic, response
|
||||||
from sanic.config import Config
|
from sanic.config import Config
|
||||||
from sanic.exceptions import RequestTimeout
|
from sanic.exceptions import RequestTimeout
|
||||||
|
|
||||||
|
|
||||||
Config.REQUEST_TIMEOUT = 1
|
Config.REQUEST_TIMEOUT = 1
|
||||||
app = Sanic(__name__)
|
app = Sanic("Example")
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route("/")
|
||||||
async def test(request):
|
async def test(request):
|
||||||
await asyncio.sleep(3)
|
await asyncio.sleep(3)
|
||||||
return response.text('Hello, world!')
|
return response.text("Hello, world!")
|
||||||
|
|
||||||
|
|
||||||
@app.exception(RequestTimeout)
|
@app.exception(RequestTimeout)
|
||||||
def timeout(request, exception):
|
def timeout(request, exception):
|
||||||
return response.text('RequestTimeout from error_handler.', 408)
|
return response.text("RequestTimeout from error_handler.", 408)
|
||||||
|
|
||||||
app.run(host='0.0.0.0', port=8000)
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(host="0.0.0.0", port=8000)
|
||||||
|
|
|
@ -1,21 +1,22 @@
|
||||||
|
from os import getenv
|
||||||
|
|
||||||
import rollbar
|
import rollbar
|
||||||
|
|
||||||
from sanic.handlers import ErrorHandler
|
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.exceptions import SanicException
|
from sanic.exceptions import SanicException
|
||||||
from os import getenv
|
from sanic.handlers import ErrorHandler
|
||||||
|
|
||||||
|
|
||||||
rollbar.init(getenv("ROLLBAR_API_KEY"))
|
rollbar.init(getenv("ROLLBAR_API_KEY"))
|
||||||
|
|
||||||
|
|
||||||
class RollbarExceptionHandler(ErrorHandler):
|
class RollbarExceptionHandler(ErrorHandler):
|
||||||
|
|
||||||
def default(self, request, exception):
|
def default(self, request, exception):
|
||||||
rollbar.report_message(str(exception))
|
rollbar.report_message(str(exception))
|
||||||
return super().default(request, exception)
|
return super().default(request, exception)
|
||||||
|
|
||||||
|
|
||||||
app = Sanic(__name__, error_handler=RollbarExceptionHandler())
|
app = Sanic("Example", error_handler=RollbarExceptionHandler())
|
||||||
|
|
||||||
|
|
||||||
@app.route("/raise")
|
@app.route("/raise")
|
||||||
|
@ -24,7 +25,4 @@ def create_error(request):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(
|
app.run(host="0.0.0.0", port=getenv("PORT", 8080))
|
||||||
host="0.0.0.0",
|
|
||||||
port=getenv("PORT", 8080)
|
|
||||||
)
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ from pathlib import Path
|
||||||
from sanic import Sanic, response
|
from sanic import Sanic, response
|
||||||
|
|
||||||
|
|
||||||
app = Sanic(__name__)
|
app = Sanic("Example")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/text")
|
@app.route("/text")
|
||||||
|
@ -48,42 +48,42 @@ async def handler_file_stream(request):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/stream", stream=True)
|
@app.post("/stream", stream=True)
|
||||||
async def handler_stream(request):
|
async def handler_stream(request):
|
||||||
while True:
|
while True:
|
||||||
body = await request.stream.read()
|
body = await request.stream.read()
|
||||||
if body is None:
|
if body is None:
|
||||||
break
|
break
|
||||||
body = body.decode("utf-8").replace("1", "A")
|
body = body.decode("utf-8").replace("1", "A")
|
||||||
# await response.write(body)
|
await response.write(body)
|
||||||
return response.stream(body)
|
return response.stream(body)
|
||||||
|
|
||||||
|
|
||||||
@app.listener("before_server_start")
|
@app.before_server_start
|
||||||
async def listener_before_server_start(*args, **kwargs):
|
async def listener_before_server_start(*args, **kwargs):
|
||||||
print("before_server_start")
|
print("before_server_start")
|
||||||
|
|
||||||
|
|
||||||
@app.listener("after_server_start")
|
@app.after_server_start
|
||||||
async def listener_after_server_start(*args, **kwargs):
|
async def listener_after_server_start(*args, **kwargs):
|
||||||
print("after_server_start")
|
print("after_server_start")
|
||||||
|
|
||||||
|
|
||||||
@app.listener("before_server_stop")
|
@app.before_server_stop
|
||||||
async def listener_before_server_stop(*args, **kwargs):
|
async def listener_before_server_stop(*args, **kwargs):
|
||||||
print("before_server_stop")
|
print("before_server_stop")
|
||||||
|
|
||||||
|
|
||||||
@app.listener("after_server_stop")
|
@app.after_server_stop
|
||||||
async def listener_after_server_stop(*args, **kwargs):
|
async def listener_after_server_stop(*args, **kwargs):
|
||||||
print("after_server_stop")
|
print("after_server_stop")
|
||||||
|
|
||||||
|
|
||||||
@app.middleware("request")
|
@app.on_request
|
||||||
async def print_on_request(request):
|
async def print_on_request(request):
|
||||||
print("print_on_request")
|
print("print_on_request")
|
||||||
|
|
||||||
|
|
||||||
@app.middleware("response")
|
@app.on_response
|
||||||
async def print_on_response(request, response):
|
async def print_on_response(request, response):
|
||||||
print("print_on_response")
|
print("print_on_response")
|
||||||
|
|
|
@ -1,22 +1,30 @@
|
||||||
from sanic import Sanic
|
|
||||||
from sanic import response
|
|
||||||
from signal import signal, SIGINT
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
import uvloop
|
import uvloop
|
||||||
|
|
||||||
app = Sanic(__name__)
|
from sanic import Sanic, response
|
||||||
|
|
||||||
|
|
||||||
|
app = Sanic("Example")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
async def test(request):
|
async def test(request):
|
||||||
return response.json({"answer": "42"})
|
return response.json({"answer": "42"})
|
||||||
|
|
||||||
asyncio.set_event_loop(uvloop.new_event_loop())
|
|
||||||
server = app.create_server(host="0.0.0.0", port=8000, return_asyncio_server=True)
|
async def main():
|
||||||
loop = asyncio.get_event_loop()
|
server = await app.create_server(
|
||||||
task = asyncio.ensure_future(server)
|
port=8000, host="0.0.0.0", return_asyncio_server=True
|
||||||
signal(SIGINT, lambda s, f: loop.stop())
|
)
|
||||||
try:
|
|
||||||
loop.run_forever()
|
if server is None:
|
||||||
except:
|
return
|
||||||
loop.stop()
|
|
||||||
|
await server.startup()
|
||||||
|
await server.serve_forever()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.set_event_loop(uvloop.new_event_loop())
|
||||||
|
asyncio.run(main())
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user