-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
402 lines (332 loc) · 9.24 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/python3.9
# from asyncio import run as aiorun
from pathlib import Path
from typing import Optional
from aiohttp.web import Application
from aiohttp.web import run_app
from aiohttp.web import get as web_GET
from aiohttp.web import post as web_POST
from aiohttp.web import delete as web_DELETE
from aiohttp.web import Request,Response,json_response
from motor.motor_asyncio import AsyncIOMotorClient
from control_Any import _TYPE_CUSTOM
from control_Any import get_client_type
from control_Any import route_src
from control_Any import route_main as route_Home
from control_admin import _ROUTE_PAGE as _ROUTE_PAGE_ADMIN
from control_admin import route_main as route_Admin
from control_admin import route_api_update_config as route_Admin_api_UpdateConfig
from control_admin import route_api_update_known_asset_names as route_Admin_api_UpdateKnownAssetNames
from control_assets import _CACHE_ASSETS
from control_assets import _ROUTE_PAGE as _ROUTE_PAGE_ASSETS
from control_assets import route_main as route_Assets
from control_assets import route_api_select_asset as route_Assets_api_GetAsset
from control_assets import route_fgmt_asset_editor as route_Assets_fgmt_EditAsset
from control_assets import route_fgmt_new_asset as route_Assets_fgmt_NewAsset
from control_assets import route_api_new_asset as route_Assets_api_NewAsset
from control_assets import route_api_asset_metadata_change as route_Assets_api_ChangeMetadata
from control_assets import route_fgmt_search_assets as route_Assets_fgmt_SearchAssets
from control_assets import route_api_search_assets as route_Assets_api_SearchAssets
from control_assets import route_api_drop_asset as route_Assets_api_DropAsset
from control_assets import route_api_add_record as route_Assets_api_AddRecord
from control_assets import route_api_get_record as route_Assets_api_GetRecord
from control_assets import util_update_known_assets as init_assets_cache
from control_orders import _ROUTE_PAGE as _ROUTE_PAGE_ORDERS
from control_orders import route_main as route_Orders
from control_orders import route_fgmt_new_order as route_Orders_fgmt_NewOrder
from control_orders import route_fgmt_list_orders as route_Orders_fgmt_ListOrders
from control_orders import route_fgmt_order_editor as route_Order_fgmt_Editor
from control_orders import route_api_new_order as route_Orders_api_NewOrder
from control_orders import route_api_delete_order as route_Orders_api_DeleteOrder
from control_orders import route_api_update_asset_in_order as route_Orders_api_UpdateAsset
from control_orders import route_api_remove_asset_from_order as route_Orders_api_RemoveAsset
from control_orders import route_api_run_order as route_Orders_api_RunOrder
from internals import read_yaml_file
from internals import util_valid_int
from internals import util_valid_int_inrange
from internals import util_valid_str
def read_config(path_config:Path)->dict:
print(
"CFG File:\n"
f"\t{path_config.absolute()}\n"
)
rawconfig=read_yaml_file(path_config)
# port
cfg_port=util_valid_int_inrange(
util_valid_int(
rawconfig.get("port"),
),
minimum=1024,
maximum=65536
)
if not isinstance(cfg_port,int):
print(
"'port' key not found in config or not in the range 1024 < x < 65536"
)
return {}
# lang
cfg_lang=util_valid_str(
rawconfig.get("lang"),
True
)
if not isinstance(cfg_lang,str):
print(
"'lang' key not found in config"
)
return {}
if cfg_lang not in ("en","es"):
print(
"Language unsupported or not found in config file, defaulting to English" "\n"
"Available languages are: 'en' (English) and 'es' (Spanish)"
)
cfg_lang="en"
# db-name
cfg_db_name=util_valid_str(
rawconfig.get("db-name"),
True
)
if not isinstance(cfg_db_name,str):
print(
"'db-name' key not found in config"
)
return {}
# db-url
cfg_db_url=util_valid_str(
rawconfig.get("db-url"),
True
)
if not isinstance(cfg_db_url,str):
print(
"'db-url' key not found in config: A local database will be used instead"
)
# all ok
return {
"db-name":cfg_db_name,
"db-url":cfg_db_url,
"port":cfg_port,
"lang":cfg_lang
}
async def mware_factory(app,handler):
async def mware_handler(request:Request):
# Local source files
if request.path.startswith("/src/"):
return (
await handler(request)
)
ct=get_client_type(request)
if not isinstance(ct,str):
return Response(status=406)
if ct==_TYPE_CUSTOM:
if (
request.path=="/" or
request.path.startswith("/page/") or
request.path.startswith("/fgmt/")
):
return json_response(data={})
return (
await handler(request)
)
return mware_handler
def build_app(
path_programdir:Path,
lang:str,
rdb_name:str,
rdb_url:Optional[str],
)->Application:
app=Application(
middlewares=[mware_factory]
)
app["path_programdir"]=path_programdir
app[_CACHE_ASSETS]={}
app["rdbn"]=rdb_name
app["lang"]=lang
has_connection_url=isinstance(rdb_name,str)
if has_connection_url:
print("Connecting to a local MongoDB database")
app["rdbc"]=AsyncIOMotorClient()
if not has_connection_url:
print(
"Connecting to a remote MongoDB database:",
rdb_url
)
app["rdbc"]=AsyncIOMotorClient(rdb_url)
app.add_routes([
web_GET(
"/src/{srctype}/{filename}",
route_src
),
web_GET(
"/",
route_Home
),
# ADMIN
web_GET(
_ROUTE_PAGE_ADMIN,
route_Admin
),
web_POST(
"/api/admin/update-config",
route_Admin_api_UpdateConfig
),
web_POST(
"/api/admin/update-known-assets",
route_Admin_api_UpdateKnownAssetNames
),
# ASSETS
web_GET(
_ROUTE_PAGE_ASSETS,
route_Assets
),
web_GET(
"/fgmt/assets/new",
route_Assets_fgmt_NewAsset
),
web_POST(
"/api/assets/new",
route_Assets_api_NewAsset
),
web_GET(
"/fgmt/assets/search-assets",
route_Assets_fgmt_SearchAssets
),
web_POST(
"/api/assets/search-assets",
route_Assets_api_SearchAssets
),
web_GET(
"/fgmt/assets/editor/{asset_id}",
route_Assets_fgmt_EditAsset
),
web_POST(
"/api/assets/select/{asset_id}",
route_Assets_api_GetAsset
),
web_POST(
"/api/assets/change-metadata",
route_Assets_api_ChangeMetadata
),
web_DELETE(
"/api/assets/drop",
route_Assets_api_DropAsset
),
web_POST(
"/api/assets/history/{asset_id}/add",
route_Assets_api_AddRecord
),
web_GET(
"/api/assets/history/{asset_id}/records/{record_uid}",
route_Assets_api_GetRecord
),
# ORDERS
web_GET(
# "/page/orders",
_ROUTE_PAGE_ORDERS,
route_Orders
),
web_GET(
"/fgmt/orders/new",
route_Orders_fgmt_NewOrder
),
web_POST(
"/api/orders/new",
route_Orders_api_NewOrder
),
web_GET(
"/fgmt/orders/current",
route_Orders_fgmt_ListOrders
),
web_GET(
"/fgmt/orders/current/{order_id}/search-assets",
route_Assets_fgmt_SearchAssets
),
web_POST(
"/api/orders/current/{order_id}/search-assets",
route_Assets_api_SearchAssets
),
web_GET(
"/fgmt/orders/current/{order_id}/editor",
route_Order_fgmt_Editor
),
web_POST(
"/api/orders/current/{order_id}/update",
route_Orders_api_UpdateAsset
),
web_DELETE(
"/api/orders/current/{order_id}/update",
route_Orders_api_RemoveAsset
),
web_POST(
"/api/orders/current/{order_id}/run",
route_Orders_api_RunOrder
),
web_DELETE(
"/api/orders/current/{order_id}/drop",
route_Orders_api_DeleteOrder
),
web_DELETE(
"/api/orders/current/{order_id}/drop-fol",
route_Orders_api_DeleteOrder
),
# web_GET(
# "/page/assets/new",
# route_Assets_page_NewAsset
# ),
# web_GET(
# "/page/assets/view/{asset_id}",
# route_Assets_page_ViewAsset
# ),
# web_POST(
# "/api/assets/create",
# route_Assets_api_CreateAsset
# ),
# web_GET(
# "/api/assets/select/{asset_id}",
# route_Assets_api_GetAsset
# ),
# web_GET(
# "/api/assets/search",
# route_Assets_api_SearchAssets
# ),
# web_DELETE(
# "/api/assets/delete/{asset_id}",
# route_Assets_api_DropAsset
# ),
# web_GET(
# "/api/assets/select/{asset_id}/modev-view/{modev_date}/{modev_uid}",
# route_Assets_api_GetRecord
# ),
# web_DELETE(
# "/api/assets/select/{asset_id}/modev-drop",
# route_Assets_api_DropRecord
# )
])
app.on_startup.append(init_assets_cache)
return app
if __name__=="__main__":
from sys import argv as sys_argv
from sys import exit as sys_exit
path_appdir=Path(sys_argv[0]).parent
the_config=read_config(
path_appdir.joinpath(
"config.yaml"
)
)
cfg_port=the_config.get("port")
if not isinstance(cfg_port,int):
sys_exit(1)
cfg_lang=the_config.get("lang")
if not isinstance(cfg_lang,str):
sys_exit(1)
cfg_db_name=the_config.get("db-name")
if not isinstance(cfg_db_name,str):
sys_exit(1)
cfg_db_url=the_config.get("db-url")
run_app(
build_app(
path_appdir,
cfg_lang,
cfg_db_name,
cfg_db_url
),
port=cfg_port,
)