Defining url rules on Google App Engine - google-app-engine

I have all my static files in a folder called html in the root directory. I get the following error while trying to access the index.html in html folder:
-
INFO 2012-07-27 04:07:44,847 dev_appserver.py:2952] "GET /images/logo_footer.jpg HTTP/1.1" 404 -
Here is the folder structure:
Handler code in handlers folder:
class MainHandler(webapp.RequestHandler):
def get (self, q):
if q is None:
q = '../html/index.html'
path = os.path.join (os.path.dirname (__file__), q)
self.response.headers ['Content-Type'] = ContentType.HTML_TEXT
self.response.out.write (template.render (path, {}))
Here is the url rule for images on app.yaml:
- url: /.*
script: notify.app
# image files
- url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
static_files: html/images/\1
upload: html/images/(.*\.(bmp|gif|ico|jpeg|jpg|png))
Am I doing anything wrong here?

You need to move the url : /.* section to after the image files section in app.yaml. These are processed in order, and /.* matches everything, so the second - url: line is never used.

Most likely you have another url that matched "/images/logo_footer.jpg" first, and that served an error.
Also, don't know what the \1 in your static_files path is.

Related

301 redirects for specific url's using Google App Engine(go)?

I am using Google App Engine (go).. and I am looking to redirect https://www.ibikeride.com/scotland/comrie-croft-mountain-bike-trails/amp
to https://www.ibikeride.com/scotland/comrie-croft-mountain-bike-trails
I looked on google app engine docs on redirects but is more than pretty vague. I assume it is a redirect set up in the app.yaml file under handlers.
I actually want to redirect all files ending in "amp" to the same url structure without the amp
if there is a straightforward way of doing that and to avoid me doing a 100+ individual redirect.
For relevant info here is how my current handlers look NB(before attempting this). I have a few handlers to remove the '.html' from the end of url's (for specific categories) and also one at the end to redirect all files to secure 'https'
Any help appreciated
handlers:
# this serves your static/some/path/<file>.html asset as /some/path/<file>.html
- url: /(england/.*\.html)$
static_files: static/\1
upload: static/england/.*\.html$
# this serves your static/some/path/<file>.html asset as /some/path/<file>
- url: /(england/.*)$
static_files: static/\1.html
upload: static/england/.*\.html$
# this serves your static/some/path/<file>.html asset as /some/path/<file>.html
- url: /(scotland/.*\.html)$
static_files: static/\1
upload: static/scotland/.*\.html$
# this serves your static/some/path/<file>.html asset as /some/path/<file>
- url: /(scotland/.*)$
static_files: static/\1.html
upload: static/scotland/.*\.html$
# this serves your static/some/path/<file>.html asset as /some/path/<file>.html
- url: /(wales/.*\.html)$
static_files: static/\1
upload: static/wales/.*\.html$
# this serves your static/some/path/<file>.html asset as /some/path/<file>
- url: /(wales.*)$
static_files: static/\1.html
upload: static/wales/.*\.html$
# this serves your static/some/path/<file>.html asset as /some/path/<file>.html
- url: /(northern-ireland/.*\.html)$
static_files: static/\1
upload: static/orthern-ireland/.*\.html$
# this serves your static/some/path/<file>.html asset as /some/path/<file>
- url: /(northern-ireland.*)$
static_files: static/\1.html
upload: static/northern-ireland/.*\.html$
#redirect always to https
- url: /.*
script: auto
secure: always
redirect_http_response_code: 301
Since you are using the classification part amp at the end instead of the beginning I would suggest you to create a small service that redirects automatically.
For instance in app.yaml:
handlers:
....
- url: /.*/amp
script: main.go
And the code (main.go):
package main
import (
"fmt"
"net/http"
)
type Handler struct{}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
uri := r.URL.Path
length := len(uri)
newUrl := uri[0:length-3] // Remove trailing amp
http.Redirect(w, r, newUrl, http.StatusMovedPermanently)
return
}
func main() {
handler := new(Handler)
http.ListenAndServe(":8080", handler)
}

Google App Engine YAML not finding php in directories

My App Engine isn't loading php files in sub directory
Directory Structure is from the app.yaml file to the corresponding files.
I'm using Google's App Engine with PHP and my YAML code is below
handlers:
- url: /
script: soap/index.php
- url: /(.+)
script: soap/index.php
- url: /getGEO.php
script: soap/getGEO.php
- url: /tests/XML_GEOOffers.php
script: soap/tests/XML_GEOOffers.php "No handlers matched this URL."
- url: /tests/test.php
script: soap/tests/test.php "No handlers matched this URL."
I also having problem to past the parameters for GET in url
localhost:11080/trace ( my script shows false as no paramter sent )
but when i past the parameters into url, it shows blank page instead for true or false
localhost:11080/trace?Pub=0&Adv=0&SDK=0&HWD=c45f9a729cd349bdf3f21e96d305afde1028be99&OS=0&AV=nothing|nothing&BROWSER=IE&Sub=0&campaign=0&Demo=0
I'd greatly appreciate if anybody can help me with this.
You need to put / last, else it matches every url. Same for your /(.+) handler. Try this order:
handlers:
- url: /getGEO.php
script: soap/getGEO.php
- url: /tests/XML_GEOOffers.php
script: soap/tests/XML_GEOOffers.php #"No handlers matched this URL."
- url: /tests/test.php
script: soap/tests/test.php #"No handlers matched this URL."
- url: /(.+)
script: soap/index.php
- url: /
script: soap/index.php
You don't really need one of the last two, as they point to the same place.

