selfprivacy-rest-api/selfprivacy_api/app.py

41 lines
1.1 KiB
Python
Raw Normal View History

2021-11-11 18:31:28 +00:00
#!/usr/bin/env python3
2021-11-16 10:32:10 +00:00
from flask import Flask, request, jsonify
2021-11-11 18:31:28 +00:00
from flask_restful import Api
2021-11-16 10:32:10 +00:00
import os
2021-11-11 18:31:28 +00:00
from selfprivacy_api.resources.users import Users
from selfprivacy_api.resources.common import DecryptDisk
def create_app():
app = Flask(__name__)
api = Api(app)
2021-11-16 10:32:10 +00:00
app.config['AUTH_TOKEN'] = os.environ.get('AUTH_TOKEN')
# Check bearer token
@app.before_request
def check_auth():
auth = request.headers.get("Authorization")
if auth is None:
return jsonify({"error": "Missing Authorization header"}), 401
# Check if token is valid
if auth != "Bearer " + app.config['AUTH_TOKEN']:
return jsonify({"error": "Invalid token"}), 401
2021-11-11 18:31:28 +00:00
api.add_resource(Users, "/users")
api.add_resource(DecryptDisk, "/decryptDisk")
from selfprivacy_api.resources.system import api_system
from selfprivacy_api.resources.services import services as api_services
app.register_blueprint(api_system)
app.register_blueprint(api_services)
return app
if __name__ == "__main__":
app = create_app()
app.run(port=5050, debug=False)