rudemaps/server.py

78 lines
2.2 KiB
Python
Raw Normal View History

2017-02-26 11:42:14 +01:00
from bottle import route, run,template,app,default_app,install,request,response,post,get
from bottlejwt import JwtPlugin
2017-02-10 18:57:41 +01:00
import logging
import json
import os
2017-02-26 11:42:14 +01:00
import password_manager
2017-02-10 18:57:41 +01:00
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))
2017-02-08 21:35:33 +01:00
@route('/')
2017-02-08 22:46:49 +01:00
def root():
2017-02-10 18:57:41 +01:00
return template('map_template', item="prova bind")
2017-02-08 21:35:33 +01:00
2017-02-08 22:46:49 +01:00
@route('/<item>')
2017-02-08 22:26:41 +01:00
def myName(item):
2017-02-10 18:57:41 +01:00
return template('map_template', item=item)
2017-02-08 21:35:33 +01:00
2017-02-26 11:42:14 +01:00
@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']
2017-02-10 18:57:41 +01:00
load_config()
2017-02-26 11:48:53 +01:00
install(JwtPlugin(password_manager.login_validation,CONFIG['jwt']['secret'], algorithm='HS256'))
2017-02-26 11:42:14 +01:00
2017-02-08 21:35:33 +01:00
2017-02-10 18:57:41 +01:00
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()