Skip to content

Instantly share code, notes, and snippets.

@amalrik
amalrik / nginxproxy.md
Created August 9, 2017 23:44 — forked from soheilhy/nginxproxy.md
How to proxy web apps using nginx?

Virtual Hosts on nginx (CSC309)

When hosting our web applications, we often have one public IP address (i.e., an IP address visible to the outside world) using which we want to host multiple web apps. For example, one may wants to host three different web apps respectively for example1.com, example2.com, and example1.com/images on the same machine using a single IP address.

How can we do that? Well, the good news is Internet browsers

@amalrik
amalrik / example.coffee
Created May 4, 2016 20:34
Example configuration for karma-coffee preprocessor.
// This file locate in directory 'test'
describe 'test suite', ->
it 'should pass', ->
expect(true).toEqual(true)
it 'should fail', ->
expect(true).toEqual(false)
@amalrik
amalrik / fallout.rb
Created December 3, 2015 16:49 — forked from tenderlove/fallout.rb
hack fallout terminals
##
# Program to help you hack terminals in Fallout
#
# Usage:
#
# Run the program once with a list of words in the terminal. The program will
# output a list, and the first word in the list is the word you should pick
# because it will eliminate the most possibilities.
#
# If that word is incorrect, then re-run the program with two lists, first the
# inspired by https://mriet.wordpress.com/2012/07/01/merge-bubbles-are-bad/
# and http://snipplr.com/view/26715/
# 1. first rebase all your local commits into one commit, e.g. for the last 3 commits into one you can use
git rebase -i HEAD~3
# and 'pick' the first, 'squash' the other commits
# 2. check the commit's sha id and save it somewhere

Installing a Gem on Heroku from a Private GitHub Repo

Sometimes you want to use a gem on Heroku that is in a private repository on GitHub.

Using git over http you can authenticate to GitHub using basic authentication. However, we don't want to embed usernames and passwords in Gemfiles. Instead, we can use authentication tokens.

  1. Get an OAuth Token from GitHub

First you will need to get an OAuth Token from GitHub using your own username and "note"

def generate_authentication_token
loop do
token = Devise.friendly_token
hashed_token = BCrypt::Password.create(token)
update_attribute(:token_id, Devise.friendly_token)
final_token = "#{token_id}-#{token}"
break { token: final_token, hashed_token: hashed_token } unless User.where(authentication_token: hashed_token).first
end
@amalrik
amalrik / gist:9765773
Last active August 29, 2015 13:57 — forked from RiANOl/gist:1077760
aes128/256
require "openssl"
require "digest"
def aes128_encrypt(key, data)
key = Digest::MD5.digest(key) if(key.kind_of?(String) && 16 != key.bytesize)
aes = OpenSSL::Cipher.new('AES-128-CBC')
aes.encrypt
aes.key = key
aes.update(data) + aes.final
end
class Avatar < ActiveRecord::Base
attr_accessor :content_type, :original_filename, :image_data
before_save :decode_base64_image
has_attached_file :image,
PAPERCLIP_CONFIG.merge(
:styles => {
:thumb => '32x32#',
:medium => '64x64#',

Exposing an API

APIs are becoming an essential feature of modern web applications. Rails does a good job of helping your application provide an API using the same MVC structure you're accustomed to.

In the Controller

Let's work with the following example controller:

class ArticlesController &lt; ApplicationController