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