I'm trying to extend an existing Python 3 application to include a REST API. I've been searching for several days trying to figure out how to make the REST API a subcomponent of the existing application, but the guides I'm finding for things like Flask, Eve, etc. don't show how to run a production API unless the API application itself is run directly.
I have successfully added a Flask REST API to my application and it responds as expected. However, it is running in a way that is unsupported and allegedly unsafe, at least according to the developers. A warning message is produced when I run my application suggesting I should be using a WSGI server instead:
* Serving Flask app "api" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Admittedly, I am new to this, so I'm not sure what that means or if/how it will work in my situation. Effectively, I'm trying to do this:
import api as API
class Stuff(object):
def setup(self):
...
self.apiThread = API(...)
def run(self):
...
self.apiThread.start()
The application is large, complex, mature, and stable, so I can't and/or don't know how to make it work as subcomponent of an API application like this:
from werkzeug.wrappers import Request, Response
@Request.application
def application(request):
return Response('Hello, World!')
if __name__ == '__main__':
from werkzeug.serving import run_simple
run_simple('localhost', 4000, application)
I've seen guides like this that have helped me understand the guts of a webserver to some degree, but I can't find a production solution that makes sense to me yet. Am I supposed to implement a class like what is shown on page 2 and then instantiate it in a thread like I showed in my example Stuff
class? Would that work correctly or am I still missing something?
Any guidance would be greatly appreciated.
Edit: The gunicorn
suggestions seem to be common and correct for many use cases - possibly including mine - but I don't understand why yet. The information on the gunicorn
page says this on the first line:
If you want to deploy your Flask application to a WSGI server...
I'm already stuck because I have not built a flask application. I am running a flask server in a thread as a very minor part of a much larger application. I don't understand how to flip this around, so to speak.