From 4dafb523e65e10648fcdfa1099eaa5f8e849038c Mon Sep 17 00:00:00 2001 From: Valeryi Savich Date: Fri, 14 Sep 2018 19:56:14 +0300 Subject: [PATCH 1/3] Added a development environment --- Dockerfile | 13 +++++++++++ docker-compose.dev.yml | 53 ++++++++++++++++++++++++++++++++++++++++++ requirements.dev.txt | 6 +++++ 3 files changed, 72 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.dev.yml create mode 100644 requirements.dev.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e8f7a8c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.6-slim-stretch +RUN apt-get update && apt-get -y install gcc + +COPY requirements.dev.txt /requirements.txt +RUN pip install -r requirements.txt + +COPY ./sanic_mongodb_ext /app/sanic_mongodb_ext +COPY ./LICENSE /app +COPY ./README.rst /app +COPY ./setup.py /app +WORKDIR /app + +RUN python setup.py develop diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..80b9583 --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,53 @@ +version: "2" + +networks: + app-tier: + driver: bridge + +services: + + app: + build: + context: . + dockerfile: ./Dockerfile + image: sanic-mongodb-extension + environment: + - MONGODB_USERNAME=user + - MONGODB_PASSWORD=password + - MONGODB_HOST=mongodb + - MONGODB_DATABASE=app + volumes: + - ./sanic_mongodb_ext:/app/sanic_mongodb_ext + ports: + - "8000:8000" + depends_on: + - mongodb + networks: + - app-tier + tty: true + + mongodb: + image: bitnami/mongodb:3.7 + ports: + - "27017:27017" + environment: + - MONGODB_USERNAME=user + - MONGODB_PASSWORD=password + - MONGODB_DATABASE=app + - MONGODB_ROOT_PASSWORD=root + networks: + - app-tier + + mongodb_ui: + image: adicom/admin-mongo:latest + ports: + - "1234:1234" + environment: + - CONN_NAME=mongodb_connection + - DB_USERNAME=root + - DB_PASSWORD=root + - DB_HOST=mongodb + depends_on: + - mongodb + networks: + - app-tier diff --git a/requirements.dev.txt b/requirements.dev.txt new file mode 100644 index 0000000..f9cdeed --- /dev/null +++ b/requirements.dev.txt @@ -0,0 +1,6 @@ +sanic==0.8.3 +sanic-base-extension==0.1.1 +Marshmallow==2.15.4 + +ipython==6.5.0 +pudb==2018.1 From 24336fb5526037262bdbf16a9791b821a1bcc6f9 Mon Sep 17 00:00:00 2001 From: Valeryi Savich Date: Fri, 14 Sep 2018 22:33:08 +0300 Subject: [PATCH 2/3] Updated README.rst --- README.rst | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index 23d06a3..5c48c30 100644 --- a/README.rst +++ b/README.rst @@ -16,30 +16,30 @@ This package should be installed using pip: :: Example ======= .. code-block:: python - + #!/usr/bin/env python3 from sanic import Sanic, response from sanic_mongodb_ext import MongoDbExtension - from umongo import Document, MotorAsyncIOInstance + from umongo import Instance, Document, MotorAsyncIOInstance from umongo.fields import StringField app = Sanic(__name__) # Configuration for MongoDB and uMongo app.config.update({ - "MONGODB_DATABASE": "example_database", + "MONGODB_DATABASE": "app", # Make ensure that the `app` is really exists "MONGODB_URI": "mongodb://user:password@mongodb:27017", "LAZY_UMONGO": MotorAsyncIOInstance(), }) - MongoDbExtension(app) # uMongo client is available as `app.mongodb` or `app.extensions['mongodb']` - instance = app.config["LAZY_UMONGO"] # For a structured applications the lazy client very useful - + # uMongo client is available as `app.mongodb` or `app.extensions['mongodb']`. + # The lazy client will be available as `app.lazy_mongodb` only when the database was specified, + # and which is a great choice for the structured projects. + MongoDbExtension(app) # Describe the model - @instance.register + @app.lazy_umongo.register class Artist(Document): name = StringField(required=True, allow_none=False) - # And use it later for APIs @app.route("/") async def handle(request): @@ -47,6 +47,9 @@ Example await artist.commit() return response.json(artist.dump()) + if __name__ == '__main__': + app.run(host='0.0.0.0', port=8000) + License ======= The sanic-mongodb-extension is published under BSD license. For more details read LICENSE_ file. @@ -55,3 +58,9 @@ The sanic-mongodb-extension is published under BSD license. For more details rea .. _uMongo: https://github.com/Scille/umongo .. _motor_asyncio: https://motor.readthedocs.io/en/stable/ .. _LICENSE: https://github.com/Relrin/sanic-mongodb-extension/blob/master/LICENSE + +Real project examples +===================== +- `Auth/Auth Microservice `_ +- `Game servers pool microservice `_ +- `Player statistics microservice `_ From 00428bbbade642abd49cf73decb31f8421fe95c3 Mon Sep 17 00:00:00 2001 From: Valeryi Savich Date: Mon, 1 Oct 2018 12:47:20 +0300 Subject: [PATCH 3/3] Fixed an issue with an access to lazy instance --- sanic_mongodb_ext/__init__.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sanic_mongodb_ext/__init__.py b/sanic_mongodb_ext/__init__.py index 0f6019f..e20dc7e 100644 --- a/sanic_mongodb_ext/__init__.py +++ b/sanic_mongodb_ext/__init__.py @@ -1,4 +1,4 @@ -__version__ = '0.2.0' +__version__ = '0.2.1' __all__ = ['MongoDbExtension', ] VERSION = __version__ @@ -10,10 +10,15 @@ class MongoDbExtension(BaseExtension): extension_name = app_attribute = 'mongodb' + lazy_app_attribute = 'lazy_umongo' def init_app(self, app, *args, **kwargs): super(MongoDbExtension, self).init_app(app, *args, **kwargs) + lazy_instance = app.config.get('LAZY_UMONGO', None) + if lazy_instance is not None: + setattr(app, self.lazy_app_attribute, lazy_instance) + @app.listener('before_server_start') async def mongodb_configure(app_inner, _loop): client = AsyncIOMotorClient(app_inner.config['MONGODB_URI']) @@ -23,10 +28,9 @@ async def mongodb_configure(app_inner, _loop): setattr(app, 'extensions', {}) app.extensions[self.extension_name] = client - database = app_inner.config['MONGODB_DATABASE'] - if database: + database = app_inner.config.get('MONGODB_DATABASE', None) + if lazy_instance and database: motor_database_client = client[database] - lazy_instance = app_inner.config['LAZY_UMONGO'] lazy_instance.init(motor_database_client) @app.listener('after_server_stop')