GithubHelp home page GithubHelp logo

Comments (7)

FedericoCeratto avatar FedericoCeratto commented on June 20, 2024

See: http://api.mongodb.org/python/current/api/pymongo/mongo_client.html
"The host parameter can be a full mongodb URI, in addition to a simple hostname."
And: http://docs.mongodb.org/manual/reference/connection-string/

from bottle-cork.

oshihirii avatar oshihirii commented on June 20, 2024

Could I therefore use:

class MongoDBBackend(Backend):
    def __init__(self, db_name='cork', hostname='os.environ[\'OPENSHIFT_MONGODB_DB_URL\']', initialize=False):
    """Initialize MongoDB Backend"""
    connection = MongoClient(hostname)
    db = connection[db_name]

Two things I am confused about:

  • Is the syntax correct for hostname value?
  • Would each user therefore be accessing a connection where they were logged in with the MongoDB admin username and password (as that is all in the OPENSHIFT_MONGODB_DB_URL environment variable - see first post)? And if so is that a problem?

Sorry for asking so many questions, but I seem to be coming up against a few issues when implementing. Nevertheless, I hope they may be helpful for others implementing on OpenShift.

from bottle-cork.

FedericoCeratto avatar FedericoCeratto commented on June 20, 2024

No need to modify mongodb_backend.py - in your own code you can do:

dbname = 'mydbname'
hostname = os.environ['OPENSHIFT_MONGODB_DB_URL']
port = os.environ['OPENSHIFT_MONGODB_DB_PORT']
port = int(port)
connection = pymongo.MongoClient(hostname=hostname, port=port)
db = connection[dbname]

"connection" would be used by any user within the same application. It should have limited access rights, e.g. update the collection that belong to your webapp and nothing more.

from bottle-cork.

oshihirii avatar oshihirii commented on June 20, 2024

Thank you for the clarification, that makes sense to me.

from bottle-cork.

oshihirii avatar oshihirii commented on June 20, 2024

I ran my application from shell and got error:

  File "mybottleapp.py", line 47, in populate_mongodb_backend
    connection = pymongo.MongoClient(hostname=hostname)
NameError: global name 'hostname' is not defined

Looking at the class definition of MongoDBBackend in mongodb_backend.py, 'hostname' is defined as a parameter.

This is the code in my application:

def populate_mongodb_backend():
    mb = MongoDBBackend(db_name='cork-example', initialize=True,
            hostname = os.environ['OPENSHIFT_MONGODB_DB_URL'],
            connection = pymongo.MongoClient(hostname=hostname)
            )

Note the value of OPENSHIFT_MONGODB_DB_URL is:

mongodb://admin:[email protected]:27017/

Update:

I realised host is the MongoClient parameter name, not hostname, so I tried:

def populate_mongodb_backend():
    mb = MongoDBBackend(db_name='cork-example', initialize=True,
            hostname = os.environ['OPENSHIFT_MONGODB_DB_URL'],
            connection = pymongo.MongoClient(host=hostname)
            )

And get the same error:

    connection = pymongo.MongoClient(host=hostname)
NameError: global name 'hostname' is not defined

from bottle-cork.

FedericoCeratto avatar FedericoCeratto commented on June 20, 2024

That's because the "hostname" variable is not defined. Use:
hostname = os.environ['OPENSHIFT_MONGODB_DB_URL']

from bottle-cork.

oshihirii avatar oshihirii commented on June 20, 2024

working code:

def populate_mongodb_backend():
    hostname = os.environ['OPENSHIFT_MONGODB_DB_URL']
    connection = pymongo.MongoClient(host=hostname)
    mb = MongoDBBackend(db_name='cork-example', initialize=True,hostname = hostname)
    .... # the rest is the same

from bottle-cork.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.