| 1 | #!/usr/bin/python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | import sys, traceback |
| 5 | |
| 6 | # configuration (codes d'accès à la base MS-SQL) |
| 7 | sys.path.append('/home/thomas/public_html/') |
| 8 | import rest_config |
| 9 | |
| 10 | # pour savoir quel objet interroger : Routes |
| 11 | from routes import Mapper |
| 12 | from routes.middleware import RoutesMiddleware |
| 13 | |
| 14 | # Gestion des erreurs |
| 15 | from jinja.exceptions import TemplateNotFound |
| 16 | from objetsql import ObjetSQLInconnuError |
| 17 | |
| 18 | # TODO : ajouter un middleware de cache (beaker, basé sur REQUEST_URI quand la methode est GET) |
| 19 | # from beaker.middleware import CacheMiddleware |
| 20 | |
| 21 | # formats de sortie autorisés, et content-type correspondant |
| 22 | # formats = { 'xml': 'application/xml', 'html': 'text/html', 'txt': 'text/plain', 'json': 'application/json', 'rss': 'application/rss+xml' } |
| 23 | formats = { 'xml': 'application/xml', 'html': 'text/html', 'txt': 'text/plain', 'json': 'text/plain', 'rss': 'application/rss+xml' } # pour debug : json en text/plain |
| 24 | |
| 25 | |
| 26 | # les routes RESTful (cf http://routes.groovie.org/manual.html#restful-services) |
| 27 | mapper = Mapper() |
| 28 | mapper.resource('demlog','demlog') |
| 29 | mapper.resource('comlog','comlog') |
| 30 | mapper.resource('demdep','demdep') |
| 31 | mapper.resource('comdep','comdep') |
| 32 | mapper.resource('dempub','dempub') |
| 33 | mapper.resource('compub','compub') |
| 34 | mapper.resource('comare','comare') |
| 35 | mapper.resource('comarei','comare:(impl)',controller='comare') # pour les comarexxx où xxx est un code d'implantation |
| 36 | mapper.resource('comsre','comsre') |
| 37 | mapper.resource('comsrei','comsre:(impl)',controller='comsre') |
| 38 | mapper.resource('comxre','comxre') # comxre = comare + comsre |
| 39 | mapper.resource('comxrei','comxre:(impl)',controller='comxre') |
| 40 | mapper.resource('dem','dem') |
| 41 | mapper.resource('com','com') |
| 42 | mapper.resource('utilisateur','utilisateur') |
| 43 | |
| 44 | # objets disponibles |
| 45 | from document import demlog, comlog, demdep, comdep, dempub, compub, comare, comsre, comxre, dem, com |
| 46 | from utilisateur import utilisateur |
| 47 | |
| 48 | def dispatcher(environ, start_response): |
| 49 | """dispatch vers la bonne methode du bon objet, et retour WSGI""" |
| 50 | results = environ['wsgiorg.routing_args'][1] # résultat du middleware Routes |
| 51 | try: |
| 52 | # On cherche l'objet puis la méthode |
| 53 | target_class = globals()[results['controller']] |
| 54 | method = getattr(target_class,results['action']) |
| 55 | except: |
| 56 | # Si erreur pendant la recherche, on renvoie un 501 |
| 57 | start_response("501 Not Implemented", [('Content-type', 'text/html')]) |
| 58 | return '<html><body><h2>501 objet ou action invalide</h2></body></html>' # <pre>%s: %s\n%s</pre></body></html>' % ( sys.exc_info()[0] , sys.exc_info()[1] , traceback.format_exc()) |
| 59 | try: |
| 60 | # On lance la méthode et on renvoie le résultat |
| 61 | type, output = method(target_class(environ)) |
| 62 | start_response("200 OK", [('Content-type', formats[type])]) |
| 63 | return output.encode('utf-8') |
| 64 | # gestion des problèmes possibles pendant l'exécution |
| 65 | except ObjetSQLInconnuError, type: |
| 66 | start_response("404 Not Found", [('Content-type', 'text/html')]) |
| 67 | return '<html><body><h2>404 %s inexistant</h2></body></html>' % type |
| 68 | except TemplateNotFound, template: |
| 69 | start_response("415 Unsupported Media Type", [('Content-type', 'text/html')]) |
| 70 | return '<html><body><h2>415 format non supporté (%s inexistant)</h2></body></html>' % template |
| 71 | except: |
| 72 | start_response("500 INTERNAL ERROR", [('Content-type', 'text/html')]) |
| 73 | return '<html><body><h2>500 erreur lors du traitement</h2><pre>%s: %s\n%s</pre></body></html>' % ( sys.exc_info()[0] , sys.exc_info()[1] , traceback.format_exc()) |
| 74 | |
| 75 | |
| 76 | # application() sera lancée par mod_wsgi : on route et on dispatche |
| 77 | application = RoutesMiddleware( dispatcher, mapper ) |
| 78 | |
| 79 | |