Skip to content

Instantly share code, notes, and snippets.

@pranavcode
Created March 30, 2019 18:46
Show Gist options
  • Save pranavcode/4301aff00d52c13a49fa8ba6dc893d2a to your computer and use it in GitHub Desktop.
Save pranavcode/4301aff00d52c13a49fa8ba6dc893d2a 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