load_dotenv() # securty settings app.secret_key = os.getenv("FLASK_SECRET") # mail settings app.mail_server = os.getenv("MAIL_SERVER") app.mail_port = os.getenv("MAIL_PORT") app.mail_use_ssl = os.getenv("MAIL_USE_SSL") app.mail_username = os.getenv("MAIL_USERNAME") app.mail_password = os.getenv("MAIL_PASSWORD") # database settings app.db_host = os.getenv("DATABASE_HOST") app.db_user = os.getenv("DATABASE_USER") app.db_password = os.getenv("DATABASE_PASSWORD") app.db_port = os.getenv("DATABASE_PORT") app.db_name = os.getenv("DATABASE_NAME") app.db_uri = (f"postgresql://{app.db_user}:{app.db_password}@" f"{app.db_host}:{app.db_port}/{app.db_name}") app.db_track_modifications = os.getenv("SQLALCHEMY_TRACK_MODIFICATIONS") # Flask config app.config['SQLALCHEMY_DATABASE_URI'] = app.db_uri app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = app.db_track_modifications app.config['MAIL_SERVER'] = app.mail_server app.config['MAIL_PORT'] = app.mail_port app.config['MAIL_USE_SSL'] = app.mail_use_ssl app.config['MAIL_USERNAME'] = app.mail_username app.config['MAIL_PASSWORD'] = app.mail_password mail = Mail(app)
location['id'] = str(location['_id']) del location['_id'] return jsonify(address=address, nickname=data['nickname'], latitude=latitude, longitude=longitude, id=location['id'], order=data['order']) @app.route('/locations/<location_id>', methods=['PUT']) def update_location(location_id): data = request.json locations = get_collection() locations.update({'_id': ObjectId(location_id)}, {'$set': data}) return jsonify(message='OK') @app.route('/locations/<location_id>', methods=['DELETE']) def delete_location(location_id): locations = get_collection() locations.remove(ObjectId(location_id)) return jsonify(message='OK') def get_collection(): conn = pymongo.Connection('localhost:27017') return conn[app.db_name].locations if __name__ == '__main__': app.db_name = 'locations_prod' app.run(host='0.0.0.0')
def update_todo(todo_id): data = request.json todos = get_collection() todos.update({'_id': ObjectId(todo_id)}, {'$set': data}) return make_json_response({'message': 'OK'}) @app.route('/todos/<todo_id>', methods=['DELETE']) def delete_todo(todo_id): todos = get_collection() todos.remove(ObjectId(todo_id)) return make_json_response({'message': 'OK'}) def get_collection(): conn = pymongo.Connection('localhost:27017', **app.conn_args) return conn[app.db_name].todos if __name__ == '__main__': import optparse parser = optparse.OptionParser() parser.add_option('-r', '--replicaset', dest='replicaset', help='Define replicaset name to connect to.') options, args = parser.parse_args() if options.replicaset is not None: app.conn_args = {'replicaset': options.replicaset, 'slave_okay': True} else: app.conn_args = {} app.db_name = 'todos_prod' app.run(host='0.0.0.0')
pac_db = PAC_DB(session['user_id']) pac_db.create_db(BytesIO(binary_data), filename) # remove db_name if exist app.db_name.append(pac_db.db_name) if 'db_name' in session: remove_db(session['db_name']) app.db_name.remove(session['db_name']) session['db_name'] = pac_db.db_name return_dict = read_pac_from_db(pac_db.db_name) return json.dumps(return_dict) def cleanup(self): try: for db_name in self.db_name: remove_db(db_name) except (AttributeError, IOError): pass if __name__ == '__main__': app.secret_key = str(uuid.uuid4()) app.config['SESSION_TYPE'] = 'filesystem' app.db_name = [] try: app.run(debug=True, extra_files=extra_files) finally: cleanup(app)
@app.route('/todos/<todo_id>', methods=['DELETE']) def delete_todo(todo_id): todos = get_collection() todos.remove(ObjectId(todo_id)) return make_json_response({'message': 'OK'}) def get_collection(): conn = pymongo.Connection('localhost:27017', **app.conn_args) return conn[app.db_name].todos if __name__ == '__main__': import optparse parser = optparse.OptionParser() parser.add_option('-r', '--replicaset', dest='replicaset', help='Define replicaset name to connect to.') options, args = parser.parse_args() if options.replicaset is not None: app.conn_args = {'replicaset': options.replicaset, 'slave_okay': True} else: app.conn_args = {} app.db_name = 'todos_prod' app.run(host='0.0.0.0')
from functools import wraps from cryptography.fernet import Fernet app = Flask(__name__) CORS(app) app.coordination_config = configparser.ConfigParser() app.coordination_config.read("./coordination_config.ini") app.mdb_client = pymongo.MongoClient( app.coordination_config["Coordination"]["mongodb_connection_string"]) app.redis_client = redis.Redis( host=app.coordination_config["Redis_PubSub"]["hostname"], port=int(app.coordination_config["Redis_PubSub"]["port"]), password=app.coordination_config["Redis_PubSub"]["password"], db=int(app.coordination_config["Redis_PubSub"]["db"])) app.db_name = app.coordination_config["Coordination"]["mongodb_dbname"] app.apptraffic_service_name = app.coordination_config["Coordination"][ "apptraffic_service_name"] app.coordination = Coordination(db_client=app.mdb_client, db_name=app.db_name, redis_client=app.redis_client, service_name=app.apptraffic_service_name) # the way to generate a new key - import os; import base64; print(base64.b64encode(os.urandom(32))) # for details, see https://geekflare.com/securing-flask-api-with-jwt/ app.secret_key = base64.b64decode( app.coordination_config["WebAPI"]["web_session_secret"]) def token_required(f):