Commit 655680fe authored by Jogi Hofmüller's avatar Jogi Hofmüller

Modify API and implement login and list

parent 46c2d4be
from tornado import web, ioloop, httpserver
from time import strftime
import tornado.web
import tornado.ioloop
import tornado.httpserver
from time import strftime, time
import json
from libgg3 import Gdb
import os
......@@ -62,101 +64,122 @@ class FileHandler(tornado.web.RequestHandler):
path = 'index.html'
location = os.path.join(basedir, path)
if not os.path.isfile(location):
raise web.HTTPError(status_code=404)
raise tornado.web.HTTPError(status_code=404)
content_type, _ = guess_type(location)
self.add_header('Content-Type', content_type)
with open(location) as source_file:
self.finish(source_file.read())
class RestHandler(web.RequestHandler):
class RestHandler(tornado.web.RequestHandler):
def set_default_headers(self):
# while testing leave this open to anyone
self.set_header('Access-Control-Allow-Origin', '*')
@web.asynchronous
@tornado.web.asynchronous
def post(self, *args):
pass
class GeruechtHandler(RestHandler):
@web.asynchronous
@tornado.web.asynchronous
def get(self, *args):
geruecht = gdb.getNextGeruecht()
if not geruecht:
data = {'status': 'error', 'message': 'GeruechtHandler not implemented yet'}
data = {'status': 'error'}
logmsg('GeruechtHandler: error')
else:
data = {'status': 'success', 'geruecht': geruecht}
logmsg('GeruechtHandler: success')
data = {'status': 'success', 'payload': geruecht}
logmsg('GeruechtHandler: served geruecht')
data = json.dumps(data)
self.finish(data)
class LoginHandler(RestHandler):
@web.asynchronous
def post(self, *args):
logmsg('LoginHandler')
data = {'status': 'error', 'message': 'LoginHandler not implemented yet'}
data = json.dumps(data)
self.finish(data)
@tornado.web.asynchronous
def post(self, name, password):
user = sessionlist.authenticate(name, password)
if not user:
logmsg('LoginHandler: authentication error for user={}, password={}'.format(name, password))
self.finish(json.dumps({'status': 'error'}))
else:
logmsg('LoginHandler: user={} logged in'.format(user.name))
self.set_secure_cookie('name', user.name)
self.finish(json.dumps({'status': 'success'}))
class ListHandler(RestHandler):
@web.asynchronous
def get(self, *args):
logmsg('ListHandler')
data = {'status': 'error', 'message': 'ListHandler not implemented yet'}
data = json.dumps(data)
self.finish(data)
@tornado.web.asynchronous
def get(self, listobject):
data = {'status': 'error',}
user = sessionlist.validate(self.get_secure_cookie('name'))
if not user:
self.finish(json.dumps(data))
logmsg('ListHandler: unauthorized access')
return
class SaveHandler(RestHandler):
@web.asynchronous
def post(self, *args):
logmsg('SaveHandler')
data = {'status': 'error', 'message': 'SaveHandler not implemented yet'}
data = json.dumps(data)
self.finish(data)
payload = gdb.getdict(listobject)
if not payload:
self.finish(json.dumps(data))
logmsg('ListHandler: unknown getlist type {}'.format(listobject))
return
class EditHandler(RestHandler):
@web.asynchronous
def get(self, *args):
logmsg('EditHandler')
data = {'status': 'error', 'message': 'EditHandler not implemented yet'}
data = json.dumps(data)
self.finish(data)
data['status'] = 'success'
data['payload'] = payload
logmsg('ListHandler: list type={}, user={}'.format(listobject, user.name))
self.finish(json.dumps(data))
class SaveHandler(RestHandler):
@tornado.web.asynchronous
def post(self, saveobject, data):
user = sessionlist.validate(self.get_secure_cookie('name'))
if not user:
self.finish(json.dumps({'status': 'error'}))
logmsg('SaveHandler: unauthorized access')
return
logmsg('SaveHandler: not implemented yet')
self.finish(json.dumps({
'status': 'error',
'message': 'Sorry {}. SaveHandler not implemented yet'.format(user.name)
}))
def run():
if len (gdb.glist) == 0:
print('There are 0 Geruechte. Exiting.')
logmsg('error: there are 0 geruechte. Exiting.')
sys.exit(1)
if len(gdb.glist.nlist) == 0:
print('There are 0 Names. Exiting.')
logmsg('error: there are 0 names. Exiting.')
sys.exit(2)
app = web.Application([
web.url(r'/(.*)', FileHandler),
web.url(r'/get', GeruechtHandler),
web.url(r'/login/user/([.0-9a-zA-Z@]+)/password/([0-9a-zA-Z]+)', LoginHandler),
web.url(r'/edit/geruecht/id/([0-9]+)', EditHandler),
web.url(r'/edit/name/id/([0-9]+)', EditHandler),
web.url(r'/list/geruecht', ListHandler),
web.url(r'/list/name', ListHandler),
web.url(r'/save/geruecht/id/([0-9]+)', SaveHandler),
web.url(r'/save/name/id/([0-9]+)', SaveHandler),
])
server = httpserver.HTTPServer(app,
if len(gdb.ulist) == 0:
logmsg('warning: there are 0 users')
handlers = [
tornado.web.url(r'/get', GeruechtHandler),
tornado.web.url(r'/login/user/([A-Za-z0-9]+)/password/([A-Za-z0-9]+)', LoginHandler),
tornado.web.url(r'/listobject/([a-z]+)', ListHandler),
tornado.web.url(r'/saveobject/([a-z]+)/data/(.*)', SaveHandler),
tornado.web.url(r'/(.*)', FileHandler),
]
settings = dict(
cookie_secret = '946e0f11a8997bf41dbafca1f6f5a4bedf46746c91801ca4f2e90dd0172f06b6',
login_url = '/login/user/anon/password/none',
)
app = tornado.web.Application(handlers, **settings)
server = tornado.httpserver.HTTPServer(app,
ssl_options = {
'certfile': '/app/ssl/fullchain.pem',
'keyfile': '/app/ssl/privkey.pem',
'certfile': '/app/data/ssl/fullchain.pem',
'keyfile': '/app/data/ssl/privkey.pem',
})
try:
server.listen(8888)
logmsg('App started')
ioloop.IOLoop.instance().start()
server.listen(443)
logmsg('App ready')
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
logmsg('App finished')
gdb.dumpJSON()
if __name__ == '__main__':
pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment