Source code for server.views_api.other

from flask import Blueprint, make_response, jsonify, current_app

from .. import socketio, slacklog, connections_devices
from ..protections import user_required, admin_required
from ..resources import devices

import pathlib

bp = Blueprint("other_api", __name__, url_prefix="/api")


[docs] @bp.route("/shutdown", methods=["POST"]) @admin_required def shutdown(): """Api endpoint to **restart** the server. The function below just cleanly shuts the server down. But when deployed, the server is managed as a service and gets automatically restarted. """ current_app.logger.info("Server shutdown initiated.") slacklog.info("Restart requested, server shutdown initiated.") socketio.stop() return jsonify(message="Server shutdown initiated."), 200
[docs] @bp.route("/news", methods=["GET"]) @user_required def news(): """Api endpoint to get a json array of latest news. Array may be empty. Example contents of a *news.json* file: .. code-block:: json [ { "header": "Big news", "description": "Yes, we have news.", "created": "27.4.2021", "expires": "1.5.2021" }, { "header": "Small news", "description": "Another one.", "created": "28.4.2021", "expires": "never" } ] """ news_file = pathlib.Path(current_app.root_path) / "news.json" if news_file.exists(): return make_response( news_file.read_text(), 200, {"Content-Type": "application/json"}, ) else: return jsonify([]), 200
[docs] @bp.route("/resource/camera-realtime/<id_>") @user_required def camera_realtime(id_): if not id_ in devices: return jsonify(message="Camera device is not online.", id=id_), 404 camera_name = devices[id_]["name"] if not camera_name.startswith("camera_"): return jsonify(message="Device is not a camera.", id=id_), 400 try: # Format: camera_name_24 --> streaming at port 5224 port = 5200 + int(camera_name.rpartition("_")[-1]) except ValueError: port = 5200 response = jsonify(message="Nginx internal redirect via X-Accel-Redirect.") response.headers["X-Accel-Redirect"] = "/resource/camera-realtime?host={}:{}".format( connections_devices.get_hosts(id_)[0], port ) return response, 200