Search code examples
python-2.7flaskamazon-ec2flask-restfulflask-restplus

Get 404 when running flask restplus app on amazon ec2


Okay I have done my research and tried and tested the following

Related Question

However, none of the solution provided there work and I don't understand why does removing constant will work? It's basically the same thing.

Here is my code,

import logging.config

import os, settings
from flask import Flask, Blueprint
from restplus.api.model.endpoints.servemodel import ns as modelserve
from restplus.api.apiInit import api

app = Flask(__name__)
# BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# logging.config.fileConfig(os.path.join(BASE_DIR, 'utils', 'logger.conf'))
# log = logging.getLogger(__name__)

def configure_app(flask_app):
    flask_app.config['SERVER_NAME'] = settings.FLASK_SERVER_NAME
    flask_app.config['SWAGGER_UI_DOC_EXPANSION'] = settings.RESTPLUS_SWAGGER_UI_DOC_EXPANSION
    flask_app.config['RESTPLUS_VALIDATE'] = settings.RESTPLUS_VALIDATE
    flask_app.config['RESTPLUS_MASK_SWAGGER'] = settings.RESTPLUS_MASK_SWAGGER
    flask_app.config['ERROR_404_HELP'] = settings.RESTPLUS_ERROR_404_HELP

def initialize_app(flask_app):
    configure_app(flask_app)
    blueprint = Blueprint('api', __name__)
    api.init_app(blueprint)
    api.add_namespace(modelserve)
    flask_app.register_blueprint(blueprint)


def main():
    initialize_app(app)
    # log.info('>>>>> Starting development server at http://{}/api/ <<<<<'.format(app.config['SERVER_NAME']))
    app.run(debug=settings.FLASK_DEBUG)


if __name__ == "__main__":
    main()

Now if you suggest that in Blueprint I set a '/api' path, I have tried it too with no success. The modelserve namespace is just a namespace at / and /recognize for function purpose.

I want my swagger UI to run at 0.0.0.0:5000. The variable settings.FLASK_SERVER_NAME is set to exactly that. And all the console says is that 404 with no explanation what so ever.

I have also tried printing app.url_map and seen that all routes of the app are registered where they should have been registered.

    Map([<Rule '/swagger.json' (HEAD, OPTIONS, GET) -> api.specs>,
 <Rule '/recognize/' (POST, OPTIONS) -> api.recognize_hand_written_digit_recognizer>,
 <Rule '/recognize/' (POST, OPTIONS) -> api.recognize_hand_written_digit_recognizer_2>,
 <Rule '/' (HEAD, OPTIONS, GET) -> api.doc>,
 <Rule '/' (HEAD, OPTIONS, GET) -> api.root>,
 <Rule '/swaggerui/<filename>' (HEAD, OPTIONS, GET) -> restplus_doc.static>,
 <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>])

But still swagger ui won't work. I am not sure what the problem is and neither does setting debug=True helping me. What can be done here?


Solution

  • Figured it out.

    When working with Flask-RESTPlus on amazon ec2, put the ip address of the server as the host. So for example, if the ip of the server is ec2-10-221-200-56.us-west-2.compute.amazonaws.com then set it as the host. So the final settings.FLASK_SERVER_NAME="http://ec2-10-221-200-56.us-west-2.compute.amazonaws.com:5000".

    This should make your swagger UI and other parts of application work fine!