Crossbar.io为Django app配置WSGI

时间:2015-08-10 20:14:22

标签: django wsgi crossbar

我正在尝试使用Crossbar.io 0.10.4和Django 1.6.11,尝试关注example here。代码显示您可以配置Crossbar.io以在" /"中提供Django应用程序。 - 但是当我在配置中尝试时,我收到Python导入错误:

ApplicationError: ApplicationError('crossbar.error.invalid_configuration', args = (u"WSGI app module 'apache/django.wsgi' import failed: Import by filename is not supported. - Python search path was ....

我的config.json在这里:

{
   "controller": {
   },
   "workers": [
      {
         "type": "router",
         "realms": [
            {
               "name": "backstage-producer",
               "roles": [
                  {
                     "name": "anonymous",
                     "permissions": [
                        {
                           "uri": "*",
                           "publish": false,
                           "subscribe": true,
                           "call": false,
                           "register": false
                        }
                     ]
                  }
               ]
            }
         ],
         "transports": [
            {
               "type": "web",
               "endpoint": {
                  "type": "tcp",
                  "port": 8080
               },
               "paths": {
                   "/": {
                     "type": "wsgi",
                     "module": "apache/django.wsgi",
                     "object": "application"
                  },
                  "ws": {
                     "type": "websocket",
                     "debug": false
                  },
                  "notify": {
                     "type": "publisher",
                     "realm": "backstage-producer",
                     "role": "anonymous"
                  },
                  "static": {
                     "type": "static",
                     "directory": "../static"
                  }
               }
            }
         ]
      }
   ]
}

在搜索的Python路径中,包含我的Django项目目录。通常我将我的特定项目目录附加到我的wsgi文件中的sys.path,但显然工作流程不适用于Crossbar.io。尝试相对导入失败(需要指定" package"参数)和完整路径(与文件名错误导入相同)。

删除" /"的定义不起作用,因为Crossbar.io抱怨必须定义它。

如何使用Crossbar.io正确设置?我的apache / django.wsgi文件如下:参考:

ALLDIRS = ['/usr/local/pythonenv/myapp/lib/python2.6/site-packages']

import os
import sys
import site

# from https://code.google.com/p/modwsgi/wiki/VirtualEnvironments

sys.path.insert(0, '/var/www/myapp/myapp_main/')
sys.path.insert(1, '/var/www/myapp/')

prev_sys_path = list(sys.path)
for directory in ALLDIRS:
    site.addsitedir(directory)

new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)

sys.path[:0] = new_sys_path

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp_main.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

----更新1 ------

根据Daniel的建议,我将文件更改为wsgi.py并将我的配置更改为使用Python模块路径,而不是文件名/目录路径。 Config然后看起来像这样:

"paths": {
       "/": {
         "type": "wsgi",
         "module": "apache.wsgi",
         "object": "application"
      },

引发同样的例外:

ApplicationError: ApplicationError('crossbar.error.invalid_configuration', args = (u"WSGI app module 'apache.wsgi' import failed: No module named apache.wsgi - Python search path was

我的目录结构是:

  Project
    |- apache
    |     |-__init__.py
    |     |-wsgi.py
    |-.crossbar
          |-config.json 

-------更新2 -------

唯一的解决方案(阅读" hack")我发现将我的项目路径硬编码到crossbar/worker/router.py中,以便它包含在Python搜索路径列表中:

sys.path.insert(0, '/var/www/myapp/myapp_main/')
sys.path.insert(1, '/var/www/myapp/')

似乎应该有更好的方式...

1 个答案:

答案 0 :(得分:3)

错误告诉您在设置中有一个指向WSGI文件的文件路径,而您需要一个Python模块路径。您的WSGI文件实际上应该是项目目录中名为“wsgi.py”的文件(可能是“apache”,这是一个明确不使用Apache的项目的奇怪名称。)

               "/": {
                 "type": "wsgi",
                 "module": "apache.wsgi",
                 "object": "application"
              },

更新所以我最终找到了config docs:他们真的不想让它变得简单,就像实际提供索引一样。哦,好吧。

看起来您可以为路由器配置提供options哈希,包括pythonpath设置:

"workers": [
      {
         "type": "router",
         "options": {
             "pythonpath": ["/var/myapp/myapp_main/", "/var/myapp"]
         },
         ...
         "transports": {
             ...