-
-
Notifications
You must be signed in to change notification settings - Fork 951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to setup configuration of application #153
Comments
Sorry for missing this question... just came across it while trying to get the Falcon issue tracker organized! In case the info is useful to anyone, I'll just mention that Falcon isn't opinionated about how you configure your app; you are free to use ConfigParser, or directly load a JSON/YAML/TOML file. You can have a module that exposes a (get_config()) call and memoizes on first use to consolidate config handling in one place without relying on global logic / variables. |
Has there been consideration of having a config dictionary on the application, a la Flask? I will admit it comes in handy there. |
@jasonab there is nothing to say you can't put a api = falcon.API()
api.config = load_config() |
Isn't there? The api object has no __dict__ attribute and you can't assign additional attributes to it due to the use of __slots__. |
It seems this doubt has been around for a while and I think @KennethLim314 is correctthat we can't add to the App class because of the |
Historically, I've often just inherited from API and put it on there since as I needed to build other things (such as signal handling) around the API anyhow. class MyCustomAPI(falcon.API):
def __init__(self, cfg):
self.cfg = cfg
# --- snip --- Likewise when creating resource classes or middleware, I often pass in the cfg and usually something to handle db session management. If you would prefer it to be on a request, you can add it to your request from a middleware using the context capabilities: https://falcon.readthedocs.io/en/stable/api/request_and_response.html#falcon.Request.context |
@jmvrbanac Thanks for the explanation. The idea behind it fits my needs :) |
Adding to what @jmvrbanac said, in some simplistic scenarios you may get away with just passing the configuration object down to resources and middleware, i.e., as showcased by above, but without even storing it on the As also covered in our FAQ, another popular way is making configuration available via a module that can be imported anywhere. Just make sure it's not loaded and cached upon import, but only when needed. I've gone this way at work, it does the job too. The main advantage is simplicity (accessible everywhere). Although using global stuff is usually not the best practice, but it's a lesser sin for things that are global, like configuration and logging. |
I'm wondering how I can set environmental configurations in falcon framework.
Does someone tell me how to do it.
The text was updated successfully, but these errors were encountered: