-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
129 lines (114 loc) · 3.75 KB
/
api.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
from flask import Flask
from flask_restful import Api
from flask_jwt_extended import JWTManager
from flasgger import Swagger
import json
from resources.user.Login import Login
from resources.user.Signup import Signup
from resources.user.password.ForgetPassword import ForgetPassword
from resources.user.password.VerifyCode import VerifyCode
from resources.user.password.ResetPassword import ResetPassword
from resources.user.Publicity import Publicity
from resources.user.Socializing.Activity import Activity
from resources.links.LinksDeleter import LinksDeleter
from resources.links.LinksAdder import LinksAdder
from resources.links.LinksGetter import LinksGetter
from resources.categories.CategoriesGetter import CategoriesGetter
from resources.links.LinksGetterByCategory import LinksGetterByCategory
from resources.links.LinksGetterBySearch import LinksGetterBySearch
from resources.links.LinksUpdateCategory import LinksUpdateCategory
from resources.links.LinksByUserId import LinksByUserId
from db import db
app = Flask(__name__)
api = Api(app)
swagger = Swagger(app)
jwt = JWTManager(app)
# Load config file
with open('config.json', mode='r') as config_file:
CONFIG = json.load(config_file)
# Database connection
database_username = CONFIG.get('database', {}).get('username')
database_password = CONFIG.get('database', {}).get('password')
database_server = CONFIG.get('database', {}).get('server')
database_db = CONFIG.get('database', {}).get('db')
if CONFIG.get('database', {}).get('mysql'):
database_uri = (f'mysql+pymysql://{database_username}:{database_password}@'
f'{database_server}/{database_db}')
else:
database_uri = 'sqlite:///tests/test.db'
# Configs
app.debug = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['JWT_SECRET_KEY'] = 'super-secret' # Change this!
app.config['SQLALCHEMY_DATABASE_URI'] = database_uri
app.config['SQLALCHEMY_ECHO'] = False
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
# Routes
api.add_resource(
Login, CONFIG.get('routes', {}).get('user', {}).get('login')
)
api.add_resource(
Signup, CONFIG.get('routes', {}).get('user', {}).get('signup')
)
api.add_resource(
LinksGetter,
CONFIG.get('routes', {}).get('links', {}).get('main'),
CONFIG.get('routes', {}).get('links', {}).get('get_id'),
endpoint='links'
)
api.add_resource(
LinksDeleter,
CONFIG.get('routes', {}).get('links', {}).get('get_id')
)
api.add_resource(
LinksAdder,
CONFIG.get('routes', {}).get('links', {}).get('main')
)
api.add_resource(
CategoriesGetter,
CONFIG.get('routes', {}).get('categories', {}).get('main'),
CONFIG.get('routes', {}).get('categories', {}).get('get_id'),
endpoint='categories'
)
api.add_resource(
LinksGetterByCategory,
CONFIG.get('routes', {}).get('links', {}).get('by_category_id'),
)
api.add_resource(
LinksGetterBySearch,
CONFIG.get('routes', {}).get('links', {}).get('by_pattern_string')
)
api.add_resource(
ForgetPassword,
CONFIG.get('routes', {}).get('user', {}).get('forget_password')
)
api.add_resource(
VerifyCode,
CONFIG.get('routes', {}).get('user', {}).get('verify_code')
)
api.add_resource(
ResetPassword,
CONFIG.get('routes', {}).get('user', {}).get('reset_password')
)
api.add_resource(
LinksUpdateCategory,
CONFIG.get('routes', {}).get('links', {}).get('update_category')
)
api.add_resource(
Publicity,
CONFIG.get('routes', {}).get('user', {}).get('publicity')
)
api.add_resource(
Activity,
CONFIG.get('routes', {}).get('user', {}).get('activity_list')
)
api.add_resource(
LinksByUserId,
CONFIG.get('routes', {}).get('links', {}).get('by_user_id')
)
@app.before_first_request
def create_tables():
db.create_all()
if __name__ == '__main__':
app.run(host=CONFIG.get('host'))