GAE - app.yaml for static page AND Python - python

I am trying to create a static html component (that will be usable offline) for a Google App that is otherwise Python.
I can't seem to get the app.yaml file configured correctly for that.
handlers:
# Serve images and JSON as static resources.
- url: /(.+\.(gif|png|jpg|json|ico))$
static_files: \1
upload: .+\.(gif|png|jpg|json|ico)$
application_readable: true
- url: \/(static)\/(index)\.html
static_files: static/\1/index.html
upload: static\/index.html
- url: /
script: roomusage.app
login: required
secure: always
- url: /welcome
script: roomusage.app
login: required
secure: always
- url: /record
script: record_usage.app
login: required
secure: always
Here's the error message I'm getting:
appcfg.py: error: Error parsing C:\gcloud\dev-myapp\app.yaml: Unable to assign value '\/(static)\/(index)\.html' to attribute 'url':
Value '\/(static)\/(index)\.html' for url does not match expression '^(?:(?!\^)/.*|\..*|(\(.).*(?!\$).)$'
in "C:\gcloud\dev-myapp\app.yaml", line 25, column 8.
2017-12-08 09:27:50 (Process exited with code 2)

Your \/(static)\/(index)\.html pattern is an invalid URL regex pattern.
First - the pattern can't start with \ - you don't need to escape /.
The round paranthesis in the pattern are used to identify positional groupings which can then be referred to as parameters by \1, \2, etc in subsequent statements, like static_files, for example. From the url row in the Handlers elements table:
url
Required element under handlers. The URL pattern, as a regular
expression. The expression can contain groupings that can be referred
to in the file path to the script with regular expression
back-references. For example, /profile/(.)/(.) would match the
URL /profile/edit/manager and use edit and manager as the
first and second groupings.
If you don't need such grouping/parameters then don't use the round paranthesis in your patterns. So to match just /static/index.html you could have:
- url: /static/index\.html
static_files: static/index.html
upload: static/index.html
But you should note that the above is redundant if you also have:
- url: /static
static_dir: static

Related

Google App Engine, Web2Py, and Cron, unable to assign value to attribute for url error

I'm having some trouble creating a Google App Engine Cron task for my Web2Py application. I've looked at the instructions for GAE Cron, and this is the task I created as a test:
my cron.yaml file is in the same directory as my app.yaml,
the path to my python controller is applications/data/default, and
the url to access the function is myapp.appspot.com/data/default/test.
This is my cron task entry:
cron:
- description: test
url: data/default/test
schedule: every 2 minutes synchronized
however, I'm getting this error when I try to deploy:
Unable to assign value 'data/default/test' to attribute 'url':
Value 'data/default/test' for url does not match expression '^(?:^/.*$)$'
This is the handlers portion of my app.yaml file:
handlers:
# Warning! Static mapping - below - isn't compatible with
# the parametric router's language logic.
# You cannot use them together.
- url: /(.+?)/static/_(\d+\.\d+\.\d+)\/(.+)
static_files: applications/\1/static/\3
upload: applications/(.+?)/static/(.+)
secure: optional
expiration: "365d"
- url: /(.+?)/static/(.+)
static_files: applications/\1/static/\2
upload: applications/(.+?)/static/(.+)
secure: optional
- url: /favicon.ico
static_files: applications/welcome/static/favicon.ico
upload: applications/welcome/static/favicon.ico
- url: /robots.txt
static_files: applications/welcome/static/robots.txt
upload: applications/welcome/static/robots.txt
- url: .*
script: gaehandler.wsgiapp # WSGI (Python 2.7 only)
secure: optional
I'm pretty lost here, as I'm not too familiar with google app engine or web2py. Any help would be appreciated
You're missing a slash in your cron.yaml url specification:
cron:
- description: test
url: /data/default/test
schedule: every 2 minutes synchronized

Handle favicon in app.yaml configuration

Is it necessary to handle favicon.ico separately like Google Developers Cloud Playground:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
If so, why not:
- url: /favicon.ico
static_files: favicon.ico
upload: favicon.ico
In my real app.yaml I didn't handle favicon.ico separately and it seems to be working:
application: myAppName
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: website/index.html
upload: website/index.html
- url: /
static_dir: website
Inside the website folder I have the following:
images_folder
favicon.ico
index.html
This isn't about your whole question, just a specific part (when you ask why not remove the backslashes in your favicon.ico). The url configuration is actually a regex (see here). The reason you have backslashes is that a "." in regex means any character. The reason this still works is that a literal "." will match something that matches any character. The backslash "escapes" the "." (turns it into a literal "." rather than any character). You will find that without the "\" it would match, for example, "faviconaico". While this would very rarely be an actual problem, it is good practice to escape any literal characters.
Looks like you're trying to serve a static site. I would use:
application: myAppName
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /images
static_dir: website/images_folder/
- url: /.+ # this should handle the favicon.ico, but see below
static_dir: website/
- url: /
static_files: website/index.html
upload: website/index.html
Although, I'd prefer to explicitly state the favicon handler:
- url: /favicon.ico
static_files: favicon.ico
upload: favicon.ico
And, really, I would put the favicon in the images_folder, for a tidier dev environment, but that's a separate discussion.
It is not necessary to handle requests for favicon.ico separately with their own handler in the app.yaml. This is merely a convention since favicons are the most commonly requested icons and generally requested by default on modern browsers.
A generic handler for all .ico files could be used instead but given the somewhat exceptional nature of favicon.ico with default requests from browsers, it's quite common to use a unique handler.

