selfprivacy-rest-api/selfprivacy_api/app.py

89 lines
2.8 KiB
Python
Raw Normal View History

2021-11-11 20:31:28 +02:00
#!/usr/bin/env python3
2021-11-16 18:14:01 +02:00
"""SelfPrivacy server management API"""
import os
2021-12-06 08:48:29 +02:00
from gevent import monkey
2021-11-16 12:32:10 +02:00
from flask import Flask, request, jsonify
2021-11-11 20:31:28 +02:00
from flask_restful import Api
2021-11-16 18:14:01 +02:00
from flask_swagger import swagger
from flask_swagger_ui import get_swaggerui_blueprint
2021-11-11 20:31:28 +02:00
2021-11-16 18:14:01 +02:00
from selfprivacy_api.resources.users import User, Users
2021-12-14 11:52:17 +02:00
from selfprivacy_api.resources.common import ApiVersion
2021-11-16 18:14:01 +02:00
from selfprivacy_api.resources.system import api_system
from selfprivacy_api.resources.services import services as api_services
2021-12-06 08:48:29 +02:00
from selfprivacy_api.restic_controller.tasks import huey, init_restic
2021-11-16 18:14:01 +02:00
swagger_blueprint = get_swaggerui_blueprint(
"/api/docs", "/api/swagger.json", config={"app_name": "SelfPrivacy API"}
)
2021-11-11 20:31:28 +02:00
def create_app(test_config=None):
2021-11-16 18:14:01 +02:00
"""Initiate Flask app and bind routes"""
2021-11-11 20:31:28 +02:00
app = Flask(__name__)
api = Api(app)
if test_config is None:
app.config["AUTH_TOKEN"] = os.environ.get("AUTH_TOKEN")
if app.config["AUTH_TOKEN"] is None:
raise ValueError("AUTH_TOKEN is not set")
app.config["ENABLE_SWAGGER"] = os.environ.get("ENABLE_SWAGGER", "0")
2021-12-02 17:06:23 +02:00
app.config["B2_BUCKET"] = os.environ.get("B2_BUCKET")
else:
app.config.update(test_config)
2021-11-16 12:32:10 +02:00
# Check bearer token
@app.before_request
def check_auth():
2021-11-16 18:14:01 +02:00
# Exclude swagger-ui
if not request.path.startswith("/api"):
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-18 09:35:50 +02:00
api.add_resource(ApiVersion, "/api/version")
2021-11-11 20:31:28 +02:00
api.add_resource(Users, "/users")
2021-11-16 18:14:01 +02:00
api.add_resource(User, "/users/<string:username>")
2021-11-11 20:31:28 +02:00
app.register_blueprint(api_system)
app.register_blueprint(api_services)
2021-11-16 18:14:01 +02:00
@app.route("/api/swagger.json")
def spec():
if app.config["ENABLE_SWAGGER"] == "1":
swag = swagger(app)
2021-11-22 18:50:50 +02:00
swag["info"]["version"] = "1.1.0"
2021-11-16 18:14:01 +02:00
swag["info"]["title"] = "SelfPrivacy API"
swag["info"]["description"] = "SelfPrivacy API"
swag["securityDefinitions"] = {
"bearerAuth": {
"type": "apiKey",
"name": "Authorization",
"in": "header",
}
}
swag["security"] = [{"bearerAuth": []}]
return jsonify(swag)
return jsonify({}), 404
if app.config["ENABLE_SWAGGER"] == "1":
app.register_blueprint(swagger_blueprint, url_prefix="/api/docs")
2021-11-11 20:31:28 +02:00
return app
if __name__ == "__main__":
2021-12-06 08:48:29 +02:00
monkey.patch_all()
2021-11-16 18:14:01 +02:00
created_app = create_app()
2021-12-06 08:48:29 +02:00
huey.start()
init_restic()
2021-11-16 18:14:01 +02:00
created_app.run(port=5050, debug=False)