-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.py
executable file
·62 lines (48 loc) · 1.44 KB
/
rest.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from aiohttp import web
from aiohttp_session import setup
from aiohttp_session.cookie_storage import EncryptedCookieStorage
from cryptography import fernet
import psycopg2
import aiopg
import base64
import sys
# HTTP requests handlers
from handlers.authorization import authorize, authorization
from handlers.vacancy.getitem import getitem
from handlers.vacancy.listitems import listitems
from handlers.vacancy.additem import additem
from handlers.vacancy.removeitem import removeitem
from sql.logic import SQL
# Routes definitions
from routes import routes
# Signal handlers
from signals.pg_close import pg_close
# Configuration
from config import config
###############################
# Simple REST server
# (c) Severogor
# 27.09.2018
# Personal use only
###############################
async def init():
"""Initialize web application
returns: application coroutine object
"""
# Base application object
app = web.Application()
# Session setup
key = base64.urlsafe_b64decode(fernet.Fernet.generate_key())
setup(app, EncryptedCookieStorage(key))
# Routes
app.add_routes(routes)
# Database
app['db'] = await aiopg.connect(config["pg_dsn"], cursor_factory=psycopg2.extras.DictCursor)
app['sql'] = SQL
# Shutdown signal handler
app.on_shutdown.append(pg_close)
return app
app = init()
web.run_app(app, host=config["host"], port=config["port"])