Flask Restful API中的某些路由无法正常工作

时间:2014-01-27 19:48:27

标签: python apache flask mod-wsgi flask-restful

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

Apache配置:

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

在bulkView

所以这里的事情变得有趣,如果我向这条路线发送一个get请求,我会得到一个方法不允许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})

在branchView

请求关于分支404s的任一路由。检查了文件的权限,尝试将类拆分为单独的文件。不知道出了什么问题:(

1 个答案:

答案 0 :(得分:0)

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