首页 > 解决方案 > /app1 中的 Python Bottle 应用程序 - 我是否被迫在所有路线前添加“/app1”?

问题描述

根据教程,我使用 run() 在本地开发了一个瓶子应用程序,其路由都以“/”开头,现在我想把它放在真实的服务器上。

http://bottlepy.org/docs/dev/deployment.html上的文档建议使用WSGIScriptAlias / /var/www/yourapp/app.wsgi,但我不希望我的站点的根目录由 WSGI 应用程序处理。我希望网站的根目录由原始 Apache 处理,并且只有 /app1 下的 URL 由 WSGI 处理。

所以我将我的设置为WSGIScriptAlias /app1 /var/www/app1/app1.py. 从某种意义上说,当我浏览到 server://app1 时,我可以看到我在 route('/') 下的 .py 文件中定义的内容,但没有一个超链接带有 /app1 前缀,并且浏览器无法选择从 /var/www/app1/css 等中提取我的 css 文件。

对象说明了一切。当我希望应用程序存在于子目录中时,我是否被迫在所有路由前添加“/app1”?

我试图让自己适应未来,因为我预见到将来会制作 /app2、/app3 等。

编辑 1:为了实验,我确实尝试将 /app1 添加到所有路由。结果更糟:我尝试在 /app1 下浏览的每个地址都会出现 404 错误。

标签: python-3.xmod-wsgibottle

解决方案


所以为了尝试这个,我从下面的 repo 中获取了一个样板代码

https://github.com/arsho/bottle-bootstrap

并以此为基础。我发现如果您遵循一些简单的规则,可以将您的应用程序安装在基本 URL 上

  • 您提供的每个 HTML 都应该使用HEAD标签定义一个基本路径。像<base href="{{ APP_MOUNT_PATH }}">
  • 需要相对于基本路径的静态引用不应以/or开头./。喜欢<script type="text/javascript" src="static/jquery.min.js"></script>base这将确保使用您在标签中提供的挂载路径生成路径
  • 启动应用时从环境变量中获取挂载路径

所以这里是更新的文件

索引.tpl

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Bottle web project template">
    <meta name="author" content="datamate">
    <link rel="icon" href="static/favicon.ico"> 
    <base href="{{ APP_MOUNT_PATH }}">
    <title>Project</title>
    <link rel="stylesheet" type="text/css" href="static/bootstrap.min.css">
    <script type="text/javascript" src="static/jquery.min.js"></script>
    <script type="text/javascript" src="static/bootstrap.min.js"></script>  
</head>
<body>
    <!-- Static navbar -->
    <nav class="navbar navbar-default navbar-static-top">
        <div class="container">
            <div class="row">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#">Project</a>
                </div>
                <div id="navbar" class="navbar-collapse collapse">
                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="navbar/">Home</a></li>
                        <li><a href="./">Github</a></li>
                        <li><a href="navbar-fixed-top/">Stackoverflow</a></li>
                    </ul>
                </div><!--/.nav-collapse -->
            </div>
        </div>
    </nav>
    <div class="container">
        <div class="row">
            <div class="jumbotron">
            <h2>Welcome from {{data["developer_name"]}}</h2>
                <p>This is a template showcasing the optional theme stylesheet included in Bootstrap. Use it as a starting point to create something more unique by building on or modifying it.</p>
            </div>
        </div>
        <!--./row-->
        <div class="row">
            <hr>
            <footer>
                <p>&copy; 2017 {{data["developer_organization"]}}.</p>
            </footer>           
        </div>
    </div> 
    <!-- /container -->
</body>
</html>

应用程序.py

from bottle import Bottle, run, \
     template, debug, static_file

import os, sys

dirname = os.path.abspath(os.path.dirname(__file__))

app = Bottle()
debug(True)

@app.route('/static/<filename:re:.*\.css>')
def send_css(filename):
    print("Sending", filename, dirname)
    return static_file(filename, root=dirname+'/static/asset/css')

@app.route('/static/<filename:re:.*\.js>')
def send_js(filename):
    print("Sending", filename, dirname)
    return static_file(filename, root=dirname+'/static/asset/js')

@app.route('/')
def index():
    data = {"developer_name":"Tarun Lalwani",
            "developer_organization":""}
    return template('index', data = data)

@app.route('/tarun/')
def tarun():
    data = {"developer_name":"Tarun Lalwani",
            "developer_organization":""}
    return template('index', data = data)


if __name__ == "__main__":
    run(app, host='localhost', port = 8080)

应用程序.wsgi

import os
os.chdir(os.path.abspath(os.path.dirname(__file__)))

import bottle
from app import app
application = bottle.default_app()

mount_path = os.getenv("APP_MOUNT_PATH", "/")
application.config['APP_MOUNT_PATH'] = mount_path

application.mount(mount_path,  app)
bottle.BaseTemplate.defaults['APP_MOUNT_PATH'] = mount_path

然后我运行它使用uwsgi

[uwsgi]
http = 127.0.0.1:3031
chdir = /Users/tarunlalwani/Documents/Projects/SO/bottle-bootstrap
pythonpath = .
env = APP_MOUNT_PATH=/app2/
wsgi-file = app.wsgi
processes = 1
threads = 1
stats = 127.0.0.1:9191
; logto = ./uwsgi.log

现在应用程序在http://localhost:3031/app2/`` http://localhost:3031/app2/tarun`处加载正常,这表明基本路径适用于两种类型的 url

基本网址

基本 URL + 一级

为了您的方便,所有代码都可以在下面的仓库中找到

https://github.com/tarunlalwani/python-bottle-base-url


推荐阅读