Esempio n. 1
0
def start(args):
	"""
	Start the app

	:param args: input parameters
	:type args: Namespace

	"""
	# --------------------------------------------------------------------------
	# Set Database connection
	# --------------------------------------------------------------------------
	if args.DB_TYPE == "file":
		# File oriented DB
		if not args.FILE_DB_PATH:
			_path = os.path.join(os.getcwd(), "service_db")
		else:
			_path = os.path.join(args.FILE_DB_PATH, "service_db")

		backend = FileBackend(_path, {'serializer_class': 'pickle'})
		backend.create_index(Service, 'name', ephemeral=False, fields=["name"])
	else:
		import socket
		from pymongo import MongoClient, DESCENDING

		if not args.MONGODB_HOST:
			raise ValueError("You must specify a MongoDB host")

		_mongo_password = "" if not args.MONGODB_PASSWORD else args.MONGODB_PASSWORD
		_mongo_user = "" if not args.MONGODB_USER else args.MONGODB_USER
		_mongo_port = 27018 if not args.MONGODB_PORT else args.MONGODB_PORT

		_mongo_uri = 'mongodb://%(user)s%(sep)s%(password)s%(arr)s%(host)s' % dict(
			user=_mongo_user,
			sep=":" if _mongo_user else "",
			arr="@" if _mongo_user else "",
			password=_mongo_password,
			host=args.MONGODB_HOST
		)

		# PyMongo doesn't check socket timeout -> test manually
		try:
			sock = socket.socket()
			sock.connect((args.MONGODB_HOST, _mongo_port))
		except socket.error:
			raise ConnectionError("Can't connect to MongoDB host")

		# MongoDB
		c = MongoClient(_mongo_uri, port=_mongo_port, connectTimeoutMS=5000)

		# Get database and scheme
		db = c["pyregister" if not args.MONGODB_DB else args.MONGODB_DB]
		col = db["services" if not args.MONGODB_SCHEME else args.MONGODB_SCHEME]

		# Create index
		col.create_index([("name", DESCENDING)])

		# create a new BlitzDB backend using a MongoDB database
		backend = MongoBackend(col)

	#
	# Link backend to web-server property
	#
	backend.autocommit = True

	# --------------------------------------------------------------------------
	# Routes
	# --------------------------------------------------------------------------
	# Catalog
	routes_catalog(app)

	app.config['APP_DB'] = backend

	# --------------------------------------------------------------------------
	# Enable doc?
	# --------------------------------------------------------------------------
	if args.NOD_DOC is False:
		Swagger(app)

	app.run(host=args.IP,
	        port=args.PORT)