def receiver_kafka(tenant, message): message = json.loads(message) try: event = message.get("event") if event == "create" or event == "update": message['username'] = message['data']['id'] uc.createOrEditUser(message) elif event == "remove": device_id = message['data']['id'] uc.deleteUser(device_id) except Exception as e: print(e)
def createOrEditUser(): if request.mimetype != 'application/json': return formatResponse(400, 'invalid mimetype') try: userInfoJson = json.loads(request.data) except ValueError: return formatResponse(400, 'malformed JSON') try: uc.createOrEditUser(userInfoJson) except RequestError as err: return formatResponse(err.errorCode, err.message) return formatResponse(200)
def deleteUser(username): # default values deleteAfter = False reason = 'UNSPECIFIED' # URL param if len(request.args) > 0: if 'reason' in request.args: reason = request.args['reason'] elif 'delete' in request.args: deleteAfter = request.args['delete'] in ['True', 'true'] try: uc.deleteUser(username, reason, deleteAfter) except RequestError as err: return formatResponse(err.errorCode, err.message) return formatResponse(200)
def start_service(): persistence = Persistence() cherrypy.tree.mount(root=UserController(persistence), script_name="/user") cherrypy.tree.mount(root=RewardUserController(persistence), script_name="/reward") root_conf = {'/': {'tools.staticdir.on': True, 'tools.staticdir.dir': '%s/static' % home_path(), 'tools.staticdir.index': 'lottery.html'}} cherrypy.quickstart(None, "/", config=root_conf)
def main(): controller = UserController() controller.start()
def run(self): while True: LOGGER.debug("waiting for new messages") # To consume latest messages and auto-commit offsets while True: try: consumer = (kafka. KafkaConsumer( get_topic(conf.service, conf.subject), group_id='ejbca', bootstrap_servers=[conf.kafkaHost] ) ) break except kafka.errors.NoBrokersAvailable: LOGGER.error('Could not connect to Kafka at %s.' ' Chances are the server is not ready yet.' ' Will retry in 30sec' % conf.kafkaHost) sleep(30) for message in consumer: try: requestData = json.loads(message.value) except ValueError: LOGGER.error('Could not decode message as JSON. ' + dumpKafkaMessage(message)) continue if 'event' not in requestData.keys(): LOGGER.error('Event not specified. ' + dumpKafkaMessage(message)) continue if requestData['event'] in ['create', 'update']: try: if 'data' not in requestData.keys(): LOGGER.error("data segment not found. " + dumpKafkaMessage(message)) continue if 'id' not in requestData['data'].keys(): LOGGER.error("device id not specified. " + dumpKafkaMessage(message)) continue requestData['username'] = requestData['data']['id'] uc.createOrEditUser(requestData) LOGGER.info('user %s created' % requestData['username']) except RequestError as err: LOGGER.error(err.message + " " + dumpKafkaMessage(message)) elif requestData['event'] == 'delete': try: if 'data' not in requestData.keys(): LOGGER.error("data segment not found. " + dumpKafkaMessage(message)) continue if 'id' not in requestData['data'].keys(): LOGGER.error("device id not specified. " + dumpKafkaMessage(message)) continue uc.deleteUser(requestData['data']['id']) LOGGER.info("Device %s revocated" % requestData['data']['id']) except RequestError as err: LOGGER.error(err.message + " " + dumpKafkaMessage(message)) else: LOGGER.error("'event' " + requestData['event'] + " not implemented" + dumpKafkaMessage(message))
import flask; import controller.DefaultController as DefaultController; import controller.UserController as UserController; app = flask.Flask(__name__); # init controllers defaultController = DefaultController.DefaultController(); userController = UserController.UserController(); # config request mapper @app.route("/",methods=['GET', 'POST']) def index(): return defaultController.index(); @app.route("/login",methods=['GET', 'POST']) def login(): return userController.login(flask.request); @app.route("/users",methods=['GET', 'POST']) def users(): return userController.users(); if __name__ == "__main__": app.run(host="0.0.0.0",port=8080,debug=True);