Skip to content

Instantly share code, notes, and snippets.

View u2's full-sized avatar
🐢
I may be slow to respond.

zhangyaning u2

🐢
I may be slow to respond.
View GitHub Profile
@u2
u2 / nagios
Created March 6, 2014 10:42
nagios3 + nginx
server {
listen 80;
server_name xxxxxxxxxxx;
access_log /var/log/nginx/nagios.access.log;
error_log /var/log/nginx/nagios.error.log info;
expires 31d;
root /usr/share/nagios3/htdocs;
//= require jquery
//= require jquery_ujs
$(function() {
var source = new EventSource('/stream');
source.addEventListener('counter', function(e) {
$('body').after(e.data + '<br />');
});
});
-- (p - 1) / (t + 2)^1.5
CREATE FUNCTION SP_POINTS(P SMALLINT(5), CREATED TIMESTAMP)
RETURNS TINYINT(3)
RETURN (P - 1) / POW(TIMESTAMPDIFF(HOUR, CREATED, NOW()) + 2, 1.5);
@u2
u2 / iterator.rb
Created March 21, 2014 03:15 — forked from tmm1/iterator.rb
module EventMachine
# A simple iterator for concurrent asynchronous work.
#
# Unlike ruby's built-in iterators, the end of the current iteration cycle is signaled manually,
# instead of happening automatically after the yielded block finishes executing. For example:
#
# (0..10).each{ |num| }
#
# becomes:
#
#!/usr/bin/env ruby
# Please read http://otobrglez.opalab.com for more information about this code.
class Book < Struct.new(:title)
def words
@words ||= self.title.gsub(/[a-zA-Z]{3,}/).map(&:downcase).uniq.sort
end
@u2
u2 / base.css
Created April 23, 2014 13:53
base.css
@charset "utf-8";
/*!
* @名称:base.css
* @功能:1、重设浏览器默认样式
* 2、设置通用原子类
*/
/* 防止用户自定义背景颜色对网页的影响,添加让用户可以自定义字体 */
html {
background:white;
color:black;

Routes

小心地使用 Match(Rails 3 已实现)

Rails 3 提供了 match 方法供我们自定义 routes,然而我们要小心使用它以避免“跨站脚本攻击”(XSS Attack)。比如像这样的 routes:

注:(r3 代表 Rails 3,r4 代表 Rails 4)

# routes.rb
.nav-list {
padding-left: 15px;
padding-right: 15px;
margin-bottom: 0;
}
.nav-list > li > a,
.nav-list .nav-header {
margin-left: -15px;
margin-right: -15px;
@u2
u2 / non_block_socket
Created May 8, 2014 07:57
Non block socket
….
send_data = “aaaaaaaaaaaaa”;
i = 0;
begin
t = TCPSocket.new("127.0.0.1", 80);
t.send(send_data, 0);
begin
recv_data = t.recv_nonblock(100); #也可用read_nonblock代替
recv_data.strip!;
rescue IO::WaitReadable
@u2
u2 / str_hex
Created May 8, 2014 08:52
str hex
str.scan(/../).map(&:hex).map(&:chr).join