api_v1.4/bootstrap.py

85 lines
2.4 KiB
Python

"""
Main
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2021 Project Author name guillermo.gutierrezmorote@concordia.ca
Project Collaborator name peteryefi@gmail.com
"""
import datetime
import flask
import yaml
from pathlib import Path
from flasgger import LazyJSONEncoder, Swagger
from flask import Response
from flask_restful import Api
from flask_cors import CORS
import threading
import hub_api.helpers.session_helper as sh
from hub_api.control.session import SessionStart, SessionEnd, KeepSessionAlive
from hub_api.control.uptime import Uptime
from hub_api.persistence.retrofit_results import RetrofitResults
from hub_api.workflow.insel_montly_energy_balance import InselMonthlyEnergyBalance
from hub_api.workflow.costs import Costs
from hub_api.workflow.energy_plus import EnergyPlus
from hub_api.energy_plus.idf_generator import IdfGenerator
sh.begin_time = datetime.datetime.now()
app = flask.Flask('cerc_api')
app.json_provider_class = LazyJSONEncoder
api = Api(app)
CORS(app)
api.add_resource(Uptime, '/v1.4/uptime')
# Session
api.add_resource(SessionStart, '/v1.4/session/start')
api.add_resource(SessionEnd, '/v1.4/session/end')
api.add_resource(KeepSessionAlive, '/v1.4/session/keep-alive')
# persistence
api.add_resource(RetrofitResults, '/v1.4/persistence/retrofit-results')
# energy plus
api.add_resource(IdfGenerator, '/v1.4/energy-plus/idf-generator')
# workflows
api.add_resource(Costs, '/v1.4/workflow/costs')
api.add_resource(EnergyPlus, '/v1.4/workflow/energy-plus')
api.add_resource(InselMonthlyEnergyBalance, '/v1.4/workflow/insel-monthly-energy-balance')
yml_path = Path('./docs/openapi-specs.yml').resolve()
with open(yml_path, "r") as stream:
swagger_config = {
"headers": [],
"specs": [
{
"endpoint": '/api/apispec',
"route": '/api/apispec/apispec.json',
"rule_filter": lambda rule: True, # all in
"model_filter": lambda tag: True, # all in
}
],
"static_url_path": "/api/static",
"specs_route": "/api/api-docs/",
"openapi": "3.0.0"
}
try:
Swagger(app, template=yaml.safe_load(stream), config=swagger_config)
except yaml.YAMLError as exc:
print(f'error: {exc}')
@app.route("/")
def home():
return Response(headers={'Access-Control-Allow-Origin': '*'})
sh.debug_mode = False
threading.Thread(target=sh.expired_sessions_collector, daemon=True, args="5").start()
app.run(port=15789, host="0.0.0.0", debug=sh.debug_mode)