1

我使用 Twilio 的 Restful 扩展在 Flask 中构建了一个 API。一切都在 Flask 的开发服务器上完美运行。但是,一旦我将应用程序移至 Apache 和 mod_wsgi,一些路由停止工作

阿帕奇配置:

Listen 1337
<VirtualHost *:1337>
        ServerName layer
        ServerAdmin webmaster@localhost                                                                              

        DocumentRoot /var/www/layer
        WSGIDaemonProcess layer user=this_is_me group=www-data threads=5
        WSGIScriptAlias / /var/www/layer/app.wsgi
        WSGIScriptReloading On
<Directory /var/www/layer>
        WSGIProcessGroup layer
        WSGIApplicationGroup %{GLOBAL}%
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order deny,allow
            Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel info
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

在 app.wsgi 中:

from src import app

application = app.create_app()

在 app.py 中:

#!flask/bin/python

from flask import Flask, request, jsonify, Response
from flask.ext.restful import Resource, Api
from view import treeView, createBranchView, branchView, lineView, bulkView

def create_app():
app = Flask(__name__)
api = Api(app, catch_all_404s=True)

logging.basicConfig(filename=os.path.abspath('exlayer.log'), level=logging.DEBUG)

#Some stand alone routes
@app.route('/')

def index():
         ### Some code here ###
         return jsonify({'status': 200, 'success':True})

@app.route('/create/', methods = ['POST'])
def create_tree():
          ### Some more code ### 
      return jsonify(dict(status=200, success=True))

## The rest of the routes
api.add_resource(treeView.dataTree, '/<tree_name>/')
api.add_resource(lineView.lineData, '/<tree_name>/line/<line_id>/')
api.add_resource(bulkView.bulkData, '/<tree_name>/bulk/<bulk_id>/')
api.add_resource(createBranchView.createBranch, '/<tree_name>/branch/')
api.add_resource(branchView.branchData, '/<tree_name>/branch/<branch_identifier>/')

app.config.from_pyfile('../config/config.py')

return app

批量查看

所以这就是事情变得有趣的地方,如果我向这条路线发送一个 get 请求,我会收到一个 Method Not Allowed 405 错误。如果我发送删除请求,它工作正常。几乎完全相同的代码在 lineView 中运行没有问题。

from flask import Flask, request, jsonify, Response
from flask.ext.restful import Resource, Api
from src import helper

class bulkData(Resource):
def get(self, tree_name, bulk_id):
    ## Some code here ##
    return jsonify({'status': 200, 'success':True})

def delete(self, tree_name, bulk_id):
    ## Some code here ##
    return jsonify({'status': 200, 'success':True})

在分支视图中

对涉及分支 404 的任一路由的请求。检查文件的权限,尝试将类拆分为单独的文件。不知道怎么了:(

4

1 回答 1

0

使用 os.path.abspath() 和 ../config/config.py 都会导致问题,因为您的进程的工作目录不会是您的项目在 Apache 下的位置。计算相对于 os.path.dirname( file )的绝对路径。请参阅 mod_wsgi 文档:

于 2014-01-28T09:44:18.800 回答