annotatable-http-proxyを作ろうと思う
大学の頃
任意のサイト(主に授業の教材)のHTMLコンテンツに対してメモを書けるようにし
それをユーザ間で共有することで、学習支援しようという研究してた。
実装方法
- WebサーバにGET/POSTリクエストでメモを書き込みたいサイトのURLを送る
- WebサーバがURLを基にオリジナルのサイトからコンテンツを取得
- Webサーバ上で先ほど取得したコンテンツにメモを付加
- ユーザに対して、メモが付加されたコンテンツを返す
というものだった。
PHPのフレームワークとしてsymfonyを導入したり、JavaScriptを大幅に書き直したりと
好き勝手やらせてもらって、それはそれで楽しかったけれど知識不足で果たせなかったこともあった。
proxyサーバ化するというもの
いちいちURLを送るのはスマートでは無いとは感じていたけれど
proxyサーバって難しそう、と避けていた。
そこでnode.jsですよ
node.jsの勉強がてら、全部JSで実装してみよう!
Apacheにmod_proxy入れれば
(∩゚д゚)アーアーきこえなーい
node.jsでproxyサーバ
httpサーバを立てる
リクエストは、GETの当て決め
サーバから受け取ったレスポンスをそのままブラウザに返す。
var sys = require('sys'), url = require('url'), http = require('http'); var port = 8000; http.createServer(function(req, res) { http.get({ host: req.headers.host, port: url.parse(req.url).port || 80, method: 'GET', path: req.url, headers: req.headers }, function(response) { res.writeHead(response.statusCode, response.headers); response.on('data', function(chunk){ res.write(chunk); }); response.on('end', function(){ res.end(); }); }); }).listen(port); sys.puts('Server listening on port ' + port);
ブラウザ側の設定
環境設定 > ネットワーク
辺りでプロキシを設定する。
簡単だな。
XMLの差分取得2
textNodeの中身消去して、XMLの構造だけを比較するバージョン
#!/usr/bin/perl use strict; use warnings; use utf8; use LWP::UserAgent; use XML::Simple; use XML::DOM::XPath; use Text::Diff; binmode(STDOUT, ':utf8'); my $content1 = get('http://search.hatena.ne.jp/keyword?word=ABC&mode=rss&ie=utf8&page=1'); my $content2 = get('http://search.hatena.ne.jp/keyword?word=ABC&mode=rss&ie=utf8&page=2'); print diff \$content1, \$content2; sub get { my $ua = LWP::UserAgent->new; my $xs = XML::Simple->new(ForceArray => 1, RootName => 'root'); my $r = $ua->get(shift); die $r->status_line unless $r->is_success; my $content = parse($r->content); return $xs->XMLout($xs->XMLin($content)); } sub parse { my $parser = XML::DOM::Parser->new(); my $doc = $parser->parse(shift); my @nodelist = $doc->findnodes('//text()'); foreach my $node (@nodelist) { $node->setNodeValue(''); } return $doc->toString(); }
XMLの差分取得
今日は、Windows XP環境で実施。
cpanが使えるということで、"Strawberry Perl"をインストールしてみた。
http://strawberryperl.com/
んで、はてなのサーチAPI叩いてみたよ。
#!C:\strawberry\perl\bin use strict; use warnings; use utf8; use LWP::UserAgent; use XML::Simple; use Text::Diff; # コマンドプロンプトで文字化けしないように binmode(STDOUT, 'encoding(cp932)'); # レスポンスのXMLを取得 my $content1 = request('http://search.hatena.ne.jp/keyword?word=ABC&mode=rss&ie=utf8&page=1'); my $content2 = request('http://search.hatena.ne.jp/keyword?word=ABC&mode=rss&ie=utf8&page=2'); # 差分表示 print diff \$content1, \$content2; sub request { my $ua = LWP::UserAgent->new; my $r = $ua->get(shift); # XMLを整形 return XMLout(XMLin($r->content)); }
APIからXMLデータ取得してそのまま標準出力
perlとも友好関係を結ぼうと試し中
#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use XML::Simple; my $ua = LWP::UserAgent->new; my $r = $ua->get('http://search.hatena.ne.jp/keyword?word=ABC&mode=rss&ie=utf8&page=1'); binmode(STDOUT, ":utf8"); print XMLout(XMLin( $r->content ));
iMac上に開発環境整える
逐次付け足していく予定
サマリー
参考書籍
Windows用だし、情報古いんで、あくまで参考程度
Struts2 フルスタック化&Webアプリケーション開発 ~Struts2+Spring 2+Tiles+iBATIS+GWTで作るショッピングサイト~
- 作者: 三国逸文
- 出版社/メーカー: 毎日コミュニケーションズ
- 発売日: 2008/11/28
- メディア: 単行本(ソフトカバー)
- 購入: 4人 クリック: 179回
- この商品を含むブログ (7件) を見る
Install Java
Java for Mac OS X 10.6 Update 3を入れたら、Headerファイルがどうのって怒られたので
Apple Developer Connectionまで、Install Java for Mac OS X 10.6 Update 3 Developer Packageを取りにいってインストール
Apple Developer Connectionは、登録作業が必須。
日本語使えないので、英語で頑張る。
Install Eclipse
Eclipse Helios (3.6.1) Eclipse IDE for Java EE Developersをインストール。ここは好みの問題?
http://www.eclipse.org/downloads/
Add Eclipse Plugin
Google Web Toolkit
App Engineもいずれ使うかな?ってことで一緒に入れた
Google Plugin for Eclipse 3.6 (Helios) Installation Instructions
http://code.google.com/intl/ja/eclipse/docs/install-eclipse-3.6.html
Cypal Studio
GWTでの開発をサポートするプラグインらしい
GWTのバージョンが2.0だったので、2.0用のCypal Studioをインストール
cypal-studio
http://code.google.com/p/cypal-studio/
お目当ての品を頂く
in.cypal.studio.for.gwt-2.alpha..zip
# ユーザ名は自分のに適宜読み替えで % cd /Users/{TakiTake}/Downloads # 物があるか確認。 % ls -l in.cypal.studio.for.gwt-2.alpha..zip # 心配性な人はchecksumもチェック % openssl sha1 in.cypal.studio.for.gwt-2.alpha..zip -------------------------------------------------------- 期待値 SHA1(in.cypal.studio.for.gwt-2.alpha..zip)= 438485a59c21b21435d1ae94c38fda2d67b7b3ba -------------------------------------------------------- # 解凍 % unzip in.cypal.studio.for.gwt-2.alpha..zip # jarファイルをEclipseのpulginフォルダに放り込む # Eclipseのフォルダへのパスは自分の環境に合わせて読み替えで % cp -i in.cypal.studio.gwt.core_2.0.0.200903252317.jar /path/to/eclipse/plugins/ % cp -i in.cypal.studio.gwt.ui_2.0.0.200903252317.jar /path/to/eclipse/plugins/
Eclipseを再起動させて
Eclipse > Preferences > Cypal Studio
があれば、インストール完了。
GWTとの連携方法は、調査中。。
Install Tomcat
macportsから、tomcat6を入れました。
# port install tomcat6
Eclipse > Preferences > Server > Runtime Enviroments
Addボタンクリック
Apache Tomcat v6.0 を選択
インストールディレクトリ指定(/opt/local/share/java/tomcat6)
ServersのViewで、control + クリックして、New > Server
Tomcat v6.0 Serverを選択
サーバ立ち上がればおk
Install PostgreSQL
macportsで9.0をインストール
# port install postgresql90 postgresql90-server
後は、指示通りにコマンド打てば起動できます。
注意点としては、
# /opt/local/lib/postgresql90/bin/postgres -D /opt/local/var/db/postgresql90/defaultdb
ってやると権限関連で怒られるので
# sudo su - postgres -c '/opt/local/lib/postgresql90/bin/postgres -D /opt/local/var/db/postgresql90/defaultdb'
と、su - postgresで委譲する。
インストールメモ
Vim
できれば、新しいのが良い。
Mercurialから落とすか
Mercurialインストール
# yum install gcc python python-devel python-setuptools # easy_install mercurial
ncurses-devel必要なんで入れとく
# yum install ncurses-devel
いよいよVim落としてきてインストール
# hg clone https://vim.googlecode.com/hg/ vim # cd vim/src # ./configure --enable-multibyte --with-features=huge --disable-selinux # make # make install # make clean
起動
# vim
やれやれ