Skip to content

Instantly share code, notes, and snippets.

@shaunlee
shaunlee / router.php
Created August 28, 2015 06:32
Phalcon Micro MVC Router
<?php
$filename = $_SERVER['REQUEST_URI'];
if ($i = strpos($filename, '?')) {
$filename = substr($filename, 0, $i);
}
if (file_exists(__DIR__ . $filename)) {
return false;
}
angular.module('App', []).config(['$interpolateProvider', function($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
}]);
@shaunlee
shaunlee / gist:dfbf5d805d4f5a3f9a54
Created October 26, 2015 04:50 — forked from jessedearing/gist:2351836
Create self-signed SSL certificate for Nginx
#!/bin/bash
echo "Generating an SSL private key to sign your certificate..."
openssl genrsa -des3 -out myssl.key 1024
echo "Generating a Certificate Signing Request..."
openssl req -new -key myssl.key -out myssl.csr
echo "Removing passphrase from key (for nginx)..."
cp myssl.key myssl.key.org
openssl rsa -in myssl.key.org -out myssl.key
@shaunlee
shaunlee / gist:6484676
Last active December 22, 2015 14:18
新老RPC协议TCP数据包大小对比

新老RPC协议TCP数据包大小对比

新协议

共计 682 字节(压缩后429),其中请求数据 149 字节(141)、返回数据 533 字节(288)

老协议

共计 1650 字节,其中请求数据 370 字节、返回数据 1280 字节(HTTP 头 136 字节 + 数据 1144 字节)

@shaunlee
shaunlee / pre-commit.sh
Last active March 2, 2016 10:20
Git hooks for phpcs
#!/bin/bash
#.git/hooks/pre-commit
EXEC=`php -r "echo 'php ', implode(DIRECTORY_SEPARATOR, [__DIR__, 'vendor', 'bin', 'phpcs']);"`
FILES=`git diff --cached --name-only | grep -i php$ | grep ^app`
ARGS='--standard=psr2 --encoding=utf8 -p'
for fn in $FILES; do
if [ ! -f $fn ]; then
DELETE=($fn)
@shaunlee
shaunlee / queue.go
Last active March 2, 2016 10:21
Recycle Queue
package main
import (
"fmt"
"time"
"sync/atomic"
)
const (
ITEM_DATA_SIZE = 4096
@shaunlee
shaunlee / clear_tube.php
Last active March 2, 2016 10:24
beanstalkd: clear tube
<?php
include 'pheanstalk/pheanstalk_init.php';
$ph = new Pheanstalk_Pheanstalk('127.0.0.1');
$ph->ignore('default')->watch($tube);
while ($job = $ph->reserve(0)) {
$ph->delete($job);
fwrite(STDOUT, $job->getId() . "\r");
@shaunlee
shaunlee / ringbuffer.go
Last active March 2, 2016 10:25
RingBuffer
package main
import (
"fmt"
"log"
"os"
"io"
"bufio"
"runtime"
"path/filepath"

通讯协议规范

新 RPC 服务器采用自定义文本协议,并满足以下条件:

  1. 可以在各种语言中简单的实现
  2. 能被快速解析
  3. 方便 Telnet 调试

相对于二进制协议,新 RPC 服务器的文本协议议具有较高的可读性,也容易被实现, 减少在客户端实现中出现BUG,并且解析性可以做到与二进制协议接近。

@shaunlee
shaunlee / phpcs4hg.sh
Last active March 2, 2016 10:27
/usr/local/bin/phpcs4hg
#!/bin/bash
while [ ! -d .hg ]; do
cd ..
[ -f proc/cpuinfo ] && break
done
[ ! -d .hg ] && echo 'Oops! No hg repository found.' && exit
for f in `hg st | awk '{print $2}'`; do