How to use GAE with firebase hosting - django

I have a GAE project created using Django. I also have a static website for the project's homepage hosted on firebase.
If someone visits www.myproj.com, they should see the static webpage hosted on firebase.
If someone visits www.myproj.com/j/64gj37, they should see the response delivered by the django project.
I am looking for something like this:
configuration:
url: /
redirect_to: https://myproj-homepage.firebaseapp.com/
url: /(.+)$
project: myproj-django
What should be the configuration for dispatch.yaml and app.yaml? I am currently using a simplistic app.yaml
# [START django_app]
runtime: python37
default_expiration: "4d 5h"
handlers:
# This configures Google App Engine to serve the files in the app's
# static directory.
- url: /static
static_dir: static/
# This handler routes all requests not caught above to the main app.
# It is required when static routes are defined, but can be omitted
# (along with the entire handlers section) when there are no static
# files defined.
- url: /.*
script: auto
# [END django_app]

Related

configuring app.yaml handler for static image files

I have been trying to configure my app.yaml in the Google Cloud Platform.
I would like to achieve two things:
redirect all URLs (except images) to the main index.html in the root folder. For example, example.com/abcd should be redirected to example.com.
load any image requests from the folder named "mypics".
Here is my existing app.yaml file.
runtime: python27
api_version: 1
threadsafe: true
handlers:
# image files
- url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
static_files: mypics/\1
upload: mypics/(.*\.(bmp|gif|ico|jpeg|jpg|png))
# URL redirect
- url: /(.*)
static_files: index.html
upload: index.html
I have managed to get the redirects working. However, the images are not being loaded. I have reversed the order of the handlers, which does not seem to make a difference.
If I try to load the image directly, I get the following error:
Error: Not Found
The requested URL /mypics/Diamond.png was not found on this server.
Thanks for your help.
I suspect it has to do with how you specify the path to your images in your template files (i.e. html files). Let me explain with the result of the test I did on my machine
Previously,
Folder structure
Project Root
static
images
image_1.png
image_2.png
css
app.yaml
- url: /static
static_dir: static
Code in index.html file was < img src="/static/images/image_1.png" alt="Image 1" >.
The above worked (i.e. images were properly displayed)
Then I changed to your code
- url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
static_files: mypics/\1
upload: mypics/(.*\.(bmp|gif|ico|jpeg|jpg|png))
The images were no longer displayed. I also tried
- url: /static/(.*\.(bmp|gif|ico|jpeg|jpg|png))
static_files: mypics/\1
upload: mypics/(.*\.(bmp|gif|ico|jpeg|jpg|png))
but the images were still not displayed.
I then went back and modified the routing in app.yaml to
- url: /static/images/(.*\.(bmp|gif|ico|jpeg|jpg|png))
static_files: mypics/\1
upload: mypics/(.*\.(bmp|gif|ico|jpeg|jpg|png))
The change here is that I specified the full path I used in my index.html i.e. static/images/ and the images got displayed again

flask routes not working on gae application

I have the following directory structure for my GAE project:
endpoints/
insights/
insights.py
init.py
lib/
__init__.py
insights.yaml
so in order to get access to the third libraries within lib folder I add the next code in __init__.py file.
import os
import sys
def add_lib_path():
lib_directory = os.path.dirname(os.path.abspath(__file__))
if lib_directory not in sys.path:
sys.path.insert(0, lib_directory)
and I added this code in the insights.py file before the import statements:
from lib import add_lib_path
add_lib_path()
the problem is that now I can import third libraries correctly but my #app.route('/something', methods=['POST']) are not working.
I send a post request and it returns status 200 but it doesn't go inside my #app.route code, I can actually send any route and it just pass out returning 200 but not data and not error.
My imports look like this:
from lib import add_lib_path
add_lib_path()
from flask import Flask, request
And my code inside #app.route('/something', methods=['POST']) looks like this:
def someDef():
some code ...
return response
my yaml file looks like this:
runtime: python27
api_version: 1
threadsafe: false
service: insights
handlers:
- url: /.*
script: endpoints/insights/insights.py
libraries:
- name: ssl
version: latest
Any suggestions about this? Thanks in advance!
The wildcard URL handler in your app.yaml is intercepting the /something post:
handlers:
- url: /.*
script: application.app
You will need to either map out individual urls in your app, or make unique url set for your insights, like /insights/.* in app.yaml. Either way, you can't have a catch-all url handler in your app.yaml if there are other urls you want to give special treatment to, like sending to a separate service.
ok I just changed in the app.yaml file the threadsafe to true, then I added the script like this endpoints.insights.insights.app, using Python module path (with dots not slash) and in my insights.py file I changed #app.route('/something', methods=['POST']) to #app.route('/insights/something', methods=['POST']) .. I added the complete URL that I defined in the app.yaml file and now it's working.
Thanks to #GAEfan for the help, I'll accept the GAEfan answer because it helped me a lot

