Search code examples
flaskiisconfigurationweb-configweb-deployment

Difference between config.py and web.config file when deploying Flask app to Windows IIS


Context Information

I am developing a flask web application at work. Due to existing web infrastructure of my company, I have to deploy the flask app on Windows Server via IIS.

For the development process I worked with the normal Flask Werkzeug development server on my local machine. During the development process, a config.py file (please see below) was created.

Now I am deploying the application to production on IIS. There, a web.config file is necessary for the IIS server to interpret the python code of my app (I am using FastCGI via wfastcgi python module).

Question

I now have two files in my IIS production server app directory that I am not sure of how they interact or relate: config.py and web.config.

Can someone explain to me the differences of those files and if config.py remains relevant for my app in a IIS production environment?


Code

config.py

import os 
from dotenv import load_dotenv

basedir = os.path.abspath(os.path.dirname(__name__))
load_dotenv(os.path.join(basedir, '.env'))

# Create the super class
class Config(object):
   SECRET_KEY = os.environ.get('SECRET_KEY')
   SQLALCHEMY_COMMIT_ON_TEARDOWN = True
   SQLALCHEMY_TRACK_MODIFICATIONS = False

# Create the development config
class DevelopmentConfig(Config):
   DEBUG = True
   

# Create the testing config
class TestingConfig(Config):
   DEBUG = False
   TESTING = True


# create the production config
class ProductionConfig(Config):
   DEBUG = False
   TESTING = False

web.config file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="FastCGI Handler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="f:\apps\venv\scripts\python.exe|f:\apps\venv\lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
        </handlers>
    </system.webServer>
</configuration>

Solution

  • A config.py file contains parameters and initial settings for computer programs. Importing a config.py file allows the variables and functions in the config.py file to be used in the current program. This is the main configuration file, and should not be modified. It can be used as a reference for configuration settings.

    The web.config is a file that is read by IIS to configure an app hosted with IIS, and web.config is used for ASP.NET Web Projects/Web Services. web.config by default has several configurations required for the web application. It is also called Application Level Configuration File and inherits setting from the machine.config file.