-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
167 lines (145 loc) · 4.35 KB
/
server.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import json
from os import environ
from typing import Optional
from fastapi import FastAPI
from psycopg_pool import AsyncConnectionPool
from pydantic import BaseModel
from starlette.middleware.exceptions import ExceptionMiddleware
from starlette.middleware.trustedhost import TrustedHostMiddleware
from account.router import app as account
from configs.router import app as configs
from emojis.router import app as emoji
from posts.router import app as posts
from probe.router import probes
from reporting.router import app as reporting
from sets.router import app as sets
from shared.config.constants import Environment, environment
from shared.config.repo import full_hash, name, short_hash
from shared.exceptions.base_error import BaseError
from shared.exceptions.handler import jsonErrorHandler
from shared.server.middleware import CustomHeaderMiddleware, HeadersToSet
from shared.server.middleware.auth import KhAuthMiddleware
from shared.server.middleware.cors import KhCorsMiddleware
from shared.sql import SqlInterface
from shared.timing import timed
from tags.router import app as tags
from users.router import app as users
timed.logger = lambda n, x : print(json.dumps({ n: x.dict() }))
app = FastAPI(
title = 'fuzz.ly',
# docs_url = None,
# redoc_url = None,
)
app.add_middleware(ExceptionMiddleware, handlers={ Exception: jsonErrorHandler }, debug=False)
app.add_exception_handler(BaseError, jsonErrorHandler)
app.middleware('http')(CustomHeaderMiddleware)
app.add_middleware(
KhCorsMiddleware,
allowed_origins = {
'localhost',
'127.0.0.1',
'api.dev.fuzz.ly',
'api-dev.fuzz.ly',
'dev.fuzz.ly',
'api.fuzz.ly',
'fuzz.ly',
},
allowed_protocols = set(['http', 'https']
if environment.is_local()
else ['https']),
allowed_headers = [
'accept',
'accept-language',
'authorization',
'cache-control',
'content-encoding',
'content-language',
'content-length',
'content-security-policy',
'content-type',
'cookie',
'host',
'location',
'referer',
'referrer-policy',
'set-cookie',
'user-agent',
'www-authenticate',
'x-frame-options',
'x-xss-protection',
],
allowed_methods = [
'GET',
'PUT',
'POST',
'PATCH',
'DELETE',
],
exposed_headers = [
'authorization',
'cache-control',
'content-type',
'cookie',
'set-cookie',
'www-authenticate',
] + list(HeadersToSet.keys()),
max_age = 86400,
)
app.add_middleware(TrustedHostMiddleware, allowed_hosts=[
environ.get('pod_ip', '127.0.0.1'),
environ.get('pod_host', 'localhost'),
])
app.add_middleware(KhAuthMiddleware, required=False)
# app.mount('/static', StaticFiles(directory = 'static'), name = 'static')
# @app.get('/docs', include_in_schema = False)
# async def custom_swagger_ui_html():
# return get_swagger_ui_html(
# openapi_url = app.openapi_url or '',
# title = app.title + ' - Swagger UI',
# oauth2_redirect_url = app.swagger_ui_oauth2_redirect_url,
# swagger_js_url = 'https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js',
# swagger_css_url = 'https://unpkg.com/swagger-ui-dist@5/swagger-ui.css',
# )
# @app.get(app.swagger_ui_oauth2_redirect_url or '', include_in_schema = False)
# async def swagger_ui_redirect():
# return get_swagger_ui_oauth2_redirect_html()
# @app.get('/redoc', include_in_schema = False)
# async def redoc_html():
# return get_redoc_html(
# openapi_url = app.openapi_url or '',
# title = app.title + ' - ReDoc',
# redoc_js_url = 'https://unpkg.com/redoc@next/bundles/redoc.standalone.js',
# )
@app.on_event('startup')
async def startup() :
if getattr(SqlInterface, 'pool', None) is None :
SqlInterface.pool = AsyncConnectionPool(' '.join(map('='.join, SqlInterface.db.items())), open=False)
await SqlInterface.pool.open()
class VersionInfo(BaseModel) :
short: str
full: str
class ServiceInfo(BaseModel) :
name: str
pod: Optional[str]
environment: Environment
version: VersionInfo
@app.get('/')
def root() -> ServiceInfo :
return ServiceInfo(
name = name,
pod = environ.get('pod_name', None),
environment = environment,
version = VersionInfo(
short = short_hash,
full = full_hash,
),
)
app.include_router(probes)
app.include_router(account)
app.include_router(configs)
app.include_router(posts)
app.include_router(sets)
app.include_router(tags)
app.include_router(users)
app.include_router(emoji)
app.include_router(reporting)