def add_json_config(): cwd = path.dirname(__file__) def load_configuration(file_name, base_name=None): """ Load json configuration file file_name(string): file name base_name(string): namespace for the configuration key in object if not set then take file_name without its extension Example: hosting.port => hosting is base_name """ base_name = base_name or file_name[:file_name.index(".")] with open(path.join(cwd, file_name)) as data_file: data = json.load(data_file) for key in data: configuration[base_name + "." + key] = data[key] # Load configuration for f in ["hosting.json", "mongo.json"]: load_configuration(f) # Load secret key for Google Authentication if env("FLASK_ENV") != "Production": for f in ["secret.json"]: load_configuration(f) else: keys = ["secret.google:clientId", "secret.google:clientSecret", "secret.google:host"] for k in keys: configuration[k] = env(k)
from os import path from flask import Flask import config from services.ulti import env # Load configration config.add_json_config() conf = config.configuration # Initialize app static = path.relpath(conf["hosting.static"]) app = Flask("BookStore", static_folder=static) app.secret_key = env("FLASK_SECRET") or "Development secret_key" app.config["environment"] = env("FLASK_ENV") or "Development" app.config["DEBUG"] = app.config["environment"] == "Development" app.config["host"] = env("HOST") or conf["hosting.host"] app.config["port"] = env("PORT") or conf["hosting.port"] # Configure routes and services config.configure_services("mongoService", app) config.configure_services("redisService", app) config.configure_routes(app) # Start server if __name__ == "__main__": debug = app.config["DEBUG"] host = app.config["host"] port = app.config["port"] app.run(debug=debug, port=int(port), host=host)