Moved API to separate App

This commit is contained in:
boyska 2013-12-10 03:04:52 +01:00
parent 40413b15b2
commit 3c707bb47f

View file

@ -60,46 +60,23 @@ class DateApp(Bottle):
'/custom?strftime=FORMAT : get now().strftime(FORMAT)' '/custom?strftime=FORMAT : get now().strftime(FORMAT)'
class RecServer: class RecAPI(Bottle):
def __init__(self): def __init__(self):
self._app = Bottle() Bottle.__init__(self)
self._route() self._route()
self.db = RecDB(get_config()['DB_URI']) self.db = RecDB(get_config()['DB_URI'])
def _route(self): def _route(self):
### This is the API part of the app self.post('/create', callback=self.create)
# TODO: move to namespace /api/ self.post('/delete', callback=self.delete)
# TODO: create a "sub-application" self.post('/update/<recid:int>', callback=self.update)
self._app.route('/api/help', callback=self.help) self.post('/generate', callback=self.generate)
self.get('/help', callback=self.help)
self._app.route('/api/create', method="POST", callback=self.create) self.get('/', callback=self.help)
self.get('/get/search', callback=self.search)
self._app.route('/api/update/<recid:int>', method="POST", self.get('/get/ongoing', callback=self.get_ongoing)
callback=self.update) self.get('/jobs', callback=self.running_jobs)
self._app.route('/api/generate', method="POST", callback=self.generate) self.get('/jobs/<job_id:int>', callback=self.check_job)
self._app.route('/api/search', callback=self.search)
self._app.route('/api/get/search', callback=self.search)
self._app.route('/api/get/ongoing', callback=self.get_ongoing)
self._app.route('/api/delete', method="POST", callback=self.delete)
self._app.route('/api/jobs', callback=self.running_jobs)
self._app.route('/api/jobs/<job_id:int>', callback=self.check_job)
## Static part of the site
self._app.route('/output/<filepath:path>',
callback=lambda filepath:
static_file(filepath,
root=get_config()['AUDIO_OUTPUT']))
self._app.route('/static/<filepath:path>',
callback=lambda filepath: static_file(filepath,
root='static/'))
self._app.route('/', callback=lambda: redirect('/new.html'))
self._app.route('/new.html',
callback=partial(static_file, 'new.html',
root='pages/'))
self._app.route('/tempo.html',
callback=partial(static_file, 'tempo.html',
root='pages/'))
def create(self): def create(self):
req = dict(request.POST.allitems()) req = dict(request.POST.allitems())
@ -246,6 +223,7 @@ class RecServer:
<h3>Not implemented.</h3>" <h3>Not implemented.</h3>"
# JSON UTILS # JSON UTILS
def rec_msg(self, msg, status=True, **kwargs): def rec_msg(self, msg, status=True, **kwargs):
d = {"message": msg, "status": status} d = {"message": msg, "status": status}
d.update(kwargs) d.update(kwargs)
@ -255,6 +233,35 @@ class RecServer:
return self.rec_msg(msg, status=False, **kwargs) return self.rec_msg(msg, status=False, **kwargs)
class RecServer:
def __init__(self):
self._app = Bottle()
self._route()
self.db = RecDB(get_config()['DB_URI'])
def _route(self):
### This is the API part of the app
# TODO: move to namespace /api/
# TODO: create a "sub-application"
## Static part of the site
self._app.route('/output/<filepath:path>',
callback=lambda filepath:
static_file(filepath,
root=get_config()['AUDIO_OUTPUT']))
self._app.route('/static/<filepath:path>',
callback=lambda filepath: static_file(filepath,
root='static/'))
self._app.route('/', callback=lambda: redirect('/new.html'))
self._app.route('/new.html',
callback=partial(static_file, 'new.html',
root='pages/'))
self._app.route('/tempo.html',
callback=partial(static_file, 'tempo.html',
root='pages/'))
if __name__ == "__main__": if __name__ == "__main__":
configs = ['default_config.py'] configs = ['default_config.py']
if 'TECHREC_CONFIG' in os.environ: if 'TECHREC_CONFIG' in os.environ:
@ -272,5 +279,6 @@ if __name__ == "__main__":
get_config().from_pyfile(conf) get_config().from_pyfile(conf)
c = RecServer() c = RecServer()
c._app.mount('/date', DateApp()) c._app.mount('/date', DateApp())
c._app.mount('/api', RecAPI())
c._app.run(host=get_config()['HOST'], port=get_config()['PORT'], c._app.run(host=get_config()['HOST'], port=get_config()['PORT'],
debug=get_config()['DEBUG']) debug=get_config()['DEBUG'])