Skip to content

Instantly share code, notes, and snippets.

@suzumura-ss
suzumura-ss / nginx_upload2s3.lua
Last active December 22, 2015 16:47
nginx-lua: upload to aws-s3
local function signature(ngxctx, method, bucket, objectkey)
local access_key = os.getenv('AWS_ACCESS_KEY_ID')
local secret_key = os.getenv('AWS_SECRET_ACCESS_KEY')
local date = ngxctx.http_time(ngxctx.time())
local headers = ngxctx.req.get_headers()
local s2s = method .. "\n"
s2s = s2s .. (headers['content-md5'] or "") .. "\n"
s2s = s2s .. (headers['content-type'] or "") .. "\n"
s2s = s2s .. date .. "\n"
s2s = s2s .. "/" .. bucket .. "/" .. objectkey
@suzumura-ss
suzumura-ss / upload_body.lua
Last active December 22, 2015 05:17
nginx-lua: upload request-body to other service without LuaSocket
local function header_join_with_crlf(header)
local str = "", k, v
for k,v in pairs(header) do
str = str .. v .. "\r\n"
end
return str
end
-- @return client, err
local function connect(ngxctx, host, port, path)
@suzumura-ss
suzumura-ss / x_cloudwatchlog_filter.rb
Last active December 23, 2015 02:27
put CloudWatchLogs with X_CLOUDWATCH_LOG header
=begin
require 'grape'
require 'uuid'
require 'ltsv'
class Hello < Grape::API
post 'teapot' do
status 201
id = UUID.generate
header XCloudwatchLogFilter::X_CLOUDWATCH_LOG, LTSV.dump({user_id:id, resource:'/teapot'})
@suzumura-ss
suzumura-ss / error.log
Last active December 21, 2015 13:09
nginx-lua: upload request-body to other service with LuaSocket
2015/12/21 20:31:38 [error] 7922#0: *70 lua entry thread aborted: runtime error: attempt to yield across C-call boundary
stack traceback:
coroutine 0:
[C]: in function 'request'
/opt/nginx/scripts/upload4.lua:45: in function </opt/nginx/scripts/upload4.lua:1>, client: 127.0.0.1, server: localhost, request: "POST /upload4 HTTP/1.0", host: "localhost"
see:
http://stackoverflow.com/questions/30111808/luasocket-nginx-error-lua-entry-thread-aborted-runtime-error-attempt-to-yi
@suzumura-ss
suzumura-ss / upload_boby.lua
Last active January 8, 2016 14:36
upload request-body to other service
-- https://github.com/diegonehab/luasocket
local HTTP = require("socket.http")
local LTN12 = require("ltn12")
-- source for ngx.req.socket()
function source_tcpsock(handle, bytes)
bytes = tonumber(bytes)
if handle then
return function()
if bytes <= 0 then
@suzumura-ss
suzumura-ss / x_reproxy_url_filter.rb
Last active December 22, 2015 00:32
Rack middleware for process X-Reproxy-URL.
=begin
require 'grape'
require 'x_reproxy_url_filter'
class Hello < Grape::API
get 'static' do
status 200
%w{The quick brown fox jumps over the lazy dog}
end
get 'download' do
@suzumura-ss
suzumura-ss / fileUploader.ru
Last active December 11, 2015 07:16
fileUploader example with ruby-grape
#!/usr/bin/env rackup
# encoding: UTF-8
=begin
# start application
$ mkdir files
$ ./fileUploader.ru
# upload
$ curl localhost:9292/files -F comment="hello,world" -F "[email protected];type=image/jpg"
@suzumura-ss
suzumura-ss / getmyip.py
Created November 13, 2015 04:50
Get IP myself in AWS Lambda
import json
import commands
class MyIP:
def rawips(self):
cmd = "awk 'NR!=1 { split($2,arr,\":\"); print arr[1] }' /proc/net/tcp|sort|uniq"
return commands.getoutput(cmd).split("\n")
def raw2str(self, raw):
n = [int(raw[n:n+2],16) for n in [6,4,2,0]]
@suzumura-ss
suzumura-ss / peewee-example.py
Created November 12, 2015 08:45
peewee with BIGINT primary key.
#from peewee import *
#import pymysql
import peewee
# 'id' to 'BIGINT AUTO_INCREMENT'
peewee.MySQLDatabase.field_overrides['primary_key'] = 'BIGINT AUTO_INCREMENT'
db = peewee.MySQLDatabase(
database='peewee_development',
user='root',
password='',
@suzumura-ss
suzumura-ss / dylib_patch.rb
Created July 17, 2015 05:46
shared libraryの参照パスを @executable_path/ 直下に再帰的に更新
#!/usr/bin/env ruby
class ModifyPath
def initialize(exe)
@exe = exe
@dylibs = `otool -L #{exe}`.each_line.map{|line|
$1 if line=~%r{\t([^/@].+\.dylib) .*$}
}.compact.delete_if{|dy|
File.basename(dy)==File.basename(exe) or !File.exist?(File.basename(exe))
}