GAE的app.yaml用瓶,不安路由 [英] GAE app.yaml routing with Flask-Restless

查看:537
本文介绍了GAE的app.yaml用瓶,不安路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展与AngularJS前端+后端GAE(Python和瓶)的应用程序。我有麻烦的app.yaml设置路由与烧瓶不宁延伸创造了我的API端点。我的app.yaml文件看起来是这样的:

I am developing an app with AngularJS frontend + GAE backend (Python and Flask). I am having troubles to setting app.yaml for routing my API endpoints created with Flask-Restless extention. My app.yaml file looks like this:

application: myAppID
version: 1
runtime: python27
threadsafe: true
api_version: 1

handlers:
# handler 1
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

# handler 2
- url: /api/.*
  script: main.app

# handler 3
- url: /test
  script: main.app

# handler 4
- url: (.*)/
  static_files: app\1/index.html
  upload: app #this is the frontend folder for Angular

# handler 5
- url: (.*)
  static_files: app\1
  upload: app #this is the frontend folder for Angular

在折角,路由配置是这样的:

In Angular, the routes configuration looks like this:

App.config(['$stateProvider', '$locationProvider', '$urlRouterProvider', 'RouteHelpersProvider',
function ($stateProvider, $locationProvider, $urlRouterProvider, helper) {
  'use strict';

  $locationProvider.html5Mode(false);

  // default route
  $urlRouterProvider.otherwise('/app/dashboard');

  // other routes ...
}]);

在main.py文件看起来是这样的:

The main.py file looks like this:

from flask import Flask
import os
from werkzeug import debug
from flask import jsonify
from google.appengine.ext.webapp.util import run_wsgi_app

app = Flask('myApp')

if os.getenv('SERVER_SOFTWARE') and os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/'):
    app.debug = False
else:
    app.debug = True

if app.debug:
    app.wsgi_app = debug.DebuggedApplication(app.wsgi_app, True)

@app.route('/test')
def test():
    return jsonify(test={"json": "test"})

import models

run_wsgi_app(app)

模式是包含烧瓶SQLAlchem​​y的模型和烧瓶不宁端点文件。

models is the file that contains Flask-SQLAlchemy models and Flask-Restless endpoints.

角度部分正确加载,例如这个URL正常工作:

The Angular part loads correctly, for example this URL works fine:


  • A)的http://本地主机:8080 /#/应用/仪表板

  • A) http://localhost:8080/#/app/dashboard

但随着500错误的GAE后端部分的反应像这些网址:

But the GAE backend part responses with a 500 error for URLs like these:


  • B)的http://本地主机:8080 / API /人

  • C)的http://本地主机:8080 /测试

  • B) http://localhost:8080/api/person
  • C) http://localhost:8080/test

如果我删除处理4 处理5 然后 B C 的URL工作正常,但角前端停止工作。

If I remove the handler 4 and handler 5 then B and C URLs works fine but Angular frontend stop working.

我在做什么错了?

推荐答案

林在旅途中,所以从我的手机写的不是好玩...

Im on the go, so writing from my phone isn't that fun...

什么办法,我在我的应用程序确实是,我只有一个处理程序触发烧瓶应用程序。

Any way, what i did in my app is that i have only one handler that triggers the flask app.

在烧瓶中的应用程序通常是/路由将返回角度的web应用程序作为一个静态文件。

In the flask app usually the / route will return the angular web app as a static file.

您需要配置瓶的应用程序,它会了解静(HTML,JS等)的文件夹。

You need to configure your Flask app , that it will know about the statics (HTML, JS etc.) Folder.

编辑:

app.yaml中应该是这样的:

app.yaml should look like this:

handlers:
- url: .*  # This regex directs all routes to main.app
   script: main.app

main.app是烧瓶应用..
现在让我们看看如何从路径/

main.app is the flask app.. now lets see how to serve the angular app from the route '/'

from flask import Flask, render_template
app = Flask(__name__, static_folder='/templates') # This sets /templates to be the folder for all JS HTML CSS files

@app.route('/')
def wellcomePage():
    return app.send_static_file('index.html')

在您的app.js文件角路由配置:

angular routing configuration in your app.js file:

app.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'templates/views/home.html'
        }).... Some More Routes..

需要注意的是templateUrl:模板/...'

Note that templateUrl: 'templates/...'

请看看我的应用程序。我认为这将有助于你明白我想在这里说...

Please take a look at my app. I think it will help you understand what I'm trying to say here...

SE集线器在github上

生病编辑这个答案时,我得到一个再用键盘:)

Ill edit this answer when i get to a freaking keyboard :)

让我知道,如果这有助于。

Let me know if this helps.

这篇关于GAE的app.yaml用瓶,不安路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