rudemaps/server.py
2017-02-26 11:48:53 +01:00

77 lines
2.2 KiB
Python

from bottle import route, run,template,app,default_app,install,request,response,post,get
from bottlejwt import JwtPlugin
import logging
import json
import os
import password_manager
logging.basicConfig(level=logging.DEBUG,)
log=logging.getLogger("rudemaps")
CONFIG={}
def load_config(config_file="config.json"):
global CONFIG
log.debug("loading configuration file")
with open(config_file, "r") as f:
try:
CONFIG = json.load(f)
log.debug("CONFIG:\n %s" % str(CONFIG))
except Exception, e:
log.warning("Problem loading config file %s :\n%s" % (config_file, e))
@route('/')
def root():
return template('map_template', item="prova bind")
@route('/<item>')
def myName(item):
return template('map_template', item=item)
@post("/login")
def login():
login_data=request.json
response['Access-Control-Allow-Origin'] = '*'
response.content_type="application/json"
print "Loggin in: %s"%str(login_data)
token=JwtPlugin.encode(login_data)
return {'status':'OK','token':token}
@get("/secure", auth="SECURE_TEST")
def secure(auth):
"""Secured URL to check if auth is working"""
response['Access-Control-Allow-Origin'] = '*'
response.content_type = "application/json"
return {"status":"OK","content":"secured content succesfully accessed","auth":auth}
@route('/<:re:.*>', method='OPTIONS')
def enableCORSGenericRoute():
"""Generic regex route to catch cors call from browser
this should be really configured to allow the right domain
"""
#for h in request.headers:
#print "%s => %s"%(h,request.headers[h])
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = "POST, GET, OPTIONS"
if (request.headers.get('Access-Control-Request-Headers')):
response.headers['Access-Control-Allow-Headers'] = request.headers['Access-Control-Request-Headers']
load_config()
install(JwtPlugin(password_manager.login_validation,CONFIG['jwt']['secret'], algorithm='HS256'))
if __name__=='__main__':
#DEVEL SERVER
from bottle import debug
debug(True)
run(host='localhost', port=9093,reloader=True)
else:
#WSGI
os.chdir(os.path.dirname(__file__))
application = default_app()