Skip to content
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

Dcp can't seem to proxy Django applications #4158

Closed
wmeints opened this issue May 12, 2024 · 4 comments
Closed

Dcp can't seem to proxy Django applications #4158

wmeints opened this issue May 12, 2024 · 4 comments
Assignees

Comments

@wmeints
Copy link

wmeints commented May 12, 2024

I am trying to add support for Django and I'm running into this weird problem where Django doesn't seem to like cooperate with Dcp. But I'm unsure if the problem lies with Dcp or with Django.

The problem I see is that the app is running succesfully, but when I open the link in the dashboard I never receive a response from the Django Server. It looks like the server never receives a request from Dcp. I tried accessing the Django app without the proxy and that works okay.

I made a short video to demonstrate what happens:

https://youtu.be/Ih6gjU_uewI?si=VeSfc6FmcL5u5Z9y

For a full repro, please check out the sample on my github repo: https://github.com/wmeints/aspire-django-demo

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 12, 2024
@davidfowl
Copy link
Member

@karolz-ms can you take a look?

@karolz-ms karolz-ms self-assigned this May 14, 2024
@karolz-ms karolz-ms removed the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 14, 2024
@karolz-ms
Copy link
Member

Will do. Thanks for the report and a repro, much appreciated @wmeints

@karolz-ms
Copy link
Member

OK so there is a couple of problems with this scenario: one is an Aspire problem, and one is the application problem.

The Aspire problem is that the TargetPort setting on the endpoint is not honored when the endpoint is proxied. Instead, Aspire ignores this setting and runs the application on a random port. That issue has been fixed in the Aspire main branch via #3611, but unfortunately that change did not make it to release/8.0 branch and will probably only arrive in 8.1 release.

The second problem is that the application is not honoring the DJANGO_HTTP_PORT environment variable and instead always listens on port 8000. Python/Django is not my strong suit, but I managed to make the app work by adding a custom runserver_env Django command following the docs:

from django.core.management.commands.runserver import Command as Runserver
import os

class Command(Runserver):
    def handle(self, *args, **options):
        options['addrport'] = os.getenv('DJANGO_HTTP_PORT', '8000')
        return super().handle(*args, **options)

After changing the AppHost project to use the custom runserver_env command instead of runserver command the program works. Not sure if this is a good solution for production (probably isn't) but the point is, the app should listen on the port specified by the DJANGO_HTTP_PORT environment variable, if present.

@wmeints let me know please if this is enough information to get this issue closed as fixed, with the caveat that the fix may need some time to be released publicly.

@wmeints
Copy link
Author

wmeints commented May 15, 2024

Well, that explains a lot :-) Thanks for looking into this! I can work around the limitation for now. I'm happy to close this issue.

@wmeints wmeints closed this as completed May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants