**************
  TurboGears 
**************

.. image:: images/turbogears-logo.png


.. topic:: Introduction

    PyAMF provides a simple WSGI Application that can be used to setup RPC
    style service easily in Python. Because
    `TurboGears2 <http://turbogears.org>`_ supports WSGI from top-to-bottom,
    it's very simple to setup a TG2 app that contains web-services for your
    Flex and Flash applications.


Install TurboGears
==================

If you haven't installed
`TG2 <http://docs.turbogears.org/2.0/main/DownloadInstall>`_, you'll need
to do that first. After that's done, you can create a new TG2 project in the
normal way::

  paster quickstart pyamftest
  cd pyamftest
  paster serve development.ini --reload

Your project should now be started, and you should be able to browse to it
at http://127.0.0.1:8080.


Creating the PyAMF gateway
==========================

Now, you're ready to start creating a PyAMF gateway for your Flex app. The 
first thing to do is to create a new ``mygateway.py`` file wherever you want
it:

.. literalinclude:: ../examples/gateways/turbogears/mygateway.py
   :linenos:

This sets up a GatewayController WSGI app that has three services that 
can be called from Flex: ``echo``, ``sum``, and ``scramble``, which each do
exactly what they say they do. 


Setup a controller
==================

Then you can import your ``GatewayController`` into ``root.py``::

    from tg import use_wsgi_app 
    from mygateway import GatewayController

Now all you have to do is add a method that delegates to the wsgi app:: 

  @expose()
  def gateway(self, *args, **kwargs):
      return use_wsgi_app(GatewayController)

Of course, you'll need to import ``use_wsgi_app`` from ``tg``, and your 
``GatewayController`` from wherever you put it. But once you've done those 
things you'll have a AMF Gateway mounted at ``/gateway`` which you can approach
from Flex. 

   
Create a Flex Client
====================

Now we're ready for the big time event, we can create a brand new Flex client
which talks to our TG2 hosted PyAMF services. This little tutorial pretty much
assumes that you know how to use Flex and just want to see how to connect it to
a TurboGears app.

Here's the MXML:

.. literalinclude:: ../examples/gateways/turbogears/app.mxml
   :language: xml
   :linenos:

You can paste that into a new Flex Builder project (or use the free SDK to create
a project with the text editor of your choice).  You can then put the HTML and SWF
files generated by Flex Builder into your TG2 project's static directory (wherever
you want them to be available) at which point you should be able to browse there,
get your Flex app, and use it to connect to the web services you just created. 