I'm getting a 404 in google cloud endpoints on app engine

In my app, the app.yaml (the relevant part) looks like this:
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /logs
script: logviewer.main.app
- url: /static
static_dir: static
- url: /(.*\.html)
static_files: pages/\1
upload: pages/(.*\.html)
- url: /_ah/spi/.*
script: api.application
- url: .*
script: main.app
I've included all handlers, just to make sure, but I'm quite certain that app.yml isn't the problem.
The relevant part of api.py
#endpoints.api(name='quizservice',version='v01',
description='api backand for quickbuzzer')
class QuizService(remote.Service):
#endpoints.method(messages.VoidMessage , messages.CreateQuizResponse, name="createQuiz")
def createQuiz(self, request):
. . .
application = endpoints.api_server([QuizService],
restricted=False)
Now, when I visit the explorer and try running the QuiizService.createQuiz method, I get a 404 back.
Looking at the logs, I see this:
INFO 2013-04-29 17:53:15,560 server.py:561] default: "GET /_ah/api/discovery/v1/apis/quizservice/v01/rest HTTP/1.1" 200 2738
INFO 2013-04-29 17:53:22,118 server.py:561] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 200 1585
WARNING 2013-04-29 17:53:22,119 api_config_manager.py:201] No endpoint found for path: quizservice/v01
INFO 2013-04-29 17:53:22,119 server.py:561] default: "POST /_ah/api/quizservice/v01 HTTP/1.1" 404 9
I was able to solve the issue by supplying a path parameter to the endpoints.method decorator. What I'm wondering now, is if the endpoints api could choose a default path based on my method name.
Another case where this error can be thrown is with incorrect order of url handlers declaration.
See https://stackoverflow.com/a/15675839/362953
- url: .*
script: main.app
Should come at the end, not before
- url: /_ah/spi/.*
script: api.application
In this case of OP the order is correct.

How to upload sitemap.xml through app engine?

I am using google app engine to host my website. I tried to upload sitemap.xml, but when I open the link i.e., www.example.com/sitemap.xml it shows Oops! This link appears to be broken.. I don't know the reason for this. Can anyone tell me what I am doing wrong?
Here is the content of my app.yaml file
application: mywebsite
version: 1
runtime: python
api_version: 1
handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css))
static_files: \1
upload: (.*\.(gif|png|jpg|ico|js|css))
- url: .*
script: main.py
- url: /icon\.ico
static_files: icon.ico
upload: icon.ico
- url: /sitemap.xml
static_files: static/sitemap.xml
upload: static/sitemap.xml
- url: /static
static_dir: static
From here, "Patterns are evaluated in the order they appear in the app.yaml, from top to bottom. The first mapping whose pattern matches the URL is the one used to handle the request.".
You should try moving the catch all handler (url: .*) to the end of the file.

Using 'secure' and 'login' handlers with WSGI/threadsafe in Appegine

It's extremely unclear to me from the Appengine documentation how to use both the app.yaml configuration settings and the webapp2 framework.
For example, the documentation suggestions this:
- url: /youraccount/.*
script: accounts.py
login: required
secure: always
However, that accounts.py is the CGI form; not compatible with the main.app way of doing things.
Here's some permutations that I've tried and have failed for various reasons:
- url: /.*
script: main.app
- url: /admin/.*
secure: always
login: required
Above fails because a script is required
- url: /.*
script: main.app
- url: /admin/.*
script: main.app
secure: always
login: required
Above fails because the secure and login directives seem to be ignored.
- url: /.*
script: main.app
- url: /admin/.*
script: admin.py
secure: always
login: required
Above fails because the CGI style of handler is not compatible with threadsafe.
Any thoughts?
This:
- url: /.*
script: main.app
- url: /admin/.*
script: main.app
secure: always
login: required
The handlers in the app.yaml file are searched top-down, and the first possible match is used. So, the symptom that "the secure and login directives seem to be ignored" is because the URL is matching the first directive (- url: /.*), and not applying your other options.
Basically, switch the order, with the most specific patterns first.
- url: /admin/.*
script: main.app
secure: always
login: required
- url: /.*
script: main.app

Categories