I am adding a service worker in Google Appengine. According to the documentation, sw.js must be in the same folder as the HTML file which is calling it.
I added the following to my app.yaml file, but I am still getting 404 Not Found for the sw.js file:
- url: /(index|sw)\.js$
static_files: \1
upload: (index|sw)\.js$
application_readable: true
I tested my regular expression in regex101.com, so I know that's correct.
I think there's something wrong with the rest of the declaration.
Your regex omits the extension from the static_files file. Try:
- url: /(index|sw)\.js$
static_files: \1.js
upload: (index|sw)\.js
application_readable: true
Related
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
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]
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
I'm trying to make a simple form that receives an input from the user and respond by displaying same input on the webpage using python2.7 and Google app engine running locally.Using SublimeText. I'm getting errors "HTTP ERROR 500" and tried to solve it as shown below but no luck.
This is the app.yaml file:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
script: test.app
This is the test file:
import webapp2
form="""
<form action="/testform">
<input name= "q">
<input type="submit">
</form>
"""
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write(form)
class TestHandler(webapp2.RequestHandler):
def get(self):
q = self.resquest.get("q")
self.response.out.write(q)
app = webapp2.WSGIApplication([('/', MainPage), ('/testform', TestHandler)], debug=True)
When I run the file above I receive this message:
The url "/testform" does not match any handlers.
I already try including a separate handler for the /testform URL:
handlers:
- url: /
script: test.app
- url: /testform
script: test.app
When I do that then I receive the following message:
localhost is currently unable to handle this request.
HTTP ERROR 500
I also try changing / to .* and also to /.* on the app.yaml file to handle all URL's like this:
handlers:
- url: .*
script: test.app
and:
handlers:
- url: /.*
script: test.app
Having the same result:
localhost is currently unable to handle this request.
HTTP ERROR 500
Converting comments to an answer to not leave the question hanging.
In the initial version of you app.yaml file you only have one handler with the url: / pattern. The /testform request doesn't match this single handler, which explains the initial error message:
The url "/testform" does not match any handlers.
All subsequent attempts went OK from handler mapping prospective (I'd go for the url: .* one as it's shorter, but that's just a personal preference, any of them should do), the error message actually changed into:
localhost is currently unable to handle this request. HTTP ERROR 500
But this error has a different root cause - the resquest vs request typo in the (now properly mapped) TestHandler.get() method.
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