-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
84 lines (63 loc) · 2.34 KB
/
app.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
import os
from dotenv import load_dotenv
from flask import Flask, make_response, jsonify
from flask_login import LoginManager
from flask_restful import Api
from waitress import serve
from api.resources import languages_resource, users_resource, projects_resource
from blueprints import (
basket,
auth,
account,
projects,
main,
developer,
uploads
)
from data import db_session
from data.users import User
load_dotenv()
app = Flask(__name__)
api = Api(app)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
login_manager = LoginManager(app)
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
db_sess = db_session.create_session()
return db_sess.query(User).get(user_id)
@app.errorhandler(404)
def not_found(_):
return make_response(jsonify({"error": "Not found"}), 404)
@app.errorhandler(400)
def bad_request(_):
return make_response(jsonify({"error": "Bad Request"}), 400)
@app.errorhandler(403)
def forbidden(_):
return make_response(jsonify({"error": "Forbidden"}), 403)
@app.errorhandler(401)
def Unauthorized(_):
return make_response(jsonify({"error": "Unauthorized"}), 401)
@app.errorhandler(500)
def InternalServerError(_):
return make_response(jsonify({"error": "Internal Server Error"}), 500)
# запуск
if __name__ == "__main__":
host = os.getenv("HOST", "0.0.0.0")
port = int(os.getenv("PORT", 8000))
db_session.global_init("db/coders_share_database.db")
app.register_blueprint(basket.blueprint)
app.register_blueprint(auth.blueprint)
app.register_blueprint(account.blueprint)
app.register_blueprint(projects.blueprint)
app.register_blueprint(main.blueprint)
app.register_blueprint(developer.blueprint)
app.register_blueprint(uploads.blueprint)
api.add_resource(languages_resource.LanguagesListResource, "/api/<api_key>/languages")
api.add_resource(languages_resource.LanguagesResource, "/api/<api_key>/languages/<int:id>")
api.add_resource(users_resource.UsersListResource, "/api/<api_key>/users")
api.add_resource(users_resource.UsersResource, "/api/<api_key>/users/<int:id>")
api.add_resource(projects_resource.ProjectsListResource, "/api/<api_key>/projects")
api.add_resource(projects_resource.ProjectsResource, "/api/<api_key>/projects/<int:id>")
# app.run(host, port)
serve(app, host=host, port=port)