2015-08-14 10:12:10 +00:00
|
|
|
from flask import Flask
|
|
|
|
from flask import Response
|
|
|
|
from flask import render_template
|
2017-10-26 08:17:17 +00:00
|
|
|
from flask import request
|
2015-08-14 10:12:10 +00:00
|
|
|
from botanjs.service.jclassresv import BotanClassResolver as JCResv
|
|
|
|
from botanjs.service.jwork import app as CeleryApp, JWork
|
2019-01-19 09:13:36 +00:00
|
|
|
from botanjs.config import Config
|
2016-05-27 16:20:58 +00:00
|
|
|
|
2015-08-14 10:12:10 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
class WebAPI:
|
|
|
|
|
|
|
|
app = None
|
|
|
|
|
|
|
|
BRoot = None
|
|
|
|
BMap = None
|
|
|
|
BCache = None
|
|
|
|
|
|
|
|
def __init__( self, jsRoot = "../src", jsCache = "/tmp", brokerURL = None ):
|
|
|
|
|
|
|
|
self.BRoot = os.path.abspath( jsRoot )
|
|
|
|
self.BCache = os.path.join( jsCache, "botanjs" )
|
|
|
|
self.BMap = os.path.join( self.BCache, "bmap.xml" )
|
|
|
|
|
|
|
|
if brokerURL != None:
|
2021-09-05 15:16:37 +00:00
|
|
|
CeleryApp.conf.update( broker_url = brokerURL )
|
2015-08-14 10:12:10 +00:00
|
|
|
|
2021-09-05 15:16:37 +00:00
|
|
|
self.app = Flask( __name__, static_url_path = "/cache/botanjs", static_folder = self.BCache )
|
2015-08-14 10:12:10 +00:00
|
|
|
self.app.jinja_env.add_extension( "compressinja.html.HtmlCompressor" )
|
|
|
|
|
2016-05-27 16:20:58 +00:00
|
|
|
self.app.add_url_rule( "/" , view_func = self.index )
|
2017-10-26 08:17:17 +00:00
|
|
|
self.app.add_url_rule( "/<mode>/" , view_func = lambda mode: self.api_request( mode, "zpayload" ) )
|
|
|
|
self.app.add_url_rule( "/<mode>/<path:code>" , view_func = self.api_request )
|
2015-08-14 10:12:10 +00:00
|
|
|
|
2019-01-19 09:13:36 +00:00
|
|
|
def run( self, *args, **kwargs ):
|
2021-09-05 15:16:37 +00:00
|
|
|
JWork.buildClassMap( self.BRoot, self.BMap )
|
2019-01-19 09:13:36 +00:00
|
|
|
return self.app.run( *args, **kwargs )
|
2015-08-14 10:12:10 +00:00
|
|
|
|
2016-05-27 16:20:58 +00:00
|
|
|
def index( self ):
|
|
|
|
return "Hello, this is the BotanJS Service API.", 200
|
2015-08-14 10:12:10 +00:00
|
|
|
|
|
|
|
def api_request( self, mode, code ):
|
2017-10-26 08:17:17 +00:00
|
|
|
|
2021-09-05 15:16:37 +00:00
|
|
|
if mode == "rebuild":
|
|
|
|
JWork.buildClassMap.delay( self.BRoot, self.BMap )
|
|
|
|
return "OK", 200
|
|
|
|
|
2017-10-26 08:17:17 +00:00
|
|
|
if code == "zpayload":
|
|
|
|
code = request.args.get( "p" )
|
|
|
|
|
2015-08-14 10:12:10 +00:00
|
|
|
try:
|
|
|
|
t = mode[1:]
|
|
|
|
if t == "js":
|
|
|
|
t = "application/javascript"
|
|
|
|
elif t == "css":
|
|
|
|
t = "text/css"
|
|
|
|
|
|
|
|
srvHandler = JCResv( JWork, self.BRoot, self.BMap, self.BCache )
|
|
|
|
return Response( srvHandler.getAPI( code, mode = mode ), mimetype = t )
|
2016-05-27 16:20:58 +00:00
|
|
|
except Exception as e:
|
2021-09-05 15:16:37 +00:00
|
|
|
if self.app.config[ "DEBUG" ]:
|
|
|
|
raise
|
2015-08-14 10:12:10 +00:00
|
|
|
return str(e), 404
|
2016-05-27 16:20:58 +00:00
|
|
|
|
2015-08-14 10:12:10 +00:00
|
|
|
|
|
|
|
return "Invalid request", 404
|