Skip to content

Instantly share code, notes, and snippets.

View emilpetkov's full-sized avatar

Emil Petkov emilpetkov

View GitHub Profile
@mrk21
mrk21 / config-initializers-trusted_proxies.rb
Created August 31, 2019 10:07
How to get a remote ip on CloudFront + Rails
# @see https://docs.aws.amazon.com/ja_jp/AmazonCloudFront/latest/DeveloperGuide/LocationsOfEdgeServers.html
# @see https://morizyun.github.io/ruby/rails-controller-get-ip.html
# @see https://dev.classmethod.jp/cloud/aws/get-ec2-public-ip-range-by-powershell/
# @see https://github.com/rails/rails/blob/c81af6a/actionpack/lib/action_dispatch/middleware/remote_ip.rb#L112-L150
Rails.application.configure do
ip_ranges_res = Faraday.get('https://ip-ranges.amazonaws.com/ip-ranges.json')
ip_ranges = JSON.parse(ip_ranges_res.body)
cloudfront_ips = ip_ranges['prefixes'].select { |v| v['service'] == 'CLOUDFRONT' }.map { |v| IPAddr.new(v['ip_prefix']) } +
ip_ranges['ipv6_prefixes'].select { |v| v['service'] == 'CLOUDFRONT' }.map { |v| IPAddr.new(v['ipv6_prefix']) }
@scotteknight
scotteknight / environment.js
Last active July 15, 2019 16:33
Webpacker Config Files 7/10/19
const { environment } = require("@rails/webpacker");
const webpack = require("webpack");
// remove when fixed https://github.com/rails/webpacker/issues/2003
const merge = require("webpack-merge");
const sassLoader = environment.loaders.get("sass");
const cssLoader = environment.loaders.get("css");
@sclinede
sclinede / pipelines.rb
Last active September 24, 2023 11:10
Sagas implementation in pure Ruby (pre-DirtyPipelines gem)
class DirtyPipeline::Action
module WrappedCall
def call
Events.publish! Event.generate(self)
super
end
end
class << self
attr_accessor :attempted_event_klass, :pipeline, :timeout
@wrburgess
wrburgess / *rail_static_assets_on_heroku_cedar.md
Created December 10, 2016 21:14 — forked from bensheldon/*rail_static_assets_on_heroku_cedar.md
Serving Rails Static Assets on Heroku using a CDN with gzip and cache control

Optimizing Rails Static Assets on Heroku Cedar

Serving Rails static assets is hard, especially on Heroku Cedar where Heroku forces Rails to serve static assets itself (which isn't particularly performant nor worth your dyno-dollar)---this is different than Heroku Bamboo which used Varnish and is no more. To make the best of the situation, I highly recomend:

  1. Using the Heroku-Deflater gem which will selectively gzips assets (it gzips text-based files; and excludes images or binary files, which can actually be bigger when gzipped)

  2. Configure your production environment to set cache-control headers and get out of denial about how static assets are being served on Heroku Cedar

  3. Use AWS Cloudfront (or a CDN of your choosing) to serve the assets. Cloudfront is great because you can use the same Distribution

@ryanermita
ryanermita / rails_locking.md
Last active February 17, 2024 02:19
Optimistic and Pessimistic Locking in Rails

Optimistic Locking assumes that a database transaction conflict is very rare to happen. It uses a version number of the record to track the changes. It raise an error when other user tries to update the record while it is lock.

usage

Just add a lock_version column to the table you want to place the lock and Rails will automatically check this column before updating the record.

Pessimistic locking assumes that database transaction conflict is very likely to happen. It locks the record until the transaction is done. If the record is currently lock and the other user make a transaction, that second transaction will wait until the lock in first transaction is release.

usage

#!/bin/bash
##
#####################################################################################
# "THE BEER-WARE LICENSE" (Revision 42):
# <blagovest@petrovs.info> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return. Blagovest Petrov
#####################################################################################
##
#!/bin/bash
########################################################################
## Script for updating Let's encrypt certs
## http://petrovs.info/2015/12/27/My-way-to-auto-update-Lets-Encrypt/
## (C) 2015 Blagovest Petrov (blagovest <at> petrovs dot info)
## Released under the GPL
########################################################################
cd /etc/letsencrypt/live
@maxivak
maxivak / 00.md
Last active May 23, 2024 13:24
Sending emails with ActionMailer and Sidekiq

Sending emails with ActionMailer and Sidekiq

Send email asynchroniously using Sidekiq.

ActionMailer

Create your mailer us usual:

@jlongster
jlongster / immutable-libraries.md
Last active May 6, 2024 12:37
List of immutable libraries

A lot of people mentioned other immutable JS libraries after reading my post. I thought it would be good to make a list of available ones.

There are two types of immutable libraries: simple helpers for copying JavaScript objects, and actual persistent data structure implementations. My post generally analyzed the tradeoffs between both kinds of libraries and everything applies to the below libraries in either category.

Libraries are sorted by github popularity.

Persistent Data Structures w/structural sharing

@scottsb
scottsb / resetting-csync2-cluster.md
Last active October 25, 2021 20:28
Guide to Resetting a csync2 Cluster

Guide to Resetting a csync2 Cluster

Introduction

These are possible steps to reset a csync2 cluster that has been seriously fubared. This is an apocalyptic approach and should only be used when more surgical fixes (like correcting an individual conflict) aren't workable.

Use Cases