-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
100 lines (74 loc) · 2.68 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
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
import asyncio
import json
import logging
import sentry_sdk
from sentry_asgi import SentryMiddleware
from starlette.applications import Starlette
from starlette.authentication import requires
from starlette.exceptions import HTTPException
from starlette.middleware.authentication import AuthenticationMiddleware
from starlette.middleware.cors import CORSMiddleware
from starlette.middleware.gzip import GZipMiddleware
from starlette.responses import JSONResponse
from starlette_jwt import JWTAuthenticationBackend
from starlette_prometheus import metrics
from starlette_prometheus import PrometheusMiddleware
from tartiflette_starlette import mount
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
from config import *
from graphql import graphql_app
# instantiate the main webapp
app = Starlette(debug=DEBUG)
if DEBUG:
import logging
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)
# enable permissive CORS
# https://www.starlette.io/middleware/#corsmiddleware
app.add_middleware(
CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]
)
# enable gzip for responses over 500 bytes
# https://www.starlette.io/middleware/#gzipmiddleware
app.add_middleware(GZipMiddleware)
# enable sentry middleware if a DSN is configured
if SENTRY_DSN:
# https://github.com/encode/sentry-asgi
sentry_sdk.init(dsn=SENTRY_DSN)
app.add_middleware(SentryMiddleware)
# if deployed behind a proxy set the client data from the X-Forwarded headers
if BEHIND_PROXY:
# https://github.com/encode/uvicorn/blob/master/uvicorn/middleware/proxy_headers.py
app.add_middleware(ProxyHeadersMiddleware)
# if metrics are enabled load the prometheus middleware
# https://github.com/perdy/starlette-prometheus
if METRICS:
app.add_middleware(PrometheusMiddleware)
app.add_route("/metrics/", metrics)
# enable JWT
# https://github.com/amitripshtos/starlette-jwt
def on_auth_error(request, exc):
return JSONResponse({"error": str(exc), "success": False}, status_code=401)
app.add_middleware(
AuthenticationMiddleware,
on_error=on_auth_error,
backend=JWTAuthenticationBackend(
secret_key=JWT_KEY,
algorithm=JWT_ALGORITHM,
prefix=JWT_PREFIX,
username_field=JWT_USER_FIELD,
),
)
# Add a generic exception handler
@app.exception_handler(HTTPException)
async def http_exception(request, exc):
return JSONResponse(
{"error": exc.detail, "success": False}, status_code=exc.status_code
)
# example authenticated route
@app.route("/example")
@requires("authenticated")
async def example(request):
return JSONResponse({"success": True})
mount.starlette(app, "/", graphql_app)