Skip to content

Instantly share code, notes, and snippets.

View ajaidanial's full-sized avatar
👨‍💻
High On Code

Ajai Danial ajaidanial

👨‍💻
High On Code
View GitHub Profile
@ajaidanial
ajaidanial / gist:5f23df331286520ec7ebd186ff00abe8
Created March 7, 2022 21:11 — forked from aaugustin/gist:2351479
Django: log all database queries in the console
import logging
logger = logging.getLogger('django.db.backends')
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
@ajaidanial
ajaidanial / middleware.py
Last active July 1, 2021 21:27
Converting session authentication to token authentication | Django
from django.conf import settings
from django.contrib.sessions.middleware import SessionMiddleware
from rest_framework.authentication import get_authorization_header
from rest_framework.reverse import reverse_lazy
class AppSessionToTokenAuthConversionMiddleware(SessionMiddleware):
"""
Apps SessionMiddleware to insert the session cookie id to the request body. This is used as a token
by the app FE to send requests. Also this takes the session cookie id from the Authorization header
@ajaidanial
ajaidanial / docker-start-celery-multi-worker-entrypoint
Created May 8, 2021 20:51 — forked from VKen/docker-start-celery-multi-worker-entrypoint
Running celery multi in docker container with running logs, signal trap, and graceful shutdown & restart
#!/bin/sh
# safety switch, exit script if there's error. Full command of shortcut `set -e`
set -o errexit
# safety switch, uninitialized variables will stop script. Full command of shortcut `set -u`
set -o nounset
# tear down function
teardown()
{
@ajaidanial
ajaidanial / convert-ssl.sh
Created April 19, 2021 10:06 — forked from csamsel/convert-ssl.sh
convert ssl certificates from pem to crt/key
openssl rsa -outform der -in privkey.pem -out privkey.key
openssl x509 -outform der -in fullchain.pem -out fullchain.crt
openssl x509 -outform der -in cert.pem -out cert.crt
@ajaidanial
ajaidanial / django-template-list
Created March 9, 2021 08:49 — forked from seanislegend/django-template-list
Create a list to use in a for-loop in Django templates.
{% with 'one two three four five six' as list %}
{% for item in list.split %}
{{ item }}<br>
{% endfor %}
{% endwith %}
@ajaidanial
ajaidanial / nginx-tuning.md
Created March 2, 2021 09:30 — forked from denji/nginx-tuning.md
NGINX tuning for best performance

Moved to git repository: https://github.com/denji/nginx-tuning

NGINX Tuning For Best Performance

For this configuration you can use web server you like, i decided, because i work mostly with it to use nginx.

Generally, properly configured nginx can handle up to 400K to 500K requests per second (clustered), most what i saw is 50K to 80K (non-clustered) requests per second and 30% CPU load, course, this was 2 x Intel Xeon with HyperThreading enabled, but it can work without problem on slower machines.

You must understand that this config is used in testing environment and not in production so you will need to find a way to implement most of those features best possible for your servers.

@ajaidanial
ajaidanial / postgres-backup.sh
Created December 17, 2020 20:35 — forked from 4410287/postgres-backup.sh
Shell script for daily postgres database backup with basic archiving.
#!/bin/bash
# Written 2018-11-15 by 4410287
# This script will create a backup file of a postgres database and compress it. It is capable of access a local or remote server to pull the backup. After creating a new backup, it will delete backups that are older than 15 days, with the exception of backups created the first of every month. It is recommended to create a seperate database user specifically for backup purposes, and to set the permissions of this script to prevent access to the login details. Backup scripts for different databases should be run in seperate folders or they will overwrite each other.
HOSTNAME=
USERNAME=
PASSWORD=
DATABASE=
@ajaidanial
ajaidanial / ssl-redirect.html
Created November 21, 2020 21:38 — forked from konklone/ssl-redirect.html
Force a quick redirect to HTTPS on Github Pages for your domain (and only your domain)
<script>
var host = "YOURDOMAIN.github.io";
if ((host == window.location.host) && (window.location.protocol != "https:"))
window.location.protocol = "https";
</script>
@ajaidanial
ajaidanial / middleware.py
Created October 5, 2020 08:30 — forked from rbtsolis/middleware.py
Django Access the Request or User Object Inside the Models and Signals
# 1) Create a middlewares/middlewares.py file and copy this:
import threading
class RequestMiddleware:
def __init__(self, get_response, thread_local=threading.local()):
self.get_response = get_response
self.thread_local = thread_local
# One-time configuration and initialization.
@ajaidanial
ajaidanial / mac-brew-python3-postgresql-psycopg2
Created September 29, 2020 23:02 — forked from iraquitan/mac-brew-python3-postgresql-psycopg2
PostgreSQL and psycopg2 initial setup on Mac with Homebrew and Python 3
# install homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# install postgresql
brew install postgresql
# start homebrew postgresql service
brew service start postgresql
# create a database with your username