「dotCloud/Play1.2.5PostgreSqlを使う」の編集履歴(バックアップ)一覧はこちら
追加された行は緑色になります。
削除された行は赤色になります。
[[dotcloud/play1.2.5アプリのデプロイ]]の続きです。
* dotcloud + Play framework1.2.5 + PostgreSql で使ってみましょう。
*** 構成ファイルにPostgreSqlを使う記述を追加
#highlight(){{
$ vi ~/play/dotcloud/hello/dotcloud.yml
www:
type: java
data:
type: postgresql
}}
*** helloプロジェクトをデプロイ
※一旦デプロイしないとDB接続情報を見られません。
#highlight(){{
$ dotcloud push hello ~/play/dotcloud/hello/
}}
*** DB接続情報確認
#highlight(){{
$ dotcloud info hello.data
config:
postgresql_password: ぱすわーど
created_at: 1341474539.4294181
datacenter: Amazon-us-east-1c
image_version: 220b790a574f (latest)
instance: data.0
memory:
- total reserved: N/A (sandbox app)
- total used: 8MB
- cache portion: 3MB of 8MB (43%)
ports:
- name: ssh
url: ssh://postgres@hello-chapati.dotcloud.com:ぽーと番号
- name: sql
url: pgsql://root:ぱすわーど@hello-chapati.dotcloud.com:ぽーと番号
pricing: free
state: running
type: postgresql
}}
*** DBユーザー作成
#highlight(){{
$ dotcloud run hello.data -- createuser chapati --pwprompt
# createuser chapati --pwprompt
Enter password for new role: 新しいユーザーのパスワード入力
Enter it again: 再入力
Shall the new role be a superuser? (y/n) y
}}
*** DB作成
#highlight(){{
$ dotcloud run hello.data -- createdb mydb
# createdb mydb
}}
* helloプロジェクト修正
DBを使えるか確認するための簡単なプログラムを作成します。
** 修正内容
+ トップページにユーザー情報入力フォームを表示。
+ トップページにユーザー一覧を表示。
+ 登録ボタンで入力されたユーザー情報を登録しトップページに戻る。
*** application.confにDB接続情報を記述
※DB接続情報確認で表示された情報をちょこっと加工してあります。
#highlight(){{
$ vi ~/play/apps/hello/conf/application.conf
db=postgres://root:ぱすわーど@hello-chapati.dotcloud.com:ぽーと番号/mydb
}}
*** Applicationコントローラ修正
#highlight(){{
$ cat ~/play/apps/hello/app/controllers/Application.java
package controllers;
import play.*;
import play.mvc.*;
import java.util.*;
import models.*;
public class Application extends Controller {
public static void index() {
List<User> users = User.findAll();
render(users);
}
public static void registUser(User user) {
user.save();
index();
}
}
}}
*** Application/index.html テンプレート修正
ユーザ登録フォームとユーザ一覧を表示できるようにします。
#highlight(){{
$ cat ~/play/apps/hello/app/views/Application/index.html
#{extends 'main.html' /}
#{set title:'ユーザー登録' /}
<h3>ユーザー登録</h3>
#{form @Application.registUser()}
<div>名前:<input type="text" value="" name="user.name"/></div>
<div >メールアドレス:<input type="text" value="" name="user.email"/></div>
<input type="submit" value='登録'>
#{/form}
<h3>登録ユーザー</h3>
#{list items:users, as:'user'}
<div>
${user.name}
${user.email}
</div>
#{/list}
}}
*** User モデル作成
#highlight(undefined){{
$ cat ~/play/apps/hello/app/models/User.java
package models;
import javax.persistence.Entity;
import javax.persistence.FieldResult;
import javax.persistence.ManyToOne;
import play.db.jpa.Model;
// dotcloudのDBにUserテーブルは作成できないためテーブル名指定
@Entity(name="helloUser")
public class User extends Model {
public String name;
public String email;
public User(String name, String email, String desc) {
this.name = name;
this.email = email;
}
}
}}
*** 修正したhelloプロジェクトをデプロイ
#highlight(){{
$ ~/play/play-1.2.5/play war hello -o ~/play/dotcloud/hello/hello.war --zip
$ dotcloud push hello ~/play/dotcloud/hello/
}}
*** ブラウザから何人かユーザー登録
#image(width=600, play125postgresql.png)
** ちゃんとDBに登録できたか確認
*** sshでhello.dataに接続
#highlight(){{
$ dotcloud ssh hello.data
# $SHELL
postgres@hello-default-data-0:~$
}}
*** mydbに接続
#highlight(){{
postgres@hello-default-data-0:~$ psql mydb
psql (9.0.4)
Type "help" for help.
}}
*** hellouserテーブルをselect文で確認してみる
#highlight(){{
mydb=# select * from hellouser;
id | email | name
----+----------+----------
1 | test | test
2 | hogehoge | hogehoge
3 | 2222 | 1111
4 | 88888 | 9999
(4 rows)
}}
画面と同じ物が表示されたのでOK!!
[[dotcloud/play1.2.5アプリのデプロイ]]の続きです。
* dotcloud + Play framework1.2.5 + PostgreSql で使ってみましょう。
*** 構成ファイルにPostgreSqlを使う記述を追加
#highlight(){{
$ vi ~/play/dotcloud/hello/dotcloud.yml
www:
type: java
data:
type: postgresql
}}
*** helloプロジェクトをデプロイ
※一旦デプロイしないとDB接続情報を見られません。
#highlight(){{
$ dotcloud push hello ~/play/dotcloud/hello/
}}
*** DB接続情報確認
#highlight(){{
$ dotcloud info hello.data
config:
postgresql_password: ぱすわーど
created_at: 1341474539.4294181
datacenter: Amazon-us-east-1c
image_version: 220b790a574f (latest)
instance: data.0
memory:
- total reserved: N/A (sandbox app)
- total used: 8MB
- cache portion: 3MB of 8MB (43%)
ports:
- name: ssh
url: ssh://postgres@hello-chapati.dotcloud.com:ぽーと番号
- name: sql
url: pgsql://root:ぱすわーど@hello-chapati.dotcloud.com:ぽーと番号
pricing: free
state: running
type: postgresql
}}
*** DBユーザー作成
#highlight(){{
$ dotcloud run hello.data -- createuser chapati --pwprompt
# createuser chapati --pwprompt
Enter password for new role: 新しいユーザーのパスワード入力
Enter it again: 再入力
Shall the new role be a superuser? (y/n) y
}}
*** DB作成
#highlight(){{
$ dotcloud run hello.data -- createdb mydb
# createdb mydb
}}
* helloプロジェクト修正
DBを使えるか確認するための簡単なプログラムを作成します。
** 修正内容
+ トップページにユーザー情報入力フォームを表示。
+ トップページにユーザー一覧を表示。
+ 登録ボタンで入力されたユーザー情報を登録しトップページに戻る。
*** application.confにDB接続情報を記述
※DB接続情報確認で表示された情報をちょこっと加工してあります。
#highlight(){{
$ vi ~/play/apps/hello/conf/application.conf
db=postgres://root:ぱすわーど@hello-chapati.dotcloud.com:ぽーと番号/mydb
}}
*** Applicationコントローラ修正
#highlight(){{
$ cat ~/play/apps/hello/app/controllers/Application.java
package controllers;
import play.*;
import play.mvc.*;
import java.util.*;
import models.*;
public class Application extends Controller {
public static void index() {
List<User> users = User.findAll();
render(users);
}
public static void registUser(User user) {
user.save();
index();
}
}
}}
*** Application/index.html テンプレート修正
ユーザ登録フォームとユーザ一覧を表示できるようにします。
#highlight(){{
$ cat ~/play/apps/hello/app/views/Application/index.html
#{extends 'main.html' /}
#{set title:'ユーザー登録' /}
<h3>ユーザー登録</h3>
#{form @Application.registUser()}
<div>名前:<input type="text" value="" name="user.name"/></div>
<div >メールアドレス:<input type="text" value="" name="user.email"/></div>
<input type="submit" value='登録'>
#{/form}
<h3>登録ユーザー</h3>
#{list items:users, as:'user'}
<div>
${user.name}
${user.email}
</div>
#{/list}
}}
*** User モデル作成
#highlight(undefined){{
$ cat ~/play/apps/hello/app/models/User.java
package models;
import javax.persistence.Entity;
import javax.persistence.FieldResult;
import javax.persistence.ManyToOne;
import play.db.jpa.Model;
// dotcloudのDBにUserテーブルは作成できないためテーブル名指定
@Entity(name="helloUser")
public class User extends Model {
public String name;
public String email;
public User(String name, String email, String desc) {
this.name = name;
this.email = email;
}
}
}}
*** 修正したhelloプロジェクトをデプロイ
#highlight(){{
$ ~/play/play-1.2.5/play war hello -o ~/play/dotcloud/hello/hello.war --zip
$ dotcloud push hello ~/play/dotcloud/hello/
}}
*** ブラウザから何人かユーザー登録
#image(play125postgresql.png)
** ちゃんとDBに登録できたか確認
*** sshでhello.dataに接続
#highlight(){{
$ dotcloud ssh hello.data
# $SHELL
postgres@hello-default-data-0:~$
}}
*** mydbに接続
#highlight(){{
postgres@hello-default-data-0:~$ psql mydb
psql (9.0.4)
Type "help" for help.
}}
*** hellouserテーブルをselect文で確認してみる
#highlight(){{
mydb=# select * from hellouser;
id | email | name
----+----------+----------
1 | test | test
2 | hogehoge | hogehoge
3 | 2222 | 1111
4 | 88888 | 9999
(4 rows)
}}
画面と同じ物が表示されたのでOK!!