1

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 ...
}]);

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)

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

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

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

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

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

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

What I'm doing wrong?

Manuel Lopera
  • 2,268
  • 1
  • 16
  • 16
  • possible duplicate of [Angular route not working when used with Google App Engine and Flask](http://stackoverflow.com/questions/30574398/angular-route-not-working-when-used-with-google-app-engine-and-flask) – vicg Jul 02 '15 at 23:27

1 Answers1

1

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.

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

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

EDITED:

app.yaml should look like this:

handlers:
- url: .*  # This regex directs all routes to main.app
   script: 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')

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..

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 Hub at github

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

Let me know if this helps.

Sagi Dayan
  • 118
  • 1
  • 7
  • Oh man! You really rescue me. Only for dev purposes I am using: `return make_response(open('app/index.html').read())` instead of `app.send_static_file('index.html')` as mentioned in [this answer](http://stackoverflow.com/a/15144980/3306960) – Manuel Lopera Jul 03 '15 at 03:48
  • How do you upload the static files to GAE?, when I deploy doesn't works :( – Manuel Lopera Jul 03 '15 at 07:01
  • What exactly is not working? Are you using CL to deploy and if so what command? Do you get any errors? – Sagi Dayan Jul 03 '15 at 09:44
  • I am using GAE Launcher on Windows, it has a Deploy button that makes all the work, no errors are shown and deployment is successfull. But when I try to access to myapp.appspot.com I get a 500 Server Error. I guess that my app/index.html file is not found. – Manuel Lopera Jul 03 '15 at 16:16
  • Go to your GAE console. And take a look at your logs. You should see an error message. Tell me what it says? – Sagi Dayan Jul 03 '15 at 17:07
  • 1
    Thanks @Sagi Dayan, I don't knew the GAE logs, they are so useful!. The error was I need import MySqldb in `app.yaml`module for connect to CloudSQL instance with Flask-SQLAlchemy using an URI like `'mysql+mysqldb://username@/database?unix_socket=/cloudsql/projectID:instanceID'`. When I am in localhost server the connection URI is `mysql://username:password@localhost/database` and MySqldb is not needed. – Manuel Lopera Jul 07 '15 at 04:32
  • Why `main.app`, and not `main.py` ? – Sohail Si Jan 20 '17 at 22:56