Application structure, versioning, and environments the Andela way.

Use the command below to install it.pip install autoenvCreate a file called .env whose function is to store all the commands that run the app, define the environment to be used, the secret keys and the urls, i.e., database urls.The autoactivation of the venv is done by running the commands below, that is after autovenv is installed.$ echo "source `which activate.sh`" >> ~/.bashrc$ source ~/.bashrcAlternatively, without installing the autovenv, you can manually activate the virtual environment by running the code below in your terminal when in the root directory.cd venv/Scripts/activateThe following are basically the content of a .env file which is created in the root directory.source env/bin/activate (this line activates the virtual env)export FLASK_APP="run.py" (this exports the flask application)export SECRET="anything that you want" (exports the secret key)export APP_SETTINGS="development" (exports the app settings)export DATABASE_URL="your database url" (the DB url)At this point, your root directory should contain a .env file as part of the structure, and you should be able to activate your virtual environment..When activated, the name of the venv will appear at the beginning of the lines in the terminal as shown below.(venv) C:UserssimonDesktopStackoverflow-lite-APIs>(???? ???? Am using windows btw: among the few programmers who feel comfortable coding with windows. So don’t get confused with the above line.)This what you should be having as we speak…wait are we speaking?.????.????.????.(don’t mind me with the emojis and the humor, they ease the coding for me ????Step 5: Set up your configurations (Important but not very necessary for version 1)I know when I say important but not necessary you will ignore because you are probably tired..DO NOT IGNORE it; it will save your time later as you proceed..This is the simplest since it is universal and the lines of code are almost the same.Python or Flask requires configurations to determine which environment should the app run in (development, production or testing) and the debug mode (True)..All these are well defined for Flask in the configuration file universally called .config which we had already created in the instance subfolder.Below are the settings and their brief explanation..Ensure that you research more so that you can be able to explain what every single line of code does.import os """We import the OS since it is from the OS that we have set all the requrements in the venv."""class Config(object): """Parent configuration class.""" DEBUG = False SECRET = os.getenv('SECRET') """gets the secret key set in the .env file""" DATABASE_URI = os.getenv('DATABASE_URL')class DevelopmentConfig(Config): """Configurations for Development.""" DEBUG = Trueclass TestingConfig(Config): """Configurations for Testing, with a separate test database.""" TESTING = True DATABASE_URI = 'testing URL for the test DB' DEBUG = Trueclass StagingConfig(Config): """Configurations for Staging.""" DEBUG = Trueclass ProductionConfig(Config): """Configurations for Production.""" DEBUG = False TESTING = Falseapp_config = { 'development': DevelopmentConfig, 'testing': TestingConfig, 'staging': StagingConfig, 'production': ProductionConfig,}The first class Config contains the general settings that all other environments will be inheriting..The app config dictionary app_config exports the defined environments which you will use later..Research and read more on what all these codes mean as you will be asked to explain by the LFAs later ( ???? ???? don't get scared).Step 6: Set up the serverThis is the last step where we want to make sure that our app structure is okay and that the server can run.. More details

Leave a Reply