Django Static Files on Google Cloud Storage

I copied google cloud sample Django project for google appengine. Then, I modified settings.py for storing all static files on google storage. I added this settings
DEFAULT_FILE_STORAGE = 'storages.backends.gs.GSBotoStorage'
GS_ACCESS_KEY_ID = 'YourID'
GS_SECRET_ACCESS_KEY = 'YourKEY'
GS_BUCKET_NAME = 'YourBucket'
STATICFILES_STORAGE = 'storages.backends.gs.GSBotoStorage'
On local machine everything is working perfectly but when I deploy it to google appengine static file problem appears. Django can't find static files which in special google storage bucket. This is the error message:
ImportError at /admin/login/ cannot import name SpooledTemporaryFile
in app.yaml file, settings about static files:
handlers:
- url: /static
static_dir: static/
- url: .*
script: mysite.wsgi.application
I am new at google cloud and i can't figure it out where I am doing wrong.
Edit: I think, I must modify app.yaml file to say appengine "Hey, use another bucket don't serve static file yourself".
Tere is more detailed error output:
ImportError at /admin/ cannot import name SpooledTemporaryFile Request
Method: GET Request URL: https://sbminteractive.appspot.com/admin/
Django Version:
1.8 Exception Type: ImportError Exception Value: cannot import name SpooledTemporaryFile Exception Location:
/base/data/home/apps/e~sbminteractive/20170414t175601.400554512166425273/lib/storages/backends/s3boto.py
in , line 6 Python Executable:
/base/data/home/runtimes/python27/python27_dist/python Python Version:
2.7.5 Python Path: ['/base/data/home/apps/e~sbminteractive/20170414t175601.400554512166425273',
'/base/data/home/apps/e~sbminteractive/20170414t175601.400554512166425273/lib',
'/base/data/home/runtimes/python27/python27_dist/lib/python27.zip',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7/plat-linux2',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-tk',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-old',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7/lib-dynload',
'/base/data/home/runtimes/python27/python27_dist/lib/python2.7/site-packages',
'/base/data/home/runtimes/python27/python27_lib/versions/1',
'/base/data/home/runtimes/python27/python27_lib/versions/third_party/MySQLdb-1.2.5',
'/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0',
'/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3',
'/base/data/home/runtimes/python27/python27_lib/versions/third_party/webob-1.1.1',
'/base/data/home/runtimes/python27/python27_lib/versions/third_party/yaml-3.10']
Server time: Fri, 14 Apr 2017 14:57:51 +0000

The app returned an error when the Google Cloud Endpoints server attempted to communicate with it

