-
Notifications
You must be signed in to change notification settings - Fork 68
Enable WSGI for Python Function App #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome, thanks folks! I'll deprecate the separate project in favor of this 😁
Hello Its aready able to run Django Applications too? Thanks! |
Hey @rolexsanches, Yes, Django also accept wsgi request via WSGI interface. import logging
import azure.functions as func
from ..Django.wsgi import DjangoApplication
main = func.WsgiMiddleware(DjangoApplication).main Please wait for the next Python release to enable this feature. |
self.content_type = self._lowercased_headers.get('content-type') | ||
self.content_length = str(len(func_req_body)) | ||
self.server_name = getattr(url, 'hostname', None) | ||
self.server_port = str(self._get_port(url, self._lowercased_headers)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getattr
might be slightly slower (due to reflection) than regular get, but may not be a big deal. Just a note.
The following code should do the trick. ?? (Ha! not exactly)
So I expended a few neurons trying to wrap my head around Azure Functions & Django and WSGI at the same time and this helpful snippet makes little/no sense to me. I'm going to assume that code snippet belongs in It's not manage.py is the command line utility for admin tasks, but for some reason my Azure Function deployment script wants to deploy that. I've added manage.py to the I think the import os
import azure.functions as func
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
application = get_wsgi_application()
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
return func.WsgiMiddleware(application).handle(req, context) ^^^ note: YMMV .. I haven't actually gotten this working YET (so the lines above may be totally wrong), though I'm pretty confident the earlier sample above doesn't work either, so hoping somebody might see this. I'm also going to assume that on Azure functions the host.json needs to have the routePrefix disabled such as: "extensions": {
"http": {
"routePrefix": ""
}
} as described in the "Customize the http endpoint" section of the Azure Functions reference In {
{
"bindings":[
"route":"myapp/{*route}",
]
}
} And that sorta works, but not really. Because for my purpose the django The *route is a kludge, maybe better answers here: https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#constraints So I can't figure out how to get Django to load from an Azure function but I've put in sufficient time in this fools errand so I'm giving up and heading back to a Docker instance. |
How do create wsgi object in flask? Could someone to give me example? from ..FlaskApp.wsgi import application import logging
import azure.functions as func
from ..FlaskApp.wsgi import application |
@DharakOne this example might clear out some things: https://github.com/Hazhzeng/functions-wsgi-demo. Let me know if it is not clear |
Isn't the example using the azf-wsgi library and the old style? https://github.com/Hazhzeng/functions-wsgi-demo/blob/master/HttpTrigger/__init__.py |
For anyone wishing for a very simple starting example implementation, try this:
I used an http Trigger function, cleverly named httpTrigger, thus the routing above.
Running the function app locally using the function runtime core tools, if I hit this URL: The response I get back is a 200 OK and "Hello, from Flask!" |
Hi guys! 👋 |
@eelwk
then init.py it always start a random port instead of func 7071 default port
|
Acknowledgement
This feature is migrated from @vtbassmatt 's azf-wsgi project which enables wsgi feature for Azure Functions Python app. We've contacted @vtbassmatt and agreed to implement this feature on azure-functions-python-library. We greatly appreciate his contribution, helping us expand the Python user base in his own time.
Usage
Fixes
This PR also includes some fixes from the original repository
Thanks
Limitation
Todos
Since the feature is not officially released, feature-wise this PR is ready for review, and of course, we need to verify the following features before we release this WSGI change.
resolves: Azure/azure-functions-python-worker#165