Fork me on GitHub

2009/08/05

はてな
ReverseHttp面白いですね。
ReverseHttp

Tunnel HTTP over HTTP, in a structured, controllable, securable way. Let programs claim part of URL space, and serve HTTP, all by using an ordinary HTTP client library.

http://www.reversehttp.net/
ただ勘違いされやすいのが「何がReverseなの」という部分。通常ブラウザからリクエストが送信され、それに対する応答がサーバから返されます。ReverseHttpはサーバで何かアクションが起きた場合に、ブラウザ側がその通知を受信する...なんて事が出来るプロトコルです。仕組みはcometというlong pollに似た仕組みで、サイトのdemoを観るとなんなく理解出来るかと思います。
例えば何が出来るのか...

ローカルPC内で動作するファイアウォール内のwebアプリを外部に公開する

rubyにhookoutというライブラリがあり、これを使用するとrackアプリがさも外部に公開されているかの様に振舞う事が出来ます。
paulj's hookout at master - GitHub

Expose Ruby applications to the web via ReverseHTTP

http://github.com/paulj/hookout/tree/master
グローバルIPが無くても、webアプリを公開出来るなんて素晴らしい!
なお、ReverseHttpはプロトコルですのでhookout以外にも同様のソフトウェアはあります。例えばmiyagawaさんが書いたAnyEvent::ReverseHttpに含まれるeg/proxy.plを使うとローカルPC内のwebアプリを外部に公開する事が出来ます。

外部で起きたアクションをローカルPCに通知させる

例えば、はてなブックマークで自分のサイトがブックマークされた瞬間にデスクトップPCが反応したらどうしますか?
スターを付けに行きませんか(笑)?ReverseHttpを使えば出来るのです。


今日はこの「はてなブックマーク通知」をやってみたいと思います。
使う材料は以下の通り。
  • hookout : 上記で紹介したrackアプリを公開するライブラリ
  • sinatra : ruby製webアプリケーションフレームワーク
  • ruby_gntp : snakaさん作のruby用Growl For Windowsインタフェース
こんだけ。
上記のサイトからhookoutを取得してインストールし、以下のsinatraアプリケーションを作成します。
my-hatebu-growler.rb
require 'rubygems'
require 'sinatra'
require 'ruby_gntp'
  
growl = GNTP.new
growl.register({
  :app_name => "はてブ",
  :notifies => [{
   :name     => "hatenabookmark",
    :enabled  => true,
  }]
})

post '/' do
  return "ng" if params[:status] !~ /add|update/
  user = params[:username]
  text = "#{params[:comment]}\r\r#{params[:title]}\r#{params[:url]}"
  icon = "http://www.hatena.ne.jp/users/#{user[0,2]}/#{user}/profile.gif"
  p params[:status]
  growl.notify({
    :name  => "hatenabookmark",
    :title => user,
    :text  => text,
    :icon  => icon,
  })
  'ok'
end
config.ru
require 'my-hatebu-growler'
set :run, false
run Sinatra::Application
これを以下の様に起動します。
hookout -a http:/www.reversehttp.net/reversehttp -n my-hatebu-growler-application -R config.ru start

my-hatebu-growler-applicationの部分は適当な物に変えて下さい。

起動すると以下の様に出力されます。
Bound to location http://my-hatebu-growler-application.www.reversehttp.net/
このURLを、はてなブックマークにwebhook登録します。
hatebu-webhook
あとは、じっとブクマされるのを待ちます。










hatebu-growler
デタ━━━゚(∀)゚━━━!!

秋の夜長に、こんなツールお一つどうでしょうか。


追記1
HTTP::Engine::Interface::ReverseHTTPもあるよとmiyagwawaさんに教えてもらいました。
hookout for HTTP::Engineらしいです。

追記2
例では分かり易くする為にwebhook APIのキー認証を省いていますが、本当はちゃんと判定する必要があります。

