2017-02-26 8 views
-2

FlaskアプリケーションがHTTPまたはHTTPSで実行されているかどうかによって、Flaskアプリケーションを異なる方法で設定することが望まれます。flask_request.is_secureをcreate_app()内で使用する

次のコードは、RuntimeErrorを発生させます。

どのようにしてFlaskを動的に設定しますか?

デプロイするたびに、さまざまな設定を手動で変更するのは苦労します。自動解決が望ましいFlaskには、実行されるまでどのマシンが実行されているかを知るメカニズムがないというのは奇妙なことです。多分flask.requestは間違った方法です。

私はthis documentationthis SO postを読みました。

from flask import Flask, request 


def create_app(): 
    app = Flask(__name__) 
    app.config['DEBUG'] = not request.is_secure  
    return app 


# Initialize App 
app = create_app() 

エラーの詳細。

RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed an active HTTP request. Consult the documentation on testing for information about how to avoid this problem.

flask config docsに基づくもう1つの試行。同じRuntimeErrorを受け取りました。ダニエルローズマンとして

# Configuration Options 
class Config(object): 
    DEBUG = False 
    TESTING = False 
    FLASKS3_BUCKET_NAME = 'nueverest' 
    FLASKS3_USE_HTTPS = True 
    USE_S3_DEBUG = False 


class Production(Config): 
    pass 


class Development(Config): 
    DEBUG = True 
    USE_S3_DEBUG = True 


app = Flask(__name__)   # Initialize Application 

if request.is_secure:    # Select Configuration 
    app.config.from_object(Production) 
else: 
    app.config.from_object(Development) 
+0

? –

+0

意味があります。リクエストオブジェクトの仕組みを完全に理解できませんでした。 –

+0

@PJサントロそのリンクはすでに質問で提供されていました。 –

答えて

0

要求が最初に(鶏と卵)を発生しなければならないので、flask.requestを使用することができない、説明しました。

Solution: Use uuid.getnode() to uniquely identify which machine the code is running on.

このスケーラビリティを実現するには、各開発者が独自のsecrets.pyファイルを作成する必要があります。ファイルは、各開発者に固有のものであるため、VCSに追加しないでください。それはこのように見えます。

import uuid 


def is_production(): 
    """ Determines if app is running on the production server via uuid comparison. 

    HOW TO USE: 
    Open a terminal 
    > python 
    > import uuid 
    > uuid.getnode() 
    12345678987654321 <-- Copy whatever is returned and replace 111111111 with this. 

    Compare uuid for the machine against the known uuid(s) of your development machine(s). 
    :return: (bool) True if code is running on the production server, and False otherwise. 
    """ 
    developer_machines = [111111111, ] 
    return uuid.getnode() not in developer_machines 

app.pyはsecrets.is_production()をインポートし、このようになります。ご要望があった前にリクエストオブジェクトにアクセスすることを期待することができますどのように

from flask import Flask 
from secrets import is_production 


def create_app(): 
    app = Flask(__name__) 
    app.config['DEBUG'] = not is_production() 
    return app 


# Initialize App 
app = create_app() 

Scalability: Each developer manages their own secrets.py file. This is scalable to any number of developers, and does not require the use of Environment Variables which can complicate testing, development, and deployment.

+0

これは2つの異なるマシン上の2人の開発者が実行しようとするとすぐには期待どおりに動作しません。解決策は既に上記にリンクされているように、devの設定を使用することです。 – davidism

+0

@davidismこのソリューションは、複数の開発者のケースを解決すると主張していません。しかし、複数の開発者uuidが必要な場合は、[124987、219847、2398047、...]の 'uuid.getnode() 'を使うことができます。 –

+0

すべての開発マシンをハードコーディングすることはスケーラブルではありません。これらのフープを飛び越える理由は全くありません。 – davidism

関連する問題