URL Handlers Multiple Dir

File Structure
ROOT
- app.yaml
- flashfolder/bin-release
-sfx
-maps
-resources
I've tried,
- url: /flashFolder/bin-release/*(.*\.(gif|png|jpg|ico|js|css|swf|xml|tmx|mp3))
static_dir: \1
upload: static/flashFolder/bin-release/(.*\.(gif|png|jpg|ico|js|css|swf|xml|tmx|mp3))
- url: /flashFolder/bin-release/*(.*\.(gif|png|jpg|ico|js|css|swf|xml|tmx|mp3))
static_files: \1
upload: static/flashFolder/bin-release/(.*\.(gif|png|jpg|ico|js|css|swf|xml|tmx|mp3))
- url: /flashFolder/bin-release/
static_dir: \1
Anybody have a link explaining the the URL handlers, other than what Google provides, the python goes right over my head.
I just wanna upload all the folders within bin-release and their contents without having to specifically map them. Is there a wild card I can throw within the bin-release map?
try:
- url: /flashFolder/bin-release/
static_dir: flashFolder/bin-release/
unless you really need to filter based on the file extension

dev_appserver: where do static routes get routed?

Could anyone point to roughly where in the python sdk code static routes get loaded into or accessed by http_server. This is to debug a failure to load static images. In eclipse I can see the static routes loading into var appinfo from the yaml file, and later can see the dynamic routes being checked during a request, but having trouble following the in-between steps.
Thanks
Update 11/30
Previously tried variations on the yaml, path, etc that were suggested in some docs and postings.
Here is one of them. In this case there is no 404 error, but image doesn't load and Firebug reports "Failed to the load the given URL".
app.yaml
application: crazywidget2
version: 1
runtime: python27
api_version: 1
threadsafe: false
handlers:
- url: /images
static_dir: /images
secure: always
-url: /.*
script: crazywidget2.py
secure: always
libraries:
- name: jinja2
version: latest
index.html
...
<img src="/images/xyz.gif" alt="XYZ illustration" />
...
crazywidget2.py
...
class MainPage(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render({}))
...
...
app = webapp2.WSGIApplication([('/script_send', ScriptSend),
('/resetkey', ResetKey),
('/admin', Admin),
('/start', Start),
('/', MainPage)],
debug=True)
def main():
app.run()
if __name__=='__main__':
main()
Update 12/3
Turns out that in the above case it works if the static_dir is relative, "images" instead of "/images". In the absolute case it tries to open that path as is. Maybe some other variations would work as well.
Here are three relevant code pointers (all in google/appengine/tools/dev_appserver.py):
class FileDispatcher
CreateURLMatcherFromMaps()
DevAppServerRequestHandler._Dispatch()
I would assume though, there are easier ways to debug your problem. If you would post your app.yaml and the path you access and the response you get, people here could start to help you.
Just a hunch: does it work if you add a slash to the end of /images in your handlers? Try replacing /images with /images/ in both places that you use it in app.yaml.

app.yaml url configs for google app engine

I'm relatively new to GAE, and I'm having some difficulty understanding the URL mappings.
I have a set of data that is static (HTML templates, login forms, js etc), and a section that's dynamic.
My current app.yaml has as follows:
handlers:
- url: /.*
static_dir: /static
- url: /service/.*
script: _go_app
login: required
The idea here is that http://myapp/service/foo would route to the app, and that anything else like http://myapp/foo.html should serve /static/foo.html. However, I'm getting a 404 error on the static request.
Ideas?
According to the documentation,
url: A URL prefix. This value uses regular expression syntax (and so regexp special characters must be escaped), but it should not contain groupings. All URLs that begin with this prefix are handled by this handler, using the portion of the URL after the prefix as part of the file path.
In your case since you are specifying url: /.*, the prefix will be foo.html, and the file to fetch would have an empty filename.
Additionally, the handlers are evaluated from top to bottom so you need to change the order.
handlers:
- url: /service/.*
script: _go_app
login: required
- url: /
static_dir: static
Order is important so your /service/ handler is likely never going to be called unless you move it above the static handler. Also, the 404s are caused by incorrect syntax in your static declaration. Change your handlers to:
handlers:
- url: /service/.*
script: _go_app
login: required
- url: /
static_dir: static
A static_dir directive serves files by the name after the prefix that matches the given regular expression. If the RE ends in .*, the entire URL will be considered the prefix, so there will be nothing left over to use as the file path.
Try url: / instead.
Also, handlers are matched in order.
The regular expression /.* matches all URLs you can receive requests for, so anything after it will never match. Put it last.

Resources