追記3
PerlでHTTP::Engine::Interface::ReverseHTTPを使ってみた。ネットワークGrowlにはアイコンが使える仕組みがないのが残念。

2009/01/16

はてな
今日、とある場所でkanaさんからvimに"syn-include"なんて物があるのを教えて貰いました。

:he syn-include
9. Including syntax files                               *:syn-include* *E397*

It is often useful for one language's syntax file to include a syntax file for
a related language.  Depending on the exact relationship, this can be done in
two different ways:

        - If top-level syntax items in the included syntax file are to be
          allowed at the top level in the including syntax, you can simply use
          the |:runtime| command: >

  " In cpp.vim:
  :runtime! syntax/c.vim
  :unlet b:current_syntax

<       - If top-level syntax items in the included syntax file are to be
          contained within a region in the including syntax, you can use the
          ":syntax include" command:

:sy[ntax] include [@{grouplist-name}] {file-name}

          All syntax items declared in the included file will have the
          "contained" flag added.  In addition, if a group list is specified,
          all top-level syntax items in the included file will be added to
          that list. >

   " In perl.vim:
   :syntax include @Pod <sfile>:p:h/pod.vim
   :syntax region perlPOD start="^=head" end="^=cut" contains=@Pod
<
          When {file-name} is an absolute path (starts with "/", "c:", "$VAR"
          or "<sfile>") that file is sourced.  When it is a relative path
          (e.g., "syntax/pod.vim") the file is searched for in 'runtimepath'.
          All matching files are loaded.  Using a relative path is
          recommended, because it allows a user to replace the included file
          with his own version, without replacing the file that does the ":syn
          include".
知らんかった...
これを使えばPerlのPodだけ別のsyntaxファイルから適用出来るといった物。
これは凄い!

つまりはregionだけ決められれば、その部分に言語毎のsyntaxが適用出来る事になる。試しに、はてなのスーパーPre記法に色を付けられる様にしてみた。
ベースはmotemenさんのhatena-vim
motemen's hatena-vim at master - GitHub

Vim scripts for posting/updating hatena diary/group

http://github.com/motemen/hatena-vim/tree/master
このsyntax/hatena.vimの最下行に以下のコードを貼り付ける。
" append to syntax/hatena.vim
function SyntaxSuperPre()
  let lnum = 1
  let lmax = line("$")
  let mx = '^>|\(.*\)|$'
  while lnum <= lmax
    let curline = getline(lnum)
    if curline =~ mx
      let lang = substitute(curline, mx, '\1', '')
      exec 'runtime! syntax/'.lang.'.vim'
      unlet b:current_syntax
      let syntaxfile = fnameescape(substitute(globpath(&rtp, 'syntax/'.lang.'.vim'), '[\r\n].*$', '', ''))
      if len(syntaxfile)
        exec 'syntax include @inline_'.lang.' '.syntaxfile
        exec 'syn region hatenaSuperPre matchgroup=hatenaBlockDelimiter start=+^>|'.lang.'|$+ end=+^||<$+ contains=@inline_'.lang
      endif
    end
    let lnum = lnum + 1
  endwhile
  " workaround for perl
  syn cluster inline_perl remove=perlFunctionName
endfunction
call SyntaxSuperPre()
適当なので使わないで下さい
perlFunctionNameはregionが広すぎるので無効にしてます
すると...
hatena super pre on vim
おーーー!出ました。

続きはこの辺でやっていきます。出来上がったらmotemenさんにmergeして貰うのも良いかも。
ちなみに、filetype適用時にロードしているので、pre記法の言語を編集途中で変更したり、新しくスーパーPre記法を追加したりするとsyntaxが適用されなくなります。ま、これからですな。

syn-include++
Posted at 12:13 in ソフトウェア::vim | WriteBacks (0)
Tagged as: hatena, vim
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip | add to buzzurl add to buzzurl | add to fc2bookmark add to fc2bookmark | add to Yahoo Bookmark add to Yahoo Bookmark | add to Pookmark add to Pookmark