sohatach's blog

http://github.com/soha

play framework 2.0.3が自動生成するファイル考察

1.testprojという名前で新規プロジェクトを作成してみる。

play new testproj

C:\Scala\workspace>play new testproj
       _            _
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/

play! 2.0.3, http://www.playframework.org

The new application will be created in C:\Scala\workspace\testproj

What is the application name?
> testproj

Which template do you want to use for this new application?

  1 - Create a simple Scala application
  2 - Create a simple Java application
  3 - Create an empty project

> 1

OK, application testproj is created.

Have fun!

以下のファイルが生成されていました。

.gitignore
README
app\controllers\Application.scala  //アプリケーションコントローラ
app\views\index.scala.html  //indexテンプレートファイル
app\views\main.scala.html  //mainテンプレートファイル
conf\application.conf  //アプリケーション設定ファイル
conf\routes  //URLルーティング定義ファイル
project\Build.scala  //sbtビルド定義ファイル
project\build.properties  //使用するsbtのバージョン指定ファイル
project\plugins.sbt  //sbtビルド定義の拡張プラグイン定義ファイル
public\images\favicon.png
public\javascripts\jquery-1.7.1.min.js
public\stylesheets\main.css


playでは、sbtというScalaのビルドシステム(JavaMavenみたいなもの)が使われています。
参考 始めるsbt:http://scalajp.github.com/sbt-getting-started-guide-ja/

生成されたファイルの中身は以下のようなものでした。

app\controllers\Application.scala

package controllers

import play.api._
import play.api.mvc._

object Application extends Controller {
  
  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }
  
}

app\views\index.scala.html

@(message: String)

@main("Welcome to Play 2.0") {
    
    @play20.welcome(message)
    
}

app\views\main.scala.html

@(title: String)(content: Html)

<!DOCTYPE html>

<html>
<head>
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
<script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
</head>
<body>
@content
</body>
</html>

conf\routes

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                           controllers.Application.index

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)

 

2.Eclipse用プロジェクトファイルの生成

1で生成されたtestprojディレクトリに移動し、

play eclipsify

を実行する。

.classpath
.project
.settings\org.scala-ide.sdt.core.prefs

が生成される。

ここでEclipse用のビルドパスの設定ファイル.classpathに様々な依存ライブラリのjarファイルに加え

<classpathentry path="target\scala-2.9.1\src_managed\main" kind="src"></classpathentry>

というクラスパスエントリーがある。
これはプロジェクトディレクトリのtarget下にあるファイルのため、不用意に消してしまうとEclipse上ビルドエラーになってしまいます。
ただし、このtarget下ファイルは、.gitignoreでコミット対象ファイルからも外されています。
どうもplay runでプロジェクト実行時に自動生成されているようです。

 

3.プログラムの処理の流れ

 1.ブラウザからのリクエストを受け付ける。

 2.conf/routesの定義に基づきアプリケーションコントローラのメソッドが呼ばれる。

  例:GET     /                           controllers.Application.index

  /へのリクエストについては、controllers.Applicationクラスのindexメソッドが呼ばれる。

 3.アプリケーションコントローラメソッド内でレスポンスを返す。

  例:Ok(views.html.index("Your new application is ready."))

  上記の例では、index.scala.htmlテンプレートファイルが出力される。
  index.scala.htmlテンプレートファイルでは、

  @main("Welcome to Play 2.0") {

  により、さらにmain.scala.htmlテンプレートファイルを画面の部品としてインクルードしている。

  @main("Welcome to Play 2.0") {
     @play20.welcome(message)
  }

についてもう少し。
@mainは、main.scala.htmlというファイル名のテンプレートファイルをインクルードするという意味で
("Welcome to Play 2.0")は、、main.scala.htmlに渡す第1引数title、{}の中で定義されているものが第2引数contentに対応します。

Windows7(64bit)でのPython環境構築

1.Pythonのインストール

http://www.python.jp/Zope/download/pythoncore
から「python-2.7.3.amd64.msi」をダウンロードし、Windowsにインストールします。

2.Pythonのパッケージ管理のための追加パッケージのインストール

Windows用のバイナリは、
http://www.lfd.uci.edu/~gohlke/pythonlibs/
よりダウンロード可能です。
以下をダウンロードし、Windows上でインストールします。

・distribute-0.6.27.win-amd64-py2.7.exe
・pip-1.1.win-amd64-py2.7.exe

上記で、Pythonでのパッケージ管理コマンドであるpipが使用可能になります。
(pipはWindows上のコマンドになります。)
なおdistributeパッケージを入れた時点で、pipと似たような機能のeasy_installというコマンドがインストールされますが、
最近では、easy_installの代わりにpipを使うことが推奨されているようです。

このあたり、追加パッケージ管理コマンドは、ほぼ必須のコマンドと言ってように思われるため、
RubyのgemコマンドみたいにPython自体に組み込んでくれても良いような気がします。。。
いくつも選択肢があるとそれだけで迷ってしまうこともあるので。

GmailをリレーしたPostfixからのメール送信

Debian6(squeeze)でPostfixからメールを送る。

 

1./etc/postfix/main.cfに以下を追加。

smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = secure
smtp_tls_CAfile = /etc/ssl/certs/Equifax_Secure_CA.pem
relayhost = [smtp.gmail.com]:587

/etc/ssl/certs/Equifax_Secure_CA.pemは、apt-getでca-certificatesパッケージを入れることで作られます。

 

2./etc/postfix/sasl_passwdを作成し、以下のように修正。

[smtp.gmail.com]:587 GMailのアドレス:GMailのパスワード

 

3./etc/postfix/sasl_passwd.dbファイルの作成。

postmap sasl_password

を実行する。

 

上記で送信できるとのことでしたが、少しはまりました。

sendmailコマンドにてメール送信を試すと/var/log/mail.logに以下のエラーが発生。

postfix/error status=bounced ([smtp.gmail.com]:587)

ログにはbouncedした旨記載はあるものの、ログインに失敗したなどのエラーの原因が記載されていませんでした。

 

 よくよくmain.cfの設定を見直すと以下の記載を発見。

default_transport = error
relay_transport = error

これらをコメントアウトすることでメール送信できるようになりました。

上記の設定だとリレーをさせないという意味になるようです。