File: Cgi

package info (click to toggle)
ruby-amrita 1.0.2-10
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,940 kB
  • sloc: ruby: 9,159; xml: 978; makefile: 78
file content (199 lines) | stat: -rw-r--r-- 4,950 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

= Using Amrita with cgi

== summary

This document describes how to use amrita with cgi programing using
series of sample code.

The sample is a web bookmark system.
You can see the demo at...

  http://www.walrus-ruby.org/amrita/


== The model class

At first, we make the model class. 

  :include: sample/cgi/bmmodel.rb

The class +Item+ is the bookmark items. It has three attributes:
group, name, url.

The class + BookmarkList is collaction of +Item+. It contains +Item+ s
separated by groups and is able to save and load the list to/from a
file.

The model class has nothing to do with HTML. So, it can be unit-tested
easily.


== bookmark.cgi

bookmark.cgi displays bookmark list. And it accepts new bookmark entry.

=== html template file

bookmark.cgi uses this template.

  :include: sample/cgi/bookmark.html

=== code

This is the code of bookmark.cgi

  :include: sample/cgi/bookmark.cgi

=== use a model class object for amrita's model data

  class Item
    include Amrita::ExpandByMember

    def link
      e(:a, :href=>url) { url } # <a href="http://www.xxx.com/">http://www.xxx.com/</a>
    end
  end

Ruby's class is an open class: it can be edited by user without
modifing the original code. The class +Item+ is defined in other
source file.

We make this class include Amrita::ExpandByMember and add a method
named +link+ so that it's method can be used directly by template.

      <tr id=items>                        
        <td id="name"></td>                
        <td id="link"></td>                
      </tr>                                

We will provide +Item+ objects for id +items+ and because +Item+
object is a Amrita::ExpandByMember object,id +name+ and +link+ will be
used as method names.

+url+ is a method related to MODEL so it should be defined in model
class (bmmodel.rb). And +link+ contains information about VIEW (HTML
presentation) so it's better to put it to the view related
source(bookmark.cgi).

=== make a forms element

If you add a new item, the next page displayed contains the selection
of groups with default of the selected group.

The model data here....

    :form => {
      :group_sel=>e(:select, :name=>"group_sel") {
        groups.collect do |g|
          if g == selected_group
            e(:option, :value=>g, :selected=>"selected") { g }
          else
            e(:option, :value=>g) { g }
          end
        end
      },
    }

generates this html.

   <td>
     <select name="group_sel">
       <option value="BBS">BBS</option>
       <option value="Script Languages" selected="selected">Script Languages</option>
       <option value="TestXSS">TestXSS</option>
     </select>
   </td>

And this HTML is inserted to the element with id +group_sel+.

=== using the compiled code

  Amrita::TemplateFileWithCache::set_cache_dir(CACHE_PATH)
  tmpl = Amrita::TemplateFileWithCache[TEMPLATE_PATH]
  tmpl.use_compiler = true
  tmpl.expand($stdout, make_model_data(bm,group))

Amrita::TemplateFileWithCache is a kind of Amrita::TemplateFile that
can reuse compiled code stored in cache file.

If there is the cache data matches to +TEMPLATE_PATH+ in +CACHE_PATH+
and it is younger than template itself, amrita reuse the compiled code
automatically.

<em>CAUTION: be careful to prevent users to edit the cache file.</em>

Currently, amrita does not check the cache file weather it was created
by amrita nor unmodified . So if someone can edit it, he or she can
insert any dangerous code into it to be executed by amrita.

It's *YOUR* resposibility to protect the cache files from
crackers. Don't use <tt>TemplateFileWithCache::set_cache_dir</tt> if
you don't understand this.

---

== using amrita script as cgi

This is a viewer of bookmark written in amrita-script.

  :include: sample/cgi/bookmark.ams

How to run in apache.

* set <tt>AllowOverride FileInfo</tt> and <tt>Options ExecCGI</tt> to
  some +cgi-bin+ Directory directive in httpd.conf

* put bin/amshandler to that directory

* put .htaccess to that directory

    AddHandler amrita-script ams
    Action amrita-script /amrita/cgi-bin/amshandler

---

== using bookmark.rb under mod_ruby

sample/cgi/bookmark.rb is a script can run under mod_ruby.

  LoadModule ruby_module /usr/lib/apache/mod_ruby.so
  RubyRequire apache/ruby-run

  Alias /amrita/cgi-bin/ /home/tnaka/cvswork/amrita/sample/cgi/

  <Location /amrita/cgi-bin>
    Options ExecCGI
    SetHandler ruby-object
    RubyHandler Apache::RubyRun.instance

    SetEnv AmritaCacheDir /tmp/bookmark # be careful
  </Location>

---

== using amrita-script under mod_ruby

   LoadModule ruby_module /usr/lib/apache/mod_ruby.so
   Alias /amrita/cgi-bin/ /home/tnaka/cvswork/amrita/sample/cgi/
   RubyRequire amrita/handlers
   SetEnv AmritaCacheDir /tmp/bookmark
   <Files *.ams>
     Options ExecCGI
     SetHandler ruby-object
     RubyHandler Amrita::AmsHandler.instance
   </Files>


---

== X

=== code and output

code:

output:

=== description