dev_appserver: where do static routes get routed? - google-app-engine

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.

Related

link that includess .php? in google app engine

I use app.yaml on google's app engine.
I have a link in my php file which is of this format: profile.php?id=1, which gives me the profile page for user 1. Any idea how to deal with this link in my app.yaml file? This is what I have done:
application: myappl-testing-858585
version: 1
runtime: php
api_version: 1
threadsafe: yes
handlers:
- url: /profile.php?id=
script: profile.php?id=
Your app.yaml file should only route the paths, like this:
handlers:
- url: /profile
script: profile.php
(Note that I also removed ".php" from the URL, since you really should not expose the internal file format like ".php", ".html", ".jsp", ".asp", etc. in your URLS... this is an implementation detail of your site, and it's both not good to bother users with it -- it makes for uglier URLs -- and it also makes it more difficult for you to modify your site in the future should you replace one implementation with another).
Then, in your *.php file, you simply use $_GET to test for the existence of / retrieve the ID.
In terms of your site structure, though, you may wish to consider changing the ID to a part of the path rather than a GET parameter if it is always a required parameter (just for URL nicety). In that case, you would register the handler like the following:
handlers:
- url: /profile/(\d+)
script: profile.php
... so that your URLs looked like "/profile/123" instead of "/profile?id=123".

overcome the 100URL limit in app.yaml

I have a php app, for the app engine. My app.yaml file is the following:
application: myproject-testing-112
version: 1
runtime: php
api_version: 1
threadsafe: yes
handlers:
- url: /edit.php
script: edit.php
....
- url: /profile.php
script: profile.php
my problem is that I have about 300 urls (i have 300 php files). As I can see app.yaml allows you 100 URLS. How to deal with this problem? Any ideas?
You can use regex patterns with PHP GAE just like in the Python GAE example referenced in the comment. So, give the following a try:
application: myproject-testing-112
version: 1
runtime: php
api_version: 1
threadsafe: yes
handlers:
- url: /(.+\.php).*
script: \1
To be clear, this one entry will map every URL that matches to a PHP file of the same name.
/edit.php --> edit.php
/profile.php --> profile.php
/profile.php/foobar --> profile.php (the ".*" at the end of the regex allows this behavior)
/someOtherPath will not match the above entry at all, since it doesn't have ".php"
Obviously, you can tweak the regular expression to get the exact behavior you're looking for.

can not figure out relation between yaml and main page handler in google app engine

I've have search a lot in google and stackoverflow but can't figure out qhy my code is not working,
app.yaml file follows:
application: morgan629200774
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /.*
script: main.app
- url: /unit1/
script: unit1.app
- url: /unit2/
script: unit2.app
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
libraries:
- name: webapp2
version: "2.5.2"
and here is my code:
import webapp2
form = """
<form method="post">
Enter some text to ROT13
<br>
<br>
<div><textarea name="content" rows="7" cols="50"></textarea></div>
<input type="submit" value="submit">
<br>
<br>
</form>
"""
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write("main page")
class unit1(webapp2.RequestHandler):
def get(self):
self.response.out.write("hello world")
class unit2(webapp2.RequestHandler):
def get(self):
self.response.out.write(form)
self.response.out.write("hello world")
def post(self):
rot13=''
text=self.request.get('content')
rot13=text.encode('rot13')
self.response.out.write(rot13)
app = webapp2.WSGIApplication([
('/.*', MainPage),
('/unit1/', unit1),
('/unit2/', unit2)
], debug=True)
Can someone tell me what am I doing wrong??
Thanks
You haven't got (and don't need) a unit1.app or unit2.app, so I don't know why you've referenced them in app.yaml. As you can see from the Python code, there is one object called app, which contains the routes for the whole application. I presume (although you don't state) that the Python file is called "main.py". which is why the app.yaml refers to main.app - ie the app object in the main module.
The purpose of the URLs in app.yaml is simply to hand off to the Python code. So, you only need a single handler: the first one. Delete the other two handlers. That captures everything under / and passes it to main.app. In that file, the first route defined at the bottom should be:
('/', MainPage)
because you don't want to capture everything in that route, only the specific root URL.

How to understand chats.html v.s ('/getchats', ChatsRequestHandler)]

I try to understand the different between:
The class ChatsRequestHandler generate a template with the name chats.html
template = self.generate('chats.html', template_values)
In the application view its is named getchats:
application = webapp.WSGIApplication(
[('/', MainRequestHandler),
('/getchats', ChatsRequestHandler)],
The same occurs to me at edit_user.html v.s ('/edituser', EditUserProfileHandler)
How is it that the application knows that the getchats is connected to the chats.html aldo they have not the same name? I would expect that it should be the same name chats.html and ('/chats', ChatsRequestHandler).
The flow of your request goes something like this.
App Engine looks up your app.yaml file. It should contain an entry that says /getchats should be handled by application in somefile.py.
It then goes to this "application view" and matches it to a Webapp Route. In this case, that route is ('/getchats', ChatsRequestHandler).
Then it calls get or post on ChatRequestHandler, passing it the request and response objects.
The output of that is sent back to the user's browser.
You are free to implement ChatRequestHandler as you'd like. In this case you're doing so by reading in a template named chats.html, populating it with some values, and then outputting it.
So the application knows that getchats is connected to ChatRequestHandler. The name of chats.html is pretty arbitrary - the ChatReqeustHandler has to know it, but that is all. You could rename it.
Thanks for helping me:
The example a came up with comes from codenvy.com as a examples app.
1 App Engine looks up your app.yaml file. It should contain an entry that says /getchats should be handled by application in somefile.py.
Here is the app.yaml file of this application
application: 3kus-apps
version: 1
runtime: python
api_version: 1
handlers:
- url: /css
static_dir: css
- url: /js
static_dir: js
- url: /.*
script: devchat.py
So as you can see it contain's no entry that says /getchats should be handled by application in somefile.py.
What i found there is a util.js file witch has a function updateChat(). function updateChat() {downloadUrl(getRandomUrl("/getchats"), "GET", null, onChatsReturned);}.
However, I would like to know - under (1) how this should be handled by a somefile.py.

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