[python]404 not found after flask_restful deployed with gunicorn

Ok, I have been struggling to get the gunicorn worked with the flask_restful.

So finally I found my answer here. after the if __name__ == "__main__" only run the app.run(). so I did some adjustment of my codes to become like this.

import sqlite3
from flask import Flask
from flask_restful import Api
from contacts import Register, ContactList, Contact

'''
Create contacts table for contacts database (contacts.db).
If the file contacts.db does not exist create one.
There are 7 columns:
1. contact_id, this is primary key hence is unique, will be incremented automatically.
2. fullname, this is the concatenation of last and first name.
3. last_name, this column stores the last name.
4. first_name, this column stores the first name.
5. email_address, this column stores the email address.
6. location_address, this column stores the physical address.
7. contact_number, this column stores the contact number.
'''
def create_table():
    query = "CREATE TABLE IF NOT EXISTS contacts (contact_id INTEGER PRIMARY KEY," \
            "fullname TEXT," \
            "last_name TEXT," \
            "first_name TEXT," \
            "email_address TEXT," \
            "location_address TEXT," \
            "contact_number)"
    db = sqlite3.connect('contacts.db')
    cursor = db.cursor()
    cursor.execute(query)
    db.commit()
    db.close()

# create the web app.
app = Flask(__name__)
# create the api object.
api = Api(app)

create_table()
'''
There are three endpoints based on Postman design.
{{url}}:5000/register - Post new contact
{{url}}:5000/contacts - Get all available contacts
{{url}}:5000/contact/ - Get specific single contact based on contact_id.
 corresponds with the contact_id of the get method of class Contact.
'''
api.add_resource(Register, '/register')
api.add_resource(ContactList, '/contacts')
api.add_resource(Contact, '/contact/')

# testing starts here.
if __name__ == '__main__':
    app.run()

Then I run gunicorn -b 0.0.0.0:5000 app:app and it worked. app:app, the app before the semi colon is the python file name, as my python web app is app.py so it becomes app:app, if your file is something else .py such as test.py then it looks like test:app.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s