Skip to content

Instantly share code, notes, and snippets.

@amitsaxena
amitsaxena / cloudflare_worker_to_proxy_requests.js
Last active Mar 10, 2021
Cloudflare worker to proxy requests (any HTTP method) to a different origin
View cloudflare_worker_to_proxy_requests.js
const base = "https://newsubdomain.example.com"
async function handleRequest(request) {
// Make a copy to allow mutation.
request = new Request(request);
// To override default caching mechanism and respect Cache-Control.
request.cf.cacheEverything = true;
// Extract path from request URL.
@amitsaxena
amitsaxena / clone_website.sh
Created Nov 6, 2019
How to clone a website and host it on a domain of your choice on AWS S3
View clone_website.sh
# Install wget and AWS CLI and set up your AWS credentials
# STEP 1
# Create a mirror of the website and re-write links
# --wait specifies that there is a wait of 1 second after each request.
# Be gentle with someone else's server. Also you risk being blocked if your request rate is too high
# Use -U to spoof user agent to that of an actual browser in case the server is blocking you from making requests
wget --mirror --convert-links --html-extension --wait=1 -o log https://aawaara.com/
@amitsaxena
amitsaxena / generate_certificate.sh
Created Jun 20, 2018
Generate a free Let’s Encrypt wildcard SSL certificate for your domain
View generate_certificate.sh
curl https://get.acme.sh | sh
# Open a new terminal window after executing above command
# Create a cloudflare account and get your API key from the profile section
export CF_Email=my.cloudflare@example.com
export CF_Key=replace_with_cloudflare_api_key
# Generate wildcard certificate for *.example.com
acme.sh --issue -d example.com -d '*.example.com' --dns dns_cf
@amitsaxena
amitsaxena / bootstrap.js
Created Sep 6, 2017
If you are using sails with waterline and struggling with timezones, try this first
View bootstrap.js
// Add the following to your config/bootstrap.js file
process.env.TZ = 'UTC';
@amitsaxena
amitsaxena / migrations.js
Last active Sep 2, 2017
Quickstart guide: Migrations with node.js
View migrations.js
// Install required modules as below:
npm install -g db-migrate
npm install -g db-migrate-mysql
// Create a database.json file:
{
"development": {
"driver": "mysql",
"host": "localhost",
@amitsaxena
amitsaxena / s3_bucket_policy.json
Last active Jun 20, 2016
How to return a 404 response code for CloudFront requests (S3 origin) of a non-existent S3 object: http://aawaara.com/post/146226425447/how-to-return-a-404-response-code-for-cloudfront
View s3_bucket_policy.json
{
"Version": "2008-10-17",
"Id": "PolicyForCloudFrontPrivateContent",
"Statement": [
{
"Sid": "1",
"Effect": "Allow",
"Principal": {
"AWS": "Enter your CloudFront Origin Access Identity here"
},
@amitsaxena
amitsaxena / active_admin_custom_filter.rb
Last active Oct 24, 2019
How to write an Active Admin custom filter (custom search)
View active_admin_custom_filter.rb
ActiveAdmin.register MyModel do
filter :custom_field, :as => :string, :label => "Custom Field Display Name"
end
class MyModel < ActiveRecord::Base
search_methods :custom_field_search
def self.custom_field_search(name)
# This method should return an ActiveRecord::Relation Object with MyModel objects
# Put in your complex search logic here
@amitsaxena
amitsaxena / gist:689d29f23d01bd563839
Last active Aug 29, 2015
Ruby text only spinner/loader/processing/progress indicators && some FUN
View gist:689d29f23d01bd563839
# Loader for infinite time
["|", "/", "-", "\\"].each{|v| STDOUT.write "\r#{v}"; sleep 0.5} while 1
# In case you want to run it for a few seconds (~ 20s in the example below)
10.times{ ["|", "/", "-", "\\"].each{|v| STDOUT.write "\r#{v}"; sleep 0.5} }
# Progress Bar
100.times{STDOUT.write "|"; sleep 0.1}
# Another kind of loading indicator
@amitsaxena
amitsaxena / gist:9cb0712e572c39a41edc
Last active Oct 24, 2019
CustomURL: Launch app if app is installed, else open an alternate URL (Android all browsers)
View gist:9cb0712e572c39a41edc
<script type="text/javascript">
var custom = "myapp://custom_url";
var alt = "http://mywebsite.com/alternate/content";
var g_intent = "intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end";
var timer;
var heartbeat;
var iframe_timer;
function clearTimers() {
clearTimeout(timer);
@amitsaxena
amitsaxena / get_s3_file_sizes_recursively.rb
Last active Aug 29, 2015
Get file size of all the files at a given path on Amazon S3 - recursively, and dump in a CSV
View get_s3_file_sizes_recursively.rb
# Using aws-sdk gem
@access_key = "access_key"
@secret_key = "secret_access_key"
@bucket = "my_bucket"
AWS.config(
:access_key_id => @access_key,
:secret_access_key => @secret_key
)