LoginSignup
10
10

More than 5 years have passed since last update.

Smalltalk Web開発編 その0(Teapot)

Last updated at Posted at 2014-12-10

はじめに

マイクロWebフレームワーク「Teapot」の紹介です。

SmalltalkのWebアプリケーションフレームワークといえば、Seaside、AIDA、Iliad、Amber Smalltalkといったところが有名ドコロです。
しかしながら、「ちょっとした小物を作りたい」という用途には、思想が独特であったり、歴史を経て大きくなっていたりと、やや取り回しが面倒です。

Teapotは、小さくて理解しやすい、どこかで見たことがあるような、どちらかといえば庶民向けのフレームワークです。
「Smalltalkに入門するついでにWebもお試しで」という用途にもうまくはまると思います。

Teapotの守備範囲

Teapotは、Pharo Smalltalkに同梱されている、Zinc HTTP Componentsを薄くラッピングしたライブラリです。
主に、「GET、POST等のHTTPメソッド」「URLパターン」「リクエストに対するアクション」の三つ組みを取り扱います。

Example01
| teapot |
Teapot stopAll.
teapot := Teapot configure: {
    #port -> 8080.
}.

teapot
    GET: '/cat/<who>/<name>' -> [:req | (req at: #who) , ' は ' , (req at: #name) , ' である'];
    start.

環境の準備

Pharo 3.0 を入手し、以下のスクリプトを実行することでTeapotのインストールができます。

InstallTeapot
Gofer it
  url: 'http://mc.stfx.eu/Neo';
  package: 'Neo-JSON-Core';
  load.

Gofer it
    smalltalkhubUser: 'zeroflag' project: 'Teapot';
    configuration;
    loadStable.

解説はしませんが、テンプレートエンジンに「Mustache」を使用する場合は、以下も合わせて実行すると良いでしょう。

InstallMustache
Gofer it
  smalltalkhubUser: 'NorbertHartl' project: 'Mustache';
  configurationOf: 'Mustache';
  loadStable.

サンプルプログラム「アクセス カウンター」

シンプルなアクセスカウンターです。
Webブラウザで、http://localhost:8080/countup にアクセスする度にカウントアップします。

AccessCounter
| teapot count |
Teapot stopAll.
teapot := Teapot configure: {
    #port -> 8080.
}.

count := 0.
teapot
    GET: '/countup' -> [:req | count := count + 1. count];
    start.

サンプルプログラム「ゴッドクラス/ゴッドメソッド ランキング」

オブジェクト指向の神 〜ゴッドクラス と ゴッドメソッド〜」がいい感じなので、こちらでもやってみましょう。
JSON形式で、ランキングを返します。

GodRanking
| teapot cache |
Teapot stopAll.
teapot := Teapot configure: {
    #port -> 8080. #defaultOutput -> #json.
}.

cache := Dictionary new.

teapot
  GET: '/god/own' -> [:req |
    cache
      at: req uri
      ifAbsentPut: [(ProtoObject allSubclasses collect: [:c | {c selectors size. c name}])
                      asSortedCollection: [:a :b | a first > b first]]];
  GET: '/god/total' -> [:req |
    cache
      at: req uri
      ifAbsentPut: [(ProtoObject allSubclasses collect: [:c | {c allSelectors size. c name}])
                      asSortedCollection: [:a :b | a first > b first]]];
  GET: '/god/method' -> [:req |
    cache
      at: req uri
      ifAbsentPut: [(CompiledMethod allInstances 
                      collect: [:m | {m getSource asString lines size. m name}])
                        asSortedCollection: [:a :b | a first > b first]]];
  start.

おわりに

マイクロWebフレームワーク「Teapot」のちょっとした紹介でした。
この他、こちら をざっと眺めれば、大体のことは把握できると思います。
イロイロ簡単にやれそうな感じがするのではないでしょうか。お試しあれ。

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10