simple web server
This commit is contained in:
commit
4d85b32efd
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
local.env
|
||||
**/*.pyc
|
||||
test/
|
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Python Debugger: main.py",
|
||||
"type": "debugpy",
|
||||
"request": "launch",
|
||||
"program": "~/.local/bin/gunicorn",
|
||||
//"console": "integratedTerminal",
|
||||
"args": ["main:app", "--chdir=src", "--bind=127.0.0.1:8080", "--reload", "-w", "1", "--timeout=7200"],
|
||||
"envFile": "${workspaceFolder}/local.env"
|
||||
}
|
||||
]
|
||||
}
|
0
Dockerfile
Normal file
0
Dockerfile
Normal file
0
docker-compose.yaml
Normal file
0
docker-compose.yaml
Normal file
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
falcon==3.1.3
|
||||
gunicorn==22.0.0
|
||||
greenlet==3.0.3
|
91
src/main.py
Normal file
91
src/main.py
Normal file
@ -0,0 +1,91 @@
|
||||
import falcon
|
||||
import json
|
||||
|
||||
|
||||
class RequireJSON:
|
||||
def process_request(self, req, resp):
|
||||
if not req.client_accepts_json:
|
||||
raise falcon.HTTPNotAcceptable(
|
||||
description='This API only supports responses encoded as JSON.',
|
||||
href='http://docs.examples.com/api/json',
|
||||
)
|
||||
|
||||
if req.method in ('POST', 'PUT'):
|
||||
if req.content_type == None or 'application/json' not in req.content_type:
|
||||
raise falcon.HTTPUnsupportedMediaType(
|
||||
title='This API only supports requests encoded as JSON.',
|
||||
href='http://docs.examples.com/api/json',
|
||||
)
|
||||
|
||||
class JSONTranslator:
|
||||
# NOTE: Normally you would simply use req.media and resp.media for
|
||||
# this particular use case; this example serves only to illustrate
|
||||
# what is possible.
|
||||
|
||||
def process_request(self, req, resp):
|
||||
# req.stream corresponds to the WSGI wsgi.input environ variable,
|
||||
# and allows you to read bytes from the request body.
|
||||
#
|
||||
# See also: PEP 3333
|
||||
if req.content_length in (None, 0):
|
||||
# Nothing to do
|
||||
return
|
||||
|
||||
body = req.stream.read()
|
||||
if not body:
|
||||
raise falcon.HTTPBadRequest(
|
||||
title='Empty request body',
|
||||
description='A valid JSON document is required.',
|
||||
)
|
||||
|
||||
try:
|
||||
req.context.doc = json.loads(body.decode('utf-8'))
|
||||
|
||||
except (ValueError, UnicodeDecodeError):
|
||||
description = (
|
||||
'Could not decode the request body. The '
|
||||
'JSON was incorrect or not encoded as '
|
||||
'UTF-8.'
|
||||
)
|
||||
|
||||
raise falcon.HTTPBadRequest(title='Malformed JSON', description=description)
|
||||
|
||||
def process_response(self, req, resp, resource, req_succeeded):
|
||||
if not hasattr(resp.context, 'result'):
|
||||
return
|
||||
|
||||
resp.text = json.dumps(resp.context.result)
|
||||
|
||||
|
||||
def max_body(limit):
|
||||
def hook(req, resp, resource, params):
|
||||
length = req.content_length
|
||||
if length is not None and length > limit:
|
||||
msg = (
|
||||
'The size of the request is too large. The body must not '
|
||||
'exceed ' + str(limit) + ' bytes in length.'
|
||||
)
|
||||
|
||||
raise falcon.HTTPPayloadTooLarge(
|
||||
title='Request body is too large', description=msg
|
||||
)
|
||||
|
||||
return hook
|
||||
|
||||
class CreateTaskResource:
|
||||
def on_post(self, req, resp):
|
||||
quote = {
|
||||
'title': 'Получена задача',
|
||||
'description': req.context.doc['task']
|
||||
}
|
||||
|
||||
resp.media = quote
|
||||
|
||||
|
||||
app = falcon.App(
|
||||
middleware=[
|
||||
RequireJSON(),
|
||||
JSONTranslator()
|
||||
]
|
||||
)
|
||||
app.add_route('/create-task', CreateTaskResource())
|
Loading…
Reference in New Issue
Block a user