-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.py
87 lines (63 loc) 路 1.95 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# app.py
import os
from typing import Any
from sanic import HTTPResponse, Sanic
import sanic.response
from sanic_ext import Config
from api import api
from config import openapi_title, openapi_version, openapi_description, hostnames
from api.utils.limiter import configure_limiter
from api.utils.auth import configure_auth
import sentry_sdk
if os.environ.get("SENTRY_DSN"):
sentry_sdk.init(
dsn=os.environ["SENTRY_DSN"],
enable_tracing=True,
traces_sample_rate=1.0,
)
else:
print("WARNING: Sentry DSN not set, not enabling Sentry")
REDIRECTS = {
"/": "/docs/swagger",
}
app = Sanic("revanced-api")
app.extend(config=Config(oas_ignore_head=False))
app.ext.openapi.describe(
title=openapi_title,
version=openapi_version,
description=openapi_description,
)
app.config.CORS_ALWAYS_SEND = True
app.config.CORS_AUTOMATIC_OPTIONS = True
app.config.CORS_VARY_HEADER = True
app.config.CORS_METHODS = ["GET", "HEAD", "OPTIONS"]
app.config.CORS_SUPPORTS_CREDENTIALS = True
app.config.CORS_SEND_WILDCARD = True
app.config.CORS_ORIGINS = "*"
# sanic-beskar
configure_auth(app)
# sanic-limiter
configure_limiter(app)
app.blueprint(api)
# https://sanic.dev/en/guide/how-to/static-redirects.html
def get_static_function(value) -> Any:
return lambda *_, **__: value
for src, dest in REDIRECTS.items():
app.route(src)(get_static_function(sanic.response.redirect(dest)))
@app.on_request
async def domain_check(request) -> HTTPResponse:
if request.host not in hostnames:
return sanic.response.redirect(f"https://api.revanced.app/{request.path}")
@app.on_response
async def add_cache_control(_, response):
response.headers["Cache-Control"] = "public, max-age=300"
@app.on_response
async def add_csp(_, response):
response.headers[
"Content-Security-Policy"
] = "default-src * 'unsafe-inline' 'unsafe-eval' data: blob:;"
app.static(
"/favicon.ico",
"static/img/favicon.ico",
name="favicon",
)