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

Routing problems with Python #3729

Closed
Uninen opened this issue Feb 3, 2020 · 4 comments
Closed

Routing problems with Python #3729

Uninen opened this issue Feb 3, 2020 · 4 comments

Comments

@Uninen
Copy link

Uninen commented Feb 3, 2020

I've tested just about everything but can't get any other routing working on Python except /api.

I'm trying to set up Starlette (or Sanic, or whatever, doesn't really matter) to respond from one app (file) to several URLs. (For example. /api/foo and /api/bar that would be routed in different handler functions in the single app with a router.)

Full source code of my test app:

from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.routing import Route

def homepage(request):
    return PlainTextResponse(f"Homepage URL: {request.url}, path: {request.url.path}")

routes = [
    Route("/{whatever:path}", homepage),
]

app = Starlette(debug=True, routes=routes)

(This works ok with uvicorn or other servers.)

When I save this as api/index.py the /api url works, but no other URL. When I save this as api/[index].py or api/[anything].py I get a 502 + following error in the logs:

Syntax error in module 'now__handler__python': invalid syntax (now__handler__python.py, line 7)

All other setup combinations result in 404 page (from ZEIT, not from the app) and no logs whatsoever.

Is it even possible to do basic URL routing with python? (Also there are zero examples or documentation on how to do this, that would probably help as well.)

@styfle
Copy link
Member

styfle commented Feb 4, 2020

Hi @Uninen

It sounds like you are trying to create one python function that responds to multiple routes.

This is achievable but is an anti-pattern. Routing in ZEIT Now is handled by the file system, so /api/foo and /api/bar are routed automatically to /api/foo.py and /api/bar.py, two different files. These can both read the request and write the response appropriately.

If you want to rewrite multiple routes to a single function, you can configure rewrites in now.json.

@Uninen
Copy link
Author

Uninen commented Feb 4, 2020

I did try rewrites also, but couldn't get them working (404s or 502s as well). But if the file-based routing is the preferred method, I can of course just copy paste the boilerplate into multiple files.

@Uninen Uninen closed this as completed Feb 4, 2020
@styfle
Copy link
Member

styfle commented Feb 4, 2020

You shouldn’t need to copy/paste because python can import shared files. You can put any common code outside the api directory or put it inside with an underscore, for example /api/_utils/common.py

kodiakhq bot pushed a commit that referenced this issue Feb 10, 2020
Previously, python would fail when using [Path Segments](https://zeit.co/docs/v2/serverless-functions/introduction#path-segments) as seen in #3729.

This PR changes the import logic so that the entrypoint file is [imported as a relative file path](https://stackoverflow.com/a/67692/266535) instead of by module name.
@ninest
Copy link

ninest commented Jun 20, 2020

@styfle I tried this method but it doesn't seem to work. I have _utils/common.py inside my api/ directory.

My index.py looks like this:

from flask import Flask, Response
from _utils.common import VAR
app = Flask(__name__)

print(VAR)

...

When I try to visit any API route, I get this error:

 File "./api/index.py", line 2, in <module>
    from _utils.common import VAR
ModuleNotFoundError: No module named '_utils'

Everything works fine when I run the code with python3 api/index.py, but using vercel dev causes this error. It comes as a 502: bad gateway error.

Is there an official example I can refer to?

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