Hi I'm new to Google App Engine and Cloud Endpoints.
I have an app engine that serves web application.
And I want to use it with Cloud Endpoints.
So I just added source codes from here:
https://cloud.google.com/appengine/docs/python/endpoints/getstarted/backend/write_api
But I cannot deploy app and get this error message:
Checking if Endpoints configuration has been updated.
07:13 AM Failed to update Endpoints configuration. The app returned an error when the Google Cloud Endpoints server attempted to communicate with it.
07:13 AM See the deployment troubleshooting documentation for more information: https://developers.google.com/appengine/docs/python/endpoints/test_deploy#troubleshooting_a_deployment_failure
And this is my source codes
import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote
package = 'Hello'
class Greeting(messages.Message):
"""Greeting that stores a message."""
message = messages.StringField(1)
class GreetingCollection(messages.Message):
"""Collection of Greetings."""
items = messages.MessageField(Greeting, 1, repeated=True)
STORED_GREETINGS = GreetingCollection(items=[
Greeting(message='hello world!'),
Greeting(message='goodbye world!'),
])
#endpoints.api(name='helloworld', version='v1')
class HelloWorldApi(remote.Service):
"""Helloworld API v1."""
#endpoints.method(message_types.VoidMessage, GreetingCollection,
path='hellogreeting', http_method='GET',
name='greetings.listGreeting')
def greetings_list(self, unused_request):
return STORED_GREETINGS
ID_RESOURCE = endpoints.ResourceContainer(
message_types.VoidMessage,
id=messages.IntegerField(1, variant=messages.Variant.INT32))
#endpoints.method(ID_RESOURCE, Greeting,
path='hellogreeting/{id}', http_method='GET',
name='greetings.getGreeting')
def greeting_get(self, request):
try:
return STORED_GREETINGS.items[request.id]
except (IndexError, TypeError):
raise endpoints.NotFoundException('Greeting %s not found.' %
(request.id,))
APPLICATION = endpoints.api_server([HelloWorldApi])
exactly same with tutorial
and my app.yaml
application: my application id
version: application version
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /data
script: MLP.app
login: admin
- url: /insert_db
script: MLP.app
login: admin
- url: /validete
script: MLP.app
login: admin
- url: /stylesheets
static_dir: stylesheets
- url: /js
static_dir: js
- url: /.*
script: MLP.app
- url: /_ah/spi/.*
script: MLP_mobile_backend.APPLICATION
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
- name: pycrypto
version: latest
- name: endpoints
version: 1.0
MLP.py is just web service with webapp2
How can I solve the problem?
From the app.yaml line script: MLP_mobile_backend.APPLICATION, it means your code sample must live in a MLP_mobile_backend.py file at the root of your app engine project
.
├── app.yaml
├── MLP.py
└── MLP_mobile_backend.py
with an endpoint api server named APPLICATION defined in that file just as in your code sample above.
APPLICATION = endpoints.api_server([HelloWorldApi])
After those requirements are fulfilled, this line points to how to access your endpoints:
#endpoints.api(name='helloworld', version='v1')
So for example, let's say you're running your devserver's module on port 8080 and you want to access the hellogreeting path:
#endpoints.method(message_types.VoidMessage, GreetingCollection,
path='hellogreeting', http_method='GET',
name='greetings.listGreeting')
you can use cURL or httpie or your browser to locally navigate to the devserver's default module at http://localhost:8080/_ah/api/helloworld/v1/hellogreeting or with Google's API explorer at http://localhost:8080/_ah/api/explorer which should result in this response body:
{
"items": [
{
"message": "hello world!"
},
{
"message": "goodbye world!"
}
]
}
Once you get it set up locally, you can then deploy it.
More here

google app engine not loading admin static media files

I am new to django. I am using Google App-engine and Google CloudSQL to serve a django1.2 project. I've been following the tutorial here and have hit a snag.
I enabled the admin back end in my settings.py, but when I go to http://www.examplesite.com/admin/ the css files aren't found.
The admin html page loads fine, but the static files give a 404.
Here's my app.yml:
application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: django
version: "1.2"
builtins:
- django_wsgi: on
I left these as default in settings.py:
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'
I know the files are located at google_appengine/lib/django_1_2/django/contrib/admin/media/
Do I need to copy those static resources into my project directory somewhere? Or do I have a setting wrong?
In GAE you need to make the static files accessible in your app.yaml
https://developers.google.com/appengine/docs/python/config/appconfig#Static_Directory_Handlers