Skip to content

Instantly share code, notes, and snippets.

View pranavcode's full-sized avatar

Pranav Kulkarni pranavcode

View GitHub Profile
@pranavcode
pranavcode / settings.py
Created March 30, 2019 17:51
Velotio - HashiCorp Consul Part 2 - Django configuration settings for connecting local MongoDB instance using Djongo
...
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'db',
}
}
...
@pranavcode
pranavcode / settings.py
Created March 30, 2019 17:53
Velotio - HashiCorp Consul Part 2 - Django configuration settings for connecting MongoDB instance on different host using Djongo
...
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'db',
'HOST': 'mongodb://mongo-primary.service.consul',
'PORT': 27017,
}
}
@pranavcode
pranavcode / models.py
Created March 30, 2019 18:30
Velotio - HashiCorp Consul Part 2 - Django models for our example application
from djongo import models
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
class Meta:
abstract = True
class Entry(models.Model):
@pranavcode
pranavcode / admin.py
Created March 30, 2019 18:44
Velotio - HashiCorp Consul Part 2 - Register Django models on Django Admin
from django.contrib import admin
from.models import Entry
admin.site.register(Entry)
@pranavcode
pranavcode / views.py
Created March 30, 2019 18:46
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'],
@pranavcode
pranavcode / urls.py
Created March 30, 2019 18:49
Velotio - HashiCorp Consul Part 2 - Django URLs or routes for the example app
from django.urls import path
from tweetapp import views
urlpatterns = [
path('', views.home, name='home'),
]
@pranavcode
pranavcode / urls.py
Created March 30, 2019 18:50
Velotio - HashiCorp Consul Part 2 - Django URLs or routes on the project level for our example application
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('web', include('tweetapp.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
@pranavcode
pranavcode / home.html
Created March 30, 2019 18:53
Velotio - HashiCorp Consul Part 2 - Django template for the view that shows MongoDB connection and setup
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Armata" rel="stylesheet">
<title>Django-Mongo-Consul</title>
@pranavcode
pranavcode / django-migrate-db.sh
Created March 30, 2019 19:33
Velotio - HashiCorp Consul Part 2 - Django migrate database
python ./manage.py migrate
@pranavcode
pranavcode / django-collect-static.sh
Created March 30, 2019 19:34
Velotio - HashiCorp Consul Part 2 - Django collect static assets of the project
python ./manage.py collectstatic --noinput