To learn more about hosting your URL shortener app online, check out:
Setting Environment Variables
00:14 It makes sense to use different settings for different environments. Your local development environment may use a differently named database than an online production environment. To be flexible, you store this information in special variables that you can adjust for each environment. While you won’t take steps to host your app online in this course, you’ll build your app in a way that enables you to deploy easily to the cloud in the future. If you want to learn more about deployment, then check out Python Web Applications: Deploy Your Script as a Flask App or Deploying a Python Flask Example Application Using Heroku.
01:26 You only have to define the variables that you want to use, and pydantic takes care of the rest. In other words, pydantic will automatically assume those default values if it doesn’t find the corresponding environment variables.
01:54 It’s a good start to have these variables in place with default values, but since the values for your current environment, the domain of your app, and the address of your database are dependent on the environment that you are working in, you’ll load the values from external environment variables later on.
To show a message once your settings are loaded, you create
get_settings(). This function returns an instance of your
Settings class and will provide you with the option of caching your settings.
If you get an error when trying to run the
import command, make sure that your Python interpreter was started from within the project’s root directory. The settings are loaded correctly when you call
However, you can optimize the retrieval of your settings a bit more. Your app settings are not changed while running the app, but still you’re loading the settings over and over again every time you call
get_settings. You can take advantage of
get_settings() being a function to implement a Least Recently Used strategy.
03:26 When you start your app, it makes sense to load your settings and then cache the data. Caching is an optimization technique that you can use in your applications to keep recent or often-used data in memory.
Now you see the
Loading settings message only once. That means that your settings are successfully cached. By adding the
@lru_cache decorator, you made your app faster while decreasing the load on computing resources.
Another improvement that you’ll implement is loading external environment variables. Start by creating an external
.env file in the root directory of your project. And then add the content seen on-screen.
05:03 By storing your environment variables externally, you are following the twelve-factor app methodology. The twelve-factor app methodology states twelve principles to enable developers to build portable and scalable web applications.
05:21 One principle is to store the configuration of your app in the environment. This includes resource handles to the database, Memcached, and other backing services; credentials to external services such as Amazon S3 or Twitter; and per-deploy values such as the canonical hostname for the deployment.
The twelve-factor principles require strict separation of config from code. Config varies substantially across deployments, and code does not. It’s recommended to have different
.env files for different environments. Also, you should never add the
.env file to your version control system, as your environment, variables may store sensitive information.
Note that if you’re sharing your code with other developers, then you may want to show in your repository what their
.env files should look like. In that case, you can add
.env_sample to your version control system.
.env_sample, you can store the keys with placeholder values. To help yourself and your fellow developers, don’t forget to write instructions in your README file on how to rename
.env_sample and store the correct values in the file.
The configuration variables that you used in your
Settings class are a fallback for your external environment variables. In the
.env file, you are declaring the same variables for your development environment.
With all of these previous instructions followed, then the directory tree should look as seen on-screen. The
url_shortener_project/ directory contains the
shortener_app/ folder. In that folder, at the moment, there are two files, a
.__init__.py file and a
config.py file. In the
url_shortener_project/ folder, you’ll also have your virtual environment and the
.env file you created in the previous step.
Become a Member to join the conversation.