diff --git a/gunicorn.conf.py b/gunicorn.conf.py new file mode 100644 index 00000000000..427c76764de --- /dev/null +++ b/gunicorn.conf.py @@ -0,0 +1,7 @@ +import openerp +# Standard OpenERP XML-RPC port. +bind = '127.0.0.1:8069' +# Some application-wide initialization is needed. +on_starting = openerp.wsgi.on_starting +# This is the big TODO: safely use more than a single worker. +workers = 1 diff --git a/openerp/__init__.py b/openerp/__init__.py index b7b5fc1b533..9026c7be90f 100644 --- a/openerp/__init__.py +++ b/openerp/__init__.py @@ -41,6 +41,7 @@ import tiny_socket import tools import wizard import workflow +import wsgi # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/openerp/wsgi.py b/openerp/wsgi.py new file mode 100644 index 00000000000..7c52dd3db46 --- /dev/null +++ b/openerp/wsgi.py @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2011 OpenERP s.a. (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +""" WSGI stuffs (proof of concept for now) + +This module offers an WSGI interface to OpenERP. + +""" + +from wsgiref.simple_server import make_server +from SimpleXMLRPCServer import SimpleXMLRPCDispatcher +import xmlrpclib + +import sys + +import openerp +import openerp.tools.config as config + +def wsgi_xmlrpc(environ, start_response): + if environ['REQUEST_METHOD'] == 'POST' and environ['PATH_INFO'].startswith('/xmlrpc/'): + length = int(environ['CONTENT_LENGTH']) + data = environ['wsgi.input'].read(length) + path = environ['PATH_INFO'][len('/xmlrpc/'):] # expected to be one of db, object, ... TODO should we expect a possible trailing '/' ? + + # TODO see SimpleXMLRPCDispatcher._marshaled_dispatch() for some necessary handling. + # TODO see OpenERPDispatcher for some othe handling (in particular, auth things). + params, method = xmlrpclib.loads(data) + result = openerp.netsvc.ExportService.getService(path).dispatch(method, None, params) + response = xmlrpclib.dumps((result,), methodresponse=1, allow_none=False, encoding=None) + + start_response("200 OK", [('Content-Type','text/xml'), ('Content-Length', str(len(response)))]) + return [response] + +def wsgi_jsonrpc(environ, start_response): + pass + +def application(environ, start_response): + + # Try all handlers until one returns some result (i.e. not None). + wsgi_handlers = [wsgi_xmlrpc, wsgi_jsonrpc] + for handler in wsgi_handlers: + result = handler(environ, start_response) + if result is None: + continue + return result + + # We never returned from the loop. + else: + response = 'No handler found.\n' + start_response('200 OK', [('Content-Type', 'text/plain'), ('Content-Length', str(len(response)))]) + return [response] + +# Serve XMLRPC via wsgiref's simple_server. +# Blocking, should probably be called in its own process. +def serve(): + httpd = make_server('localhost', config['xmlrpc_port'], application) + httpd.serve_forever() + +# Application setup be fore we can spawn any worker process. +# This is suitable for e.g. gunicorn's on_starting hook. +def on_starting(server): + config = openerp.tools.config + config['addons_path'] = '/home/openerp/repos/addons/trunk/' # need a config file + openerp.netsvc.init_logger() + openerp.osv.osv.start_object_proxy() + openerp.service.web_services.start_web_services() + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: