Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created June 17, 2020 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velotiotech/77a9a9414a1c9b7900649dd2d05fc76b to your computer and use it in GitHub Desktop.
Save velotiotech/77a9a9414a1c9b7900649dd2d05fc76b to your computer and use it in GitHub Desktop.
Velotio - HashiCorp Consul Part 2 - Django view to show the current MongoDB connection and setup
from django.shortcuts import render
from pymongo import MongoClient
def home(request):
client = MongoClient("mongo-primary.service.consul")
replica_set = client.admin.command('ismaster')
return render(request, 'home.html', {
'mongo_hosts': replica_set['hosts'],
'mongo_primary_host': replica_set['primary'],
'mongo_connected_host': replica_set['me'],
'mongo_is_primary': replica_set['ismaster'],
'mongo_is_secondary': replica_set['secondary'],
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment