绑定app

def init_app(self, app):
        """绑定app
        """
        if app.config.GRIDFS_SETTINGS and isinstance(app.config.GRIDFS_SETTINGS, dict):
            self.GRIDFS_SETTINGS = app.config.GRIDFS_SETTINGS
            self.app = app

        else:
            raise ValueError(
                "nonstandard sanic config GRIDFS_URIS,GRIDFS_URIS must be a Dict[Bucket_name,Tuple[dburl,collection]]")

        @app.listener("before_server_start")
        async def init_mongo_connection(app, loop):
            for bucket_name, (dburl,collection) in app.config.GRIDFS_SETTINGS.items():
                if isinstance(dburl,str):
                    bucket = GridFSBucket(dburl,ioloop=loop,collection = collection).bucket
                else:
                    bucket = GridFSBucket(ioloop=loop,collection = collection,**dburl).bucket
                self.GridFSs[bucket_name] = bucket

        @app.listener("before_server_stop")
        async def sub_close(app, loop):
            log.info("mongo connection {numbr}".format(numbr=len(self.GridFSs)))
            for bucket_name,bucket in self.GridFSs.items():
                bucket.client.close
                log.info("{bucket_name} connection closed".format(bucket_name=bucket_name))


        if "extensions" not in app.__dir__():
            app.extensions = {}
        app.extensions['SanicGridFS'] = self

        app.GridFS = self.GridFSs
        return self