@abudu/vercel-python-wsgi-fix 中文文档教程

发布于 3年前 浏览 17 项目主页 更新于 3年前

vercel-python-wsgi

Python WSGI 应用程序的 Vercel 构建器

NPM 版本构建状态License

Quickstart

如果您有现有的 WSGI 应用程序,让这个构建器为您工作是一个 一片 ????!

1. Add a Vercel configuration

vercel.json 文件添加到应用程序的根目录:

{
    "builds": [{
        "src": "index.py",
        "use": "@ardnt/vercel-python-wsgi",
        "config": { "maxLambdaSize": "15mb" }
    }]
}

此配置在 “builds” 部分执行一些操作:

  1. "src": "index.py" This tells Now that there is one entrypoint to build for. index.py is a file we'll create shortly.
  2. "use": "@ardnt/vercel-python-wsgi" Tell Now to use this builder when deploying your application
  3. "config": { "maxLambdaSize": "15mb" } Bump up the maximum size of the built application to accommodate some larger python WSGI libraries (like Django or Flask). This may not be necessary for you.

2. Add a Now entrypoint

添加 index.py 到应用程序的根目录。 这个入口点应该使 可用一个名为 application 的对象,它是您的 WSGI 的一个实例 应用。 例如:

# For a Dango app
from django_app.wsgi import application
# Replace `django_app` with the appropriate name to point towards your project's
# wsgi.py file

查看您的框架文档以获得访问 WSGI 的帮助 应用。

如果 WSGI 实例没有命名为 application 你可以设置 wsgiApplicationName 配置选项以匹配您的应用程序名称(请参阅 下面的配置部分)。

3. Deploy!

就是这样,您可以开始了:

$ vercel
> Deploying python-wsgi-app
...
> Success! Deployment ready [57s]

Requirements

您的项目可以选择包含一个 requirements.txt 文件来声明任何 依赖项,例如:

# requirements.txt
Django >=2.2,<2.3

请注意,构建器将安装 Werkzeug 作为 处理程序。 如果您的项目需要不同版本的 Werkzeug 比处理程序。

Configuration options

runtime

选择 lambda 运行时。 默认为 python3.6

{
    "builds": [{
        "config": { "runtime": "python3.6" }
    }]
}

wsgiApplicationName

选择要从您的入口点运行的 WSGI 应用程序。 默认为 <代码>应用程序。

{
    "builds": [{
        "config": { "wsgiApplicationName": "application" }
    }]
}

Additional considerations

Routing

您可能希望所有到达部署 URL 的请求都被路由到 你的申请。 您可以通过向 Now 添加路由重写来做到这一点 配置:

{
    "builds": [{
        "src": "index.py",
        "use": "@ardnt/vercel-python-wsgi"
    }],
    "routes" : [{
        "src" : "/(.*)", "dest":"/"
    }]
}

Avoiding the index.py file

如果在你的项目中有一个额外的文件很麻烦或者看起来没有必要,那就是 也可以将 Now 配置为直接使用您的应用程序,而无需通过 它通过 index.py

如果您的 WSGI 应用程序位于 vercel_app/wsgi.py 中并且名为 application, 然后您可以将其配置为入口点并相应地调整路由:

{
    "builds": [{
        "src": "vercel_app/wsgi.py",
        "use": "@ardnt/vercel-python-wsgi"
    }],
    "routes" : [{
        "src" : "/(.*)", "dest":"/vercel_app/wsgi.py"
    }]
}

Lambda environment limitations

在撰写本文时,Vercel 在 AWS Lambda 上运行。 这有许多 对您可以使用哪些库的影响,特别是:

  • PostgreSQL, so psycopg2 won't work out of the box
  • MySQL, so MySQL adapters won't work out of the box either
  • Sqlite, so the built-in Sqlite adapter won't be available

Contributing

To-dos

  • [ ] Add tests for various types of requests

Attribution

此实现借鉴了以下工作:

vercel-python-wsgi

A Vercel builder for Python WSGI applications

NPM versionBuild StatusLicense

Quickstart

If you have an existing WSGI app, getting this builder to work for you is a piece of ????!

1. Add a Vercel configuration

Add a vercel.json file to the root of your application:

{
    "builds": [{
        "src": "index.py",
        "use": "@ardnt/vercel-python-wsgi",
        "config": { "maxLambdaSize": "15mb" }
    }]
}

This configuration is doing a few things in the "builds" part:

  1. "src": "index.py" This tells Now that there is one entrypoint to build for. index.py is a file we'll create shortly.
  2. "use": "@ardnt/vercel-python-wsgi" Tell Now to use this builder when deploying your application
  3. "config": { "maxLambdaSize": "15mb" } Bump up the maximum size of the built application to accommodate some larger python WSGI libraries (like Django or Flask). This may not be necessary for you.

2. Add a Now entrypoint

Add index.py to the root of your application. This entrypoint should make available an object named application that is an instance of your WSGI application. E.g.:

# For a Dango app
from django_app.wsgi import application
# Replace `django_app` with the appropriate name to point towards your project's
# wsgi.py file

Look at your framework documentation for help getting access to the WSGI application.

If the WSGI instance isn't named application you can set the wsgiApplicationName configuration option to match your application's name (see the configuration section below).

3. Deploy!

That's it, you're ready to go:

$ vercel
> Deploying python-wsgi-app
...
> Success! Deployment ready [57s]

Requirements

Your project may optionally include a requirements.txt file to declare any dependencies, e.g.:

# requirements.txt
Django >=2.2,<2.3

Be aware that the builder will install Werkzeug as a requirement of the handler. This can cause issues if your project requires a different version of Werkzeug than the handler.

Configuration options

runtime

Select the lambda runtime. Defaults to python3.6.

{
    "builds": [{
        "config": { "runtime": "python3.6" }
    }]
}

wsgiApplicationName

Select the WSGI application to run from your entrypoint. Defaults to application.

{
    "builds": [{
        "config": { "wsgiApplicationName": "application" }
    }]
}

Additional considerations

Routing

You'll likely want all requests arriving at your deployment url to be routed to your application. You can do this by adding a route rewrite to the Now configuration:

{
    "builds": [{
        "src": "index.py",
        "use": "@ardnt/vercel-python-wsgi"
    }],
    "routes" : [{
        "src" : "/(.*)", "dest":"/"
    }]
}

Avoiding the index.py file

If having an extra file in your project is troublesome or seems unecessary, it's also possible to configure Now to use your application directly, without passing it through index.py.

If your WSGI application lives in vercel_app/wsgi.py and is named application, then you can configure it as the entrypoint and adjust routes accordingly:

{
    "builds": [{
        "src": "vercel_app/wsgi.py",
        "use": "@ardnt/vercel-python-wsgi"
    }],
    "routes" : [{
        "src" : "/(.*)", "dest":"/vercel_app/wsgi.py"
    }]
}

Lambda environment limitations

At the time of writing, Vercel runs on AWS Lambda. This has a number of implications on what libaries will be available to you, notably:

  • PostgreSQL, so psycopg2 won't work out of the box
  • MySQL, so MySQL adapters won't work out of the box either
  • Sqlite, so the built-in Sqlite adapter won't be available

Contributing

To-dos

  • [ ] Add tests for various types of requests

Attribution

This implementation draws upon work from:

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文