WebStack

docs/resources.html

396:370deefd28f4
2005-07-16 paulb [project @ 2005-07-16 18:37:44 by paulb] Added directory redirects to the ResourceMap.MapResource.
     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">     2 <html xmlns="http://www.w3.org/1999/xhtml">     3 <head>     4   <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" />     5   <title>Applications and Resources</title>     6   <meta name="generator"     7  content="amaya 8.1a, see http://www.w3.org/Amaya/" />     8   <link href="styles.css" rel="stylesheet" type="text/css" />     9 </head>    10 <body>    11 <h1>Applications and Resources</h1>    12 At its simplest a WebStack application is just&nbsp;a single Python    13 class that we call a "resource". This class can be defined inside a    14 normal Python module or package, so let us start by doing the following:<br />    15 <ol>    16   <li>Create a new directory for    17 our application; choose any name since we just want an empty space in    18 which to put new files.</li>    19   <li>Create a file called <code>MyApplication.py</code>    20 - this is our module.</li>    21 </ol>    22 We are going to call our resource&nbsp;<code>MyResource</code>    23 and in principle it will have a structure that looks like this:    24 <pre>class MyResource:<br />    def respond(self, trans):<br />        [Examine the transaction, decide what the user wants to do.]<br />        [Perform some kind of action with the information supplied.]<br />        [Produce some kind of response which tells the user what happened.]</pre>    25 <p>It is in this kind of resource    26 class that we write the    27 actual application code or at least the beginnings of it. When a user    28 of the application sends us a request,    29 the <code>respond</code>&nbsp;method    30 will be called and the code    31 within it executed. The parts of the pseudo-code in    32 the above text which aren't valid Python    33 (ie. the bits in square brackets) will, when we have written them, use    34 the&nbsp;<code>trans</code>    35 object to find out what any given user of the application has sent us,    36 and to send information back    37 to the    38 user in response.</p>    39 <h2>Starting Simple</h2>    40 <p>The simplest way to turn this    41 into a working application is to    42 ignore the first two activities mentioned in the pseudo-code and just    43 to produce some kind of    44 response. Here is how we can make our application do something:</p>    45 <ol>    46   <li>Edit the&nbsp;module    47 file&nbsp;<code>MyApplication.py</code>.</li>    48   <li>Write into it the following    49 code which defines&nbsp;<code>MyResource</code>:</li>    50 </ol>    51 <pre>class MyResource:<br />   &nbsp;def respond(self, trans):<br />        out = trans.get_response_stream()<br />        print &gt;&gt;out, "Hello world."</pre>    52 <h2>Testing the Resource</h2>    53 <p>To test this resource we need to deploy it, and to do that we need    54 an    55 adapter. Here is a quick way of writing an adapter and testing this    56 code:</p>    57 <ol>    58   <li> Create a&nbsp;file called&nbsp;<code>MyAdapter.py</code> - you    59 can choose another name if you want - this will be where the adapter    60 code lives.</li>    61   <li>Copy the example adapter in <a href="deploying.html">"Deploying    62 a WebStack Application"</a> and write it into&nbsp;<code>MyAdapter.py</code>.</li>    63   <li>Now, with two files in your directory, <code>MyApplication.py</code>    64 and <code>MyAdapter.py</code>, you may run&nbsp;<code>MyAdapter.py</code>    65 as follows:</li>    66 </ol>    67 <pre>python MyAdapter.py</pre>    68 <p>This should start the adapter program and print the following    69 message:</p>    70 <pre>Serving...</pre>    71 <p>You should now be able to visit&nbsp;<code>http://localhost:8080</code>    72 in your    73 browser and see the message printed by your application:</p>    74 <pre>Hello world.</pre>    75 <h2>About Resources</h2>    76 <ul>    77   <li><a href="resource-creation.html">How Resources are Created</a></li>    78   <li><a href="design.html">Application Design Considerations</a></li>    79 </ul>    80 </body>    81 </html>