main.py
2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
from flask import Flask, send_from_directory
from flask.ext.cors import CORS
from jinja2 import Environment, PackageLoader
from corretor import Corretor
import os
import pyutil
app = Flask(__name__)
controller = None
@app.route("/update_project")
def update_project():
try:
return controller.update_project()
except:
pyutil.print_stack_trace()
raise
@app.route("/create_project")
def create_project():
try:
return controller.create_project()
except:
pyutil.print_stack_trace()
raise
@app.route('/upload', methods=["POST"])
def upload_file():
try:
return controller.upload_file()
except:
pyutil.print_stack_trace()
raise
@app.route("/render_video", methods=["POST"])
def render_video():
try:
return controller.render_video()
except:
pyutil.print_stack_trace()
raise
@app.route("/finish_task", methods=["POST"])
def finish_task():
try:
return controller.finish_task()
except:
pyutil.print_stack_trace()
raise
def read_settings(app):
here = os.path.abspath(__file__)
config_path = os.path.join(os.path.dirname(here), 'settings_local.py')
if os.path.exists(config_path):
app.config.from_pyfile(config_path)
if app.config['APACHE_HOST']:
app.config['HOST_ENDPOINT'] = "http://" + app.config['SERVER_HOST'] + app.config['APACHE_HOST_ENDPOINT']
app.config['HOST_STATIC_FILES_ENDPOINT'] = "http://" + app.config['SERVER_HOST'] + app.config['APACHE_STATIC_FILES_ENDPOINT']
else:
app.config['HOST_ENDPOINT'] = "http://" + app.config['SERVER_HOST'] + ":" + str(app.config['SERVER_PORT'])
app.config['HOST_STATIC_FILES_ENDPOINT'] = app.config['HOST_ENDPOINT']
def setup_controller():
global controller
read_settings(app)
env = Environment(loader=PackageLoader('main', 'view/templates'))
controller = Corretor(app.config, env)
def setup_static_files_service(app):
if not app.config['APACHE_HOST']:
CORS(app)
@app.route("/<path:path>")
def send_static_files(path):
root_dir = os.path.abspath(os.path.dirname(__file__))
file_dir = os.path.join(root_dir, "view")
return send_from_directory(file_dir, path)
def run():
app.run(host=app.config['SERVER_HOST'], port=app.config['SERVER_PORT'])
setup_controller()
setup_static_files_service(app)
if __name__ == '__main__':
try:
run()
except:
pyutil.print_stack_trace()
raise