Skip to content

Instantly share code, notes, and snippets.

View taminhtien's full-sized avatar

Tạ Minh Tiên taminhtien

  • Ho Chi Minh City
View GitHub Profile
@taminhtien
taminhtien / db.rake
Created June 12, 2017 17:04 — forked from hopsoft/db.rake
Rails rake tasks for dump & restore of PostgreSQL databases
# lib/tasks/db.rake
namespace :db do
desc "Dumps the database to db/APP_NAME.dump"
task :dump => :environment do
cmd = nil
with_config do |app, host, db, user|
cmd = "pg_dump --host #{host} --username #{user} --verbose --clean --no-owner --no-acl --format=c #{db} > #{Rails.root}/db/#{app}.dump"
end
puts cmd
@taminhtien
taminhtien / gist:dcf73197f23581b3f7ad133220e87ddd
Last active July 5, 2017 07:42
Set up SSL/cert-bot/Let's encrypt in EC2 instance Amazon
```sudo -H pip install zope.interface -U```
```sudo -H pip install certbot -U```
```wget https://dl.eff.org/certbot-auto```
```chmod a+x certbot-auto```
```sudo ./certbot-auto certonly --standalone -d agilelab.sg www.agilelab.sg --debug```
Set up Nginx to serve SSL
```
upstream futureworkz {
@taminhtien
taminhtien / gist:32e6bb76b64483bda5628cbacbee1c12
Created June 2, 2017 03:19
Set up SSL/cert-bot/Let's encrypt in EC2 instance Amazon
```sudo -H pip install zope.interface -U```
```sudo -H pip install certbot -U```
```certbot-auto certonly --standalone -d agilelab.sg www.agilelab.sg --debug```
Set up Nginx to serve SSL
```
upstream futureworkz {
server unix:///home/futureworkz/production/shared/tmp/futureworkz.sock;
}
@taminhtien
taminhtien / rendering-json
Created April 25, 2017 04:52
Rendering JSON
Rendering JSON: Active Model Serializer Vs JBuilder
- AMS: seperate its own serialization concerns into seperate folders, files, OOP
- JBuilder: view extension, use along with html version (eg: show.html.slim and show.jbuilder)
So, if full api, use AMS, if have api and views, use jbuilder natively or ASM
ASM is OOP approach -> choose this 1 as personal
@taminhtien
taminhtien / rails-api-routes
Created April 25, 2017 04:37
Rails api routes
User `constraints` to specify the subdomain name for api portion and can introduce load balancing or increase number of api server needed based on its usage.
```ruby
Rails.application.routes.draw do
namespace :api, path: '/', constraints: { subdomain: 'api' } do
resources :users
end
end
```
@taminhtien
taminhtien / json-tricks
Created April 25, 2017 04:23
Playing with JSON
require 'net/http'
require 'json'
url = 'http://jsonplaceholder.typicode.com/posts/1'
uri = URI(url)
response = Net::HTTP.get(uri)
standard = JSON.parse(response) # keys are string
symbol = JSON.parse(response, symbolize_names: true) # keys are symbols
struct = JSON.parse(response, object_class: OpenStruct) # make resonse become an object, can use struct.id
@taminhtien
taminhtien / install-redis.sh
Created April 17, 2017 01:57 — forked from FUT/install-redis.sh
Install Redis on EC2
1. Install Linux updates, set time zones, followed by GCC and Make
sudo yum -y update
sudo ln -sf /usr/share/zoneinfo/America/Indianapolis \
/etc/localtime
sudo yum -y install gcc make
2. Download, Untar and Make Redis 2.8 (check here http://redis.io/download)

tmux cheatsheet

As configured in my dotfiles.

start new:

tmux

start new with session name:

for host in `aws ec2 describe-instances --region us-east-1 | grep PublicIpAddress | cut -d "\"" -f4` ; do ssh -i ~/.ssh/keypairs/dev-gamma-keypair.pem ec2-user@$host -t -o StrictHostKeyChecking=no 'sudo rpm -Uvh https://yum.newrelic.com/pub/newrelic/el5/x86_64/newrelic-repo-5-3.noarch.rpm ; sudo yum -y install newrelic-sysmond ; sudo nrsysmond-config --set license_key=dfc748af2b5d0a493659d3a443a7bc2fdbe06fa1 ; sudo /etc/init.d/newrelic-sysmond start' ; done
1. Create db
```shell
createuser --createdb --login -P <database_name>
--createdb tells Postgres that our user should be able to create databases
--login switch will allow our user to log in to the database
-P means we want to set our new user’s password right now
```