Skip to content

Commit

Permalink
Merge pull request #2 from Relrin/hotfix-example
Browse files Browse the repository at this point in the history
Fix of the example in README
  • Loading branch information
Relrin authored Oct 1, 2018
2 parents 48d681a + 00428bb commit 1e1c6b3
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 12 deletions.
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
25 changes: 17 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,40 @@ 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):
artist = Artist(name="A new rockstar!")
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.
Expand All @@ -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 <https://github.com/OpenMatchmaking/microservice-auth/>`_
- `Game servers pool microservice <https://github.com/OpenMatchmaking/microservice-game-servers-pool/>`_
- `Player statistics microservice <https://github.com/OpenMatchmaking/microservice-player-statistics/>`_
53 changes: 53 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -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
12 changes: 8 additions & 4 deletions sanic_mongodb_ext/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.2.0'
__version__ = '0.2.1'
__all__ = ['MongoDbExtension', ]

VERSION = __version__
Expand All @@ -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'])
Expand All @@ -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')
Expand Down

0 comments on commit 1e1c6b3

Please sign in to comment.