def handle_get_markers(req, config) -> dict: """ Handle the get markers call Args: req (werkzeug.local.LocalProxy): Flask request config (models.Config): Config instance Returns: dict: Response """ key = req.args.get('key') # If the key is invalid just return INVALID_KEY if not isValidKey("private", key, config): msg.fail(f"Requested from {req.remote_addr}") return dict(FAIL="INVALID_KEY") _lat, _long = req.args.get('lat'), req.args.get('long') # Convert to ints try: recv_lat, recv_long = int(float(_lat)), int(float(_long)) except ValueError: return dict(FAIL="INVALID_DATA") msg.info(f"[REQ_GET_MKS] Received request from {req.remote_addr} for lat: {recv_lat} and long: {recv_long}") try: markers = db.return_markers(config, recv_lat, recv_long) return dict(data=markers) except Exception as e: msg.exception(e) return dict(FAIL="UNKNOWN_FAIL")
def __init__(self): if not self.__do_config_files_exist(): fatal_fail("One or more required files not found!") stop() # Load config with open(CONFIG_FILE_PATH) as conf_file: json_ = json.load(conf_file) self.LOGGING_FORMAT = json_["logging_format"] self.DATE_FORMAT = json_["date_format"] self.SERVE_PORT = json_["serve_port"] self.ALLOW_PUBLIC = json_["allow_public"] self.PUBLIC_UPDATE_DELAY = json_["public_update_delay"] info("Configs loaded") # Load keys with open(KEY_FILE_PATH) as key_file: json_ = json.load(key_file) private_keys = json_["private_keys"] public_keys = json_["public_keys"] for key in private_keys: self.PRIVATE_KEYS[key] = "valid" for key in public_keys: self.PUBLIC_KEYS[key] = "valid" self.DB_LOGIN_USER = json_["db_user"] self.DB_LOGIN_PASS = json_["db_pass"] info("Keys loaded")
def __do_config_files_exist(self) -> bool: """ Checks if the config and key files exist Returns: bool: True - they exist | False - they do not exist """ if not path.exists(CONFIG_FILE_PATH): info("Couldn't find config.json!") return False if not path.exists(KEY_FILE_PATH): info("Couldn't find keys.json!") return False return True
def handle_add_zone(req, config) -> dict: """ Handle the add zone call Args: req (werkzeug.local.LocalProxy): Flask request config (models.Config): Config instance Returns: dict: Response """ key = req.args.get('key') # If the key is invalid just return INVALID_KEY if not isValidKey("private", key, config): msg.fail(f"Requested from {req.remote_addr}") return dict(FAIL="INVALID_KEY") _type = req.args.get('type') _coords = req.args.get('coords') try: _type = int(_type) except ValueError: return dict(FAIL="INVALID_DATA") coords = str(_coords) # Check to see if it can find at least one set of delimiters if coords.find(',') == -1: return dict(FAIL="INVALID_DATA") if coords.find('@') == -1: return dict(FAIL="INVALID_DATA") msg.info(f"[REQ_ADD_ZNS] Received request from {req.remote_addr} with type: {_type} and coords: {coords}") try: response = db.add_zone(config, _type, coords) if response == 0: return dict(SUCCESS="DATA_ADDED") else: msg.fail(f"Recieved {response} from method add_zone") return dict(FAIL=str(response)) except Exception as e: msg.exception(e) return dict(FAIL="UNKNOWN_FAIL")
def handle_add_marker(req, config) -> dict: """ Handle the add marker call Args: req (werkzeug.local.LocalProxy): Flask request config (models.Config): Config instance Returns: dict: Response """ key = req.args.get('key') # If the key is invalid just return INVALID_KEY if not isValidKey("private", key, config): msg.fail(f"Requested from {req.remote_addr}") return dict(FAIL="INVALID_KEY") _lat, _long, _type = req.args.get('lat'), req.args.get('long'), req.args.get('type') # Convert to float try: recv_lat, recv_long = float(_lat), float(_long) except ValueError: return dict(FAIL="INVALID_DATA") # Check to see if the type is valid try: if TYPES[_type] == "valid": pass except KeyError: msg.debug(f"[RUNTIME_DEBUG] Type {_type} is NOT valid!") return dict(FAIL="INVALID_TYPE") msg.info(f"[REQ_ADD_MKS] Received request from {req.remote_addr} for lat: {recv_lat}, long: {recv_long} and type: {_type}") try: response = db.add_marker(config, recv_lat, recv_long, _type) if response == 0: return dict(SUCCESS="DATA_ADDED") else: msg.fail(f"Recieved {response} from method add_marker") return dict(FAIL=str(response)) except Exception as e: msg.exception(e) return dict(FAIL="UNKNOWN_FAIL")
def handle_del_zone(req, config) -> dict: """ Handle the delete zones call Args: req (werkzeug.local.LocalProxy): Flask request config (models.Config): Config instance Returns: dict: Response """ key = req.args.get('key') # If the key is invalid just return INVALID_KEY if not isValidKey("private", key, config): msg.fail(f"Requested from {req.remote_addr}") return dict(FAIL="INVALID_KEY") _coords = req.args.get('coords') coords = str(_coords) # Check to see if it can find at least one set of delimiters if coords.find(',') == -1: return dict(FAIL="INVALID_DATA") if coords.find('@') == -1: return dict(FAIL="INVALID_DATA") msg.info(f"[REQ_DEL_ZNS] Received DELETE request from {req.remote_addr} for coords: {coords}") try: response = db.del_markers(config, coords) if response == 0: return dict(SUCCESS="DATA_DELETED") else: msg.fail(f"Recieved {response} from method del_zone") return dict(FAIL=str(response)) except Exception as e: msg.exception(e) return dict(FAIL="UNKNOWN_FAIL")
def handle_del_markers(req, config) -> dict: """ Handle the delete markers call Args: req (werkzeug.local.LocalProxy): Flask request config (models.Config): Config instance Returns: dict: Response """ key = req.args.get('key') # If the key is invalid just return INVALID_KEY if not isValidKey("private", key, config): msg.fail(f"Requested from {req.remote_addr}") return dict(FAIL="INVALID_KEY") _lat, _long = req.args.get('lat'), req.args.get('long') # Convert to float try: recv_lat, recv_long = float(_lat), float(_long) except ValueError: return dict(FAIL="INVALID_DATA") msg.info(f"[REQ_DEL_MKS] Received DELETE request from {req.remote_addr} for lat: {recv_lat}, long: {recv_long}") try: response = db.del_markers(config, recv_lat, recv_long) if response == 0: return dict(SUCCESS="DATA_DELETED") else: msg.fail(f"Recieved {response} from method del_markers") return dict(FAIL=str(response)) except Exception as e: msg.exception(e) return dict(FAIL="UNKNOWN_FAIL")
def main(): msg.start(REV) msg.info("Initializing...") # Load configs config = Config() initialize_logging() msg.info("Checking for missing modules") if check_imports_.check_imports(): # Fatal -> One or more modules not found msg.fatal_fail("One or more modules not found") logging.critical("One or more modules not found -> exiting") stop.stop() else: msg.ok("All modules found") msg.ok("Successfully initialized, start serving:") msg.info("Ctrl-C to Stop Serving") api.start_serving(config) cleanup.cleanup()
def cleanup() -> None: """ Run cleanup """ msg.info("Cleaning up") stop.stop()