3

I am trying to forward my flask api to mydomain.example with the end point being api.mydomain.example

For example, my method ping would have a end point api.mydomain.example/v1/server/ping. However, what I get is xx.xxx.xxx.xx:5005/v1/server/ping as endpoint.

Looking in other questions here in SO I found suggestions to modify app.config['SERVER_NAME'] or to add subdomain to route decorator. But nothing works. (I get a 404 error when I do any of these modifications).

Here is a minimal working example:

from flask import Flask, Blueprint
from flask_restplus import Resource, Api

app = Flask(__name__)
api = Api(app, version='1.0',
          title='My API',
          description='An API')

blueprint = Blueprint('api', __name__, url_prefix='/v1')
api.init_app(blueprint)
app.register_blueprint(blueprint)
#app.config['SERVER_NAME'] = 'mydomain.example:5005'
#app.url_map.default_subdomain = "test"

server = api.namespace("server",
                   description='Server API')


@server.route("/ping") #, subdomain="test")
class Ping(Resource):
    def get(self):
        """
        Check if Server is still alive
        """
        return {"reply":"PONG"}

if __name__ == '__main__':
    app.config["SWAGGER_UI_DOC_EXPANSION"] = "list"
    app.run(port=5005, host= '0.0.0.0', debug=True)

I went to my domain admin page (name.com) and added url forward. In that case, mydomain.info goes to swagger admin page, but mydomain.example/v1/server/ping also goes to swagger admin page.

But still, I get xx.xxx.xxx.xx:5005 in the Request URL section.

How do I make it work with the subdomain name?

Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
zeh
  • 1,197
  • 2
  • 14
  • 29
  • Are you pointing direct to flask or are you using a webserver like nginx or apache? – Tarun Lalwani May 19 '18 at 10:08
  • Do you have a server configuration for virtual hosts enabled? Did you created the A register for the `api` subdomain in your hosting provider? (which will allow you to get your dns trasnlation as you mean) – Evhz May 26 '18 at 08:54

1 Answers1

0

You don't have to change your app configuration. What you need to do is to run your app on a WSGI server which will proxy your app (on port 80 for example) through web servers such as Apache and Nginx. The WSGI server needs an entry point to communicate with your app on the port you specified (5005 in your code snippet). Here's a fairly simple tutorial about that (for more details, you can also refer to this link).

Furthermore, API Gateways are your best friends here especially if you're publishing your APIs to the public internet. You can look at AWS API Gateway or Apigee for API management with first-class support for Swagger (there are many others - including OSS).

0xack13
  • 462
  • 4
  • 8