Creating the configuration

Before we can start our application, we need to configure our modules that we are going to use in our application. So, let's first go ahead and create the global configuration of our application by opening up config.py in our code editor and adding the following content to it:

'''
File: config.py
Description: Global configuration for Bugzot project
'''
DEBUG = False
SECRET_KEY = 'your_application_secret_key'
BCRYPT_LOG_ROUNDS = 5 # Increase this value as required for your application
SQLALCHEMY_DATABASE_URI = "sqlite:///bugzot.db"
SQLALCHEMY_ECHO = False
SESSION_TYPE = 'filesystem'
STATIC_PATH = 'bugzot/static'
TEMPLATES_PATH = 'bugzot/templates'

With this, we have drafted our global application configuration. Let's try to understand what each line means here.

First, we have set DEBUG = False, which tells Flask not to run in debug mode and hence not to print any back-traces of the application when an error occurs. This allows us to hide sensitive application information when running in production mode.

The next key, SECRET_KEY, provides a way to set a secret key, which will be used by the application to sign the application cookies.

Next key inline BCRYPT_LOG_ROUNDS configures the flask-bcrypt module to run n number of rounds for every input provided to generate a hash value.

The following two settings, namely, SQLALCHEMY_DATABASE_URI and SQLALCHEMY_ECHOconfigure the flask-sqlalchemy module by setting up how sqlalchemy should connect to the database and whether sqlalchemy should, or should not, print the SQL for the operations it is doing.

With sqlalchemy configured, we next move on to configure our flask sessions by setting SESSION_TYPE to filesystem. This tells the flask session modules to use filesystem-based sessions to manage user activity.

Lastly, in the configuration, we set up two more keys, STATIC_PATH and TEMPLATES_PATHwhich tell flask where to look for static files and template files respectively.

We are now done with setting up the global configuration of our application. We next move on to setting up the instance-specific configuration, where we define our unique application related keys. To do this, create a new file named config.py under the instance directory and add the following content to it:

'''
File: config.py
Description: Instance specific configuration for BugZot
'''
BCRYPT_LOG_ROUNDS=16
DEBUG=True
SECRET_KEY='hdu8463$$Jh#*@jdjsu937822i__#@*hsiud829aiuwnxhsi'
SQLALCHEMY_DATABASE_URI='postgresql://bugzot_admin:bugzotuser@localhost/bugzot'
SQLALCHEMY_ECHO=True

Here, we have enabled the debug mode of Flask because we are currently developing our application and any stack-trace can help us better understand the reason behind the errors. We have also defined a URI for sqlalchemy to use the postgresql based database, which we created earlier in our application, and have also enabled the printing of SQL statements by sqlalchemy module for the purpose of development.

With this we are all set to run our application. So, let's give it a try by issuing the following command inside our project root:

python run.py

On a successful execution, we should be getting the following message printed on our terminal:

/home/sbadhwar/book_example/Hands-On-Enterprise-Application-Development-with-Python/chapter6/bugzot/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
* Serving Flask app "bugzot.application" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
* Restarting with stat
/home/sbadhwar/book_example/Hands-On-Enterprise-Application-Development-with-Python/chapter6/bugzot/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py:794: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
* Debugger is active!
* Debugger PIN: 174-906-953

With this, we are now up and running with our application. But, we still can't do anything. If we go ahead and try to open the URL in our browser, we won't see anything, and will instead be greeted with an error. This happens because we still haven't defined any route inside our application that can be served up by Flask and which the user can see.

So, as a first step, let's define the first route of our application.

To do this, open up bugzot/application.py and add the following set of lines to the end of the file:

@app.route('/ping', methods=['GET'])
def ping():
'''Output a pong response as a greeting.'''

return "Pong", 200

What we did here was to create a new route that can be accessed at the /ping endpoint of our application. This route is only accessible in the form of GET requests and prints a simple response to the browser, along with sending HTTP 200 – SUCCESS as a status code to the browser.

With this done, we are now fully prepared to enter the development of database models that we will use to manage the data of our application. So, let's hop onto the model development wagon.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.68