2023年1月31日火曜日

Cozy Web/動作モード

Cozy Webでは開発中や製品版といった動作モードでアプリケーションの振る舞いを調整することができます。

今回は動作モードを使用して、開発時の初期データやデモ時に使用するデータの初期投入を行う方法について説明します。

モード

Cozy Webでは以下の動作モードがあります。

developモードで開発を行い、製品リリース時にproductionモードにします。

develop
開発モード(デフォルト)
production
製品モード
test
テスト・モード
demo
デモ・モード

test用途、デモ用途にはそれぞれtestモード、demoモードを使用します。

HelloResource

前回作成したHelloResourceに対して、デモ実行時に埋め込みDB上に初期化データを投入する設定を行います。

準備

前回の作業でcozyを起動するディレクトリのwebappsディレクトリに、アプリケーションのホームディレクトリとなるHelloResourceができています。このHelloResourceを使用します。

ビュー

HelloResourceでは、Web画面を記述するビュー(*.html, *.jadeなど)は定義していません。リソースの表示はデフォルトのビュー機能によって自動的にレイアウトされます。

これは前回と変わりません。

モデル

WEB-INF/modelsでアプリケーションで使用するモデルを定義します。以下のモデル定義をmodel.orgとしてWEB-INF/modelsに格納します。

これも前回と変わりません。

  1. * entity    
  2. ** product  
  3. *** features    
  4. table=product  
  5. *** attributes    
  6. | Name  | Type   | Multiplicity |  
  7. |-------+--------+--------------|  
  8. | id    | token  |            1 |  
  9. | name  | string |            1 |  
  10. | price | int    |            1 |  

初期化データ

WEB-INF配下に動作モード名のディレクトリを作成し、そこに各種定義ファイルを配置することで動作モード固有の定義を行うことができます。

今回はデモ時にデータの初期投入を行ってみます。

デモ・モードを示すdemoディレクトリをWEB-INF配下に作成し、そこにモデル定義のファイルmodels/model.orgを配置します。

WEB-INF/demo/models/model.orgの内容は以下になります。

  1. * data-store  
  2. ** product  
  3.     
  4. *** data  
  5. 1,Apple,300  
  6. 2,Orange,350  
  7. 3,Peach,400  
  8. 4,berry,450  

DBのprodutテーブルに4レコードを作成します。

実行

それではHelloResourceを実行してみましょう。

一覧

まず一覧取得です。

モデルとして定義したリソースのproductの一覧取得は以下になります。

WebアプリケーションのURL http://localhost:8080/web/HelloResource の直下にリソース名productを指定しています。

  1. $ curl http://localhost:8080/web/HelloResource/product  

この結果、以下のHTML文書が返されます。(読みやすいようにxmllintで整形しています。)

  1. <?xml version="1.0"?>  
  2. <!DOCTYPE html>  
  3. <html>  
  4.   <head>  
  5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
  6.     <meta name="keywords" content="TBD"/>  
  7.     <meta name="description" content="TBD"/>  
  8.     <meta name="robots" content="noindex,nofollow"/>  
  9.     <meta name="author" content="TBD"/>  
  10.     <title>product</title>  
  11.   </head>  
  12.   <body>  
  13.     <table class="">  
  14.       <caption class="">product</caption>  
  15.       <thead class="">  
  16.         <tr class="">  
  17.           <th scope="col" class="">Id</th>  
  18.           <th scope="col" class="">Name</th>  
  19.           <th scope="col" class="">Price</th>  
  20.         </tr>  
  21.       </thead>  
  22.       <tbody class="">  
  23.         <tr data-href="product/1.html">  
  24.           <td class="">1</td>  
  25.           <td class="">Apple</td>  
  26.           <td class="">300</td>  
  27.         </tr>  
  28.         <tr data-href="product/2.html">  
  29.           <td class="">2</td>  
  30.           <td class="">Orange</td>  
  31.           <td class="">350</td>  
  32.         </tr>  
  33.         <tr data-href="product/3.html">  
  34.           <td class="">3</td>  
  35.           <td class="">Peach</td>  
  36.           <td class="">400</td>  
  37.         </tr>  
  38.         <tr data-href="product/3.html">  
  39.           <td class="">4</td>  
  40.           <td class="">Berry</td>  
  41.           <td class="">450</td>  
  42.         </tr>  
  43.       </tbody>  
  44.     </table>  
  45.   </body>  
  46. </html>  

今回データとして投入した4つのエンティティに対応する情報が表示されています。

詳細

  1. $ curl http://localhost:8080/web/HelloResource/product/4  

この結果、以下のHTML文書が返されます。(読みやすいようにxmllintで整形しています。)

  1. <?xml version="1.0"?>  
  2. <!DOCTYPE html>  
  3. <html>  
  4.   <head>  
  5.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>  
  6.     <meta name="keywords" content="TBD"/>  
  7.     <meta name="description" content="TBD"/>  
  8.     <meta name="robots" content="noindex,nofollow"/>  
  9.     <meta name="author" content="TBD"/>  
  10.   </head>  
  11.   <body>  
  12.     <table class="">  
  13.       <tbody>  
  14.         <tr class="">  
  15.           <th scope="row" class="">Id</th>  
  16.           <td class="">4</td>  
  17.         </tr>  
  18.         <tr class="">  
  19.           <th scope="row" class="">Name</th>  
  20.           <td class="">Berry</td>  
  21.         </tr>  
  22.         <tr class="">  
  23.           <th scope="row" class="">Price</th>  
  24.           <td class="">450</td>  
  25.         </tr>  
  26.       </tbody>  
  27.     </table>  
  28.   </body>  
  29. </html>  

まとめ

データベースを使用するシステム開発を行う場合、開発を進める実行環境上でのDBの構築が煩雑な作業となっています。今回はCozy Webのデータ移入機能とモードを組み合わせて対処する方法を紹介しました。

次回はビューを定義して、デザイン化された画面でエンティティ情報の表示を行ってみる予定です。

諸元

Cozy
0.0.11