Changeset 5000

Show
Ignore:
Timestamp:
07/18/08 19:38:00 (4 months ago)
Author:
mramm
Message:

Updated PyAMF example to use the new use_wsgi_app function.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/docs/main/TGandPyAMF.rst

    r4863 r5000  
    4444---------------------------- 
    4545 
    46 Now, you're ready to start creating a PyAMF gateway for your Flex app.  The first thing to do is to create a new gateway.py file in pyamftest/pyamftest/controllers When you've got that file created, add the following contents:: 
     46Now, you're ready to start creating a PyAMF gateway for your Flex app.  The  
     47first thing to do is to create a new gateway.py file wherever you want it:: 
    4748 
    4849 from pyamf.remoting.gateway.wsgi import WSGIGateway 
     
    6970 GatewayController = WSGIGateway(services) 
    7071 
    71 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.  
     72This sets up a GatewayController WSGI app that has three services that  
     73can be called from flex: echo, sum, and scramble, which each do exactly what  
     74they say they do.  
    7275 
    73 Setup a Route to your services
    74 ----------------------------------- 
     76Setup a controller that uses the GatewayController WSGI app
     77--------------------------------------------------------------- 
    7578 
    76 Now that we have a service, we can modify pyamftest/pyamftest/config/environment.py to have a route to the GatewayController.  
     79In root.py just add a method that delegates to the wsgi app:  
    7780 
    78 This requires creating a custom overide for the standard TG2 object dispatch.  See http://docs.turbogears.org/2.0/RoutesIntegration for more information.  
     81@expose() 
     82def simple(self, **kwargs): 
     83    return use_wsgi_app(GatewayController) 
    7984 
    80 Basically you need to set up custom route_map function, that has a map.connect() function which sets a link up the URL you want to use for your service, and  and the WSGI controller you want to call for that URL:: 
    8185 
    82   map.connect('gateway', controller = 'gateway') 
     86You'll need to import use_wsgi_app from tg, and your GatewayController from  
     87wherever you put it.  
    8388 
    84 If you want to you can just copy and paste this in as a replacement for your environment.py file:: 
    85  
    86     """TurboGears environment configuration""" 
    87     import os 
    88  
    89     from pylons import config 
    90  
    91     from pylons.i18n import ugettext 
    92     from genshi.filters import Translator 
    93     from tg import setup 
    94     from sqlalchemy import engine_from_config 
    95  
    96     import pyamftest.lib.app_globals as app_globals 
    97  
    98     from routes import Mapper  ##### Add this line ######### 
    99  
    100     def make_map(): 
    101         """Create, configure and return the routes Mapper""" 
    102         map = Mapper(directory=config['pylons.paths']['controllers'], 
    103                      always_scan=config['debug']) 
    104     
    105         # This route connects your root controller 
    106         map.connect('gateway', controller = 'gateway')  ####### Add this line ######## 
    107         map.connect('*url', controller='root', action='route') 
    108     
    109         # The ErrorController route (handles 404/500 error pages); it should 
    110         # likely stay at the top, ensuring it can always be resolved 
    111         map.connect('error/:action/:id', controller='error') 
    112  
    113         # CUSTOM ROUTES HERE 
    114         # map.connect(':controller/:action/:id') 
    115         map.connect('*url', controller='template', action='view') 
    116  
    117         return map 
    118  
    119     def template_loaded(template): 
    120         "Plug-in our i18n function to Genshi." 
    121         template.filters.insert(0, Translator(ugettext)) 
    122  
    123     def load_environment(global_conf, app_conf): 
    124         """Configure the Pylons environment via the ``pylons.config`` 
    125         object 
    126         """ 
    127         # Pylons paths 
    128         root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
    129         paths = dict(root=root, 
    130                      controllers=os.path.join(root, 'controllers'), 
    131                      static_files=os.path.join(root, 'public'), 
    132                      templates=[os.path.join(root, 'templates')]) 
    133     
    134         # This setups up a set of default route that enables a standard 
    135         # TG2 style object dispatch.   Fell free to overide it with 
    136         # custom routes.  TODO: Link to TG2+routes doc. 
    137         
    138         # Initialize config with the basic options 
    139         config.init_app(global_conf, app_conf, package='pyamftest', 
    140                         template_engine='genshi', paths=paths) 
    141         config['routes.map'] = make_map()                
    142         config['pylons.g'] = app_globals.Globals() 
    143         config['pylons.g'].sa_engine = engine_from_config(config, 'sqlalchemy.') 
    144  
    145         # Customize templating options via this variable 
    146         tmpl_options = config['buffet.template_options'] 
    147         tmpl_options['genshi.loader_callback'] = template_loaded 
    148  
    149         # CONFIGURATION OPTIONS HERE (note: all config options will override 
    150         # any Pylons config options) 
    151  
    152         from pyamftest import model 
    153         model.DBSession.configure(bind=config['pylons.g'].sa_engine) 
    154         model.metadata.bind = config['pylons.g'].sa_engine 
    15589    
    15690Create a Flex Client