SuperScriptMaterializer/SuperScriptViewer/ServerCore.py

101 lines
2.8 KiB
Python
Raw Normal View History

import CustomConfig
import Database
from flask import Flask
from flask import g
from flask import render_template
from flask import url_for
from flask import request
2020-04-13 15:36:37 +08:00
from flask import abort
2020-04-18 12:49:48 +08:00
from flask import redirect
app = Flask(__name__)
2020-04-18 12:49:48 +08:00
# =============================================database
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = Database.CreateDatabase()
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
Database.CloseDatabase(db)
2020-04-12 16:02:20 +08:00
2020-04-18 12:49:48 +08:00
# =============================================route
# =========== default
@app.route('/', methods=['GET'])
def handle_nospec():
return redirect(url_for('handle_index'))
2020-04-18 12:49:48 +08:00
# =========== basic pages
@app.route('/index', methods=['GET'])
def handle_index():
return app.send_static_file("html/index.html")
@app.route('/script', methods=['GET'])
def handle_script():
return redirect(url_for('handle_index'))
@app.route('/array', methods=['GET'])
def handle_array():
return redirect(url_for('handle_index'))
@app.route('/environment', methods=['GET'])
def handle_environment():
return redirect(url_for('handle_index'))
2020-04-18 12:49:48 +08:00
@app.route('/help', methods=['GET'])
def handle_help():
return app.send_static_file("html/help.html")
2020-04-18 12:49:48 +08:00
@app.route('/about', methods=['GET'])
def handle_about():
return app.send_static_file("html/about.html")
# =========== viewer
# script and array should have at least 2 items splitted by slash(/)
# the first one is document id and the second one is the real CK_ID of viewing object.
# however, environment do not have this, environment only allow one item, the id of environment.
@app.route('/script/<path:script_path>', methods=['GET'])
def handle_script_viewer(script_path):
# check invalid url
if len(script_path.split('/')) < 2:
return redirect(url_for('handle_index'))
return app.send_static_file("html/viewer_script.html")
@app.route('/array/<path:array_path>', methods=['GET'])
def handle_array_viewer(array_path):
# check invalid url
if len(script_path.split('/')) < 2:
return redirect(url_for('handle_index'))
return app.send_static_file("html/viewer_array.html")
@app.route('/environment/<path:environment_path>', methods=['GET'])
def handle_environment_viewer(environment_path):
# check invalid url
if len(script_path.split('/')) > 1:
return redirect(url_for('handle_index'))
return app.send_static_file("html/viewer_environment.html")
# =========== viewer
@app.route('/api/index/getList', methods=['POST'])
def handle_api_index_getList(scriptPath):
return {
"document": [],
"script": [],
"array": [],
"environment": []
}
2020-04-13 12:35:41 +08:00
# =========== startup
def run():
app.run()