forked from SQLMatches/API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
184 lines (152 loc) · 5.71 KB
/
__init__.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# -*- coding: utf-8 -*-
"""
GNU General Public License v3.0 (GPL v3)
Copyright (c) 2020-2020 WardPearce
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.sessions import SessionMiddleware
from starlette_wtf import CSRFProtectMiddleware
from secrets import token_urlsafe
from databases import Database
from aiohttp import ClientSession
import backblaze
from .tables import create_tables
from .resources import Sessions, Config
from .settings import DatabaseSettings, B2Settings
from .routes import ROUTES, ERROR_HANDLERS
from .http_middleware import APIMiddleware
__version__ = "0.0.13"
MAP_IMAGES = {
"de_austria": "austria.jpg",
"de_cache": "cache.jpg",
"de_canals": "canals.jpg",
"de_cbble": "cbble.jpg",
"de_dust": "dust.jpg",
"de_dust2": "dust2.jpg",
"de_inferno": "inferno.jpg",
"de_mirage": "mirage.jpg",
"de_nuke": "nuke.jpg",
"de_overpass": "overpass.jpg",
"de_train": "train.jpg",
}
class SQLMatches(Starlette):
def __init__(self, database_settings: DatabaseSettings,
b2_settings: B2Settings,
friendly_url: str,
secret_key: str = token_urlsafe(),
csrf_secret: str = token_urlsafe(),
map_images: dict = MAP_IMAGES,
upload_delay: float = 0.1,
max_upload_size: int = 80000000,
**kwargs) -> None:
"""
SQLMatches server.
database_settings: DatabaseSettings
Holds settings for database.
friendly_url: str
URL to project.
csrf_secret: str
Optionally pass your own url safe secret key.
secret_key: str
Optionally pass your own url safe secret key.
map_images: dict
Key as actual map name, value as image name.
upload_delay: float
by default 0.1
max_upload_size: int
by default 80000000
kwargs
Additional parameters to pass to the Starlette instance.
"""
startup_tasks = [self._startup]
shutdown_tasks = [self._shutdown]
if "on_startup" in kwargs:
startup_tasks = startup_tasks + kwargs["on_startup"]
if "on_shutdown" in kwargs:
shutdown_tasks = shutdown_tasks + kwargs["on_shutdown"]
middlewares = [
Middleware(SessionMiddleware, secret_key=secret_key),
Middleware(CSRFProtectMiddleware, csrf_secret=csrf_secret),
Middleware(APIMiddleware)
]
if "middleware" in kwargs:
middlewares = middlewares + kwargs["middleware"]
if "routes" in kwargs:
routes = kwargs["routes"] + ROUTES
else:
routes = ROUTES
if "exception_handlers" in kwargs:
exception_handlers = kwargs["exception_handlers"] + ERROR_HANDLERS
else:
exception_handlers = ERROR_HANDLERS
if friendly_url[:1] != "/":
friendly_url += "/"
Config.url = friendly_url
Config.map_images = map_images
Config.demo_pathway = b2_settings.pathway
Config.cdn_url = b2_settings.cdn_url
Config.upload_delay = upload_delay
Config.max_upload_size = max_upload_size
database_url = "://{}:{}@{}:{}/{}?charset=utf8mb4".format(
database_settings.username,
database_settings.password,
database_settings.server,
database_settings.port,
database_settings.database
)
Sessions.database = Database(
database_settings.engine + database_url
)
self.b2 = backblaze.Awaiting(
b2_settings.key_id,
b2_settings.application_key
)
Sessions.bucket = self.b2.bucket(
b2_settings.bucket_id
)
create_tables(
"{}+{}{}".format(
database_settings.engine,
database_settings.alchemy_engine,
database_url
)
)
Starlette.__init__(
self,
routes=routes,
exception_handlers=exception_handlers,
middleware=middlewares,
on_startup=startup_tasks,
on_shutdown=shutdown_tasks,
**kwargs
)
async def _startup(self) -> None:
"""
Starts up needed sessions.
"""
await Sessions.database.connect()
await self.b2.authorize()
Sessions.aiohttp = ClientSession()
async def _shutdown(self) -> None:
"""
Closes any underlying sessions.
"""
await Sessions.database.disconnect()
await self.b2.close()
await Sessions.aiohttp.close()