2019-06-12 13:55:49 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import json
|
2023-09-08 20:53:04 +08:00
|
|
|
from typing import *
|
2019-06-12 13:55:49 +08:00
|
|
|
|
|
|
|
import tornado.web
|
|
|
|
|
2024-11-02 23:22:54 +08:00
|
|
|
import config
|
|
|
|
|
2019-06-12 13:55:49 +08:00
|
|
|
|
2023-09-08 20:53:04 +08:00
|
|
|
class ApiHandler(tornado.web.RequestHandler):
|
2021-07-17 13:03:45 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2023-09-08 20:53:04 +08:00
|
|
|
self.json_args: Optional[dict] = None
|
2021-07-17 13:03:45 +08:00
|
|
|
|
2024-11-02 23:22:54 +08:00
|
|
|
def set_default_headers(self):
|
2023-07-29 23:05:04 +08:00
|
|
|
self.set_header('Cache-Control', 'no-cache')
|
|
|
|
|
2024-11-02 23:22:54 +08:00
|
|
|
self.add_header('Vary', 'Origin')
|
|
|
|
origin = self.request.headers.get('Origin', None)
|
|
|
|
if origin is None:
|
|
|
|
return
|
|
|
|
cfg = config.get_config()
|
|
|
|
if not cfg.is_allowed_cors_origin(origin):
|
|
|
|
return
|
|
|
|
|
|
|
|
self.set_header('Access-Control-Allow-Origin', origin)
|
|
|
|
self.set_header('Access-Control-Allow-Methods', '*')
|
|
|
|
self.set_header('Access-Control-Allow-Headers', '*')
|
|
|
|
self.set_header('Access-Control-Max-Age', '3600')
|
|
|
|
|
|
|
|
def prepare(self):
|
2021-07-17 13:03:45 +08:00
|
|
|
if not self.request.headers.get('Content-Type', '').startswith('application/json'):
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
self.json_args = json.loads(self.request.body)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
pass
|
2024-11-02 23:22:54 +08:00
|
|
|
|
|
|
|
async def options(self, *_args, **_kwargs):
|
|
|
|
self.set_status(204)
|