Simple static website in GAE with custom 404 error page - google-app-engine

I am using GAE for a simple static website with just html/htm pages, pictures etc. I am also using Python 2.7.
So i use a straight forward app.yaml and main.py and that works. However, when accessing a page which does not exist, it shows a standard 404 page. I want to change that one into custom error page, and tried this below but it does not work.
here are my app.yaml and main.py files:
application: xxxx
version: 11
runtime: python27
api_version: 1
threadsafe: true
default_expiration: "7d"
inbound_services:
- warmup
handlers:
- url: /
static_files: index.html
upload: index.html
- url: /(.*)
static_files: \1
upload: (.*)
- url: /.*
script: main.app
Main.py:
import webapp2
class BaseHandler(webapp2.RequestHandler):
def handle_exception(self, exception, debug):
# Set a custom message.
self.response.write('An error occurred.')
# If the exception is a HTTPException, use its error code.
# Otherwise use a generic 500 error code.
if isinstance(exception, webapp2.HTTPException):
self.response.set_status(exception.code)
else:
self.response.set_status(500)
class MissingPage(BaseHandler):
def get(self):
self.response.set_status(404)
self.response.write('Page has moved. Pls look at http://www.yyyyyy.yy to find the new location.')
class IndexHandler(webapp2.RequestHandler):
def get(self):
if self.request.url.endswith('/'):
path = '%sindex.html'%self.request.url
else:
path = '%s/index.html'%self.request.url
self.redirect(path)
def post(self):
self.get()
app = webapp2.WSGIApplication(
[ (r'/', IndexHandler),
(r'/.*', MissingPage)
],
debug=True)
What is not correct?? I find a lot of entries, but none exactly explains how to do this for a simple website with Python 2.7,
let me know, many thanks, Michael

it looks like it doesn't really need to have any dynamic part of your website except the 404 page.
There is an error_handlers can be used directly.
https://developers.google.com/appengine/docs/python/config/appconfig#Custom_Error_Responses
application: xxxx
version: 11
runtime: python27
api_version: 1
threadsafe: true
default_expiration: "7d"
inbound_services:
- warmup
handlers:
- url: /
static_files: index.html
upload: index.html
- url: /(.*)
static_files: \1
upload: (.*)
error_handlers:
- file: default_error.html

Related

ImportError using Flask and GAE

I'd like the app engine to associate index.html with the root URL and main.app with /stats. Here's my app.yaml:
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /
static_files: index.html
upload: index.html
- url: /stats.*
script: main.app
- url: /(.*)
static_files: \1
upload: (.*)
If the URL is /stats, I'd like to print a short message. Here's the code in main.py:
import logging
from flask import Flask
app = Flask(__name__)
#app.route('/stats')
def stats():
return 'Hello World!'
When I try to access /stats, the GCP log says ImportError: No module named main. How can I fix this?
Looks like you entered in a conflict between the /stats handler and the /(.*) handler. As per the documentation for static_files:
If a static file path matches a path to a script used in a dynamic handler, the script will not be available to the dynamic handler.
So, either remove the /(.*) handler, or, as you intent to serve static files with it, I recommend using a handler like the one described in the documentation:
- url: /(.*\.(gif|png|jpg|whateverextension))$
static_files: static/\1
upload: static/.*\.(gif|png|jpg|whateverextension)$
Also, don't forget to add the Flask library to your app.yaml file:
libraries:
- name: flask
version: 0.12

main domain + subdomain + CSS not found

I have one dispatch.yaml that splits 2 services; admin_main.py that controls the admin login, and main.py that controls the user landing. My problem is that admin_main.py does not see its own CSS that I directed it to it. However, it keeps matching with the main.py CSS.
my files are structured as
admin
|assets
|CSS
+styles.min.css
www
|assets
|CSS
+styles.min.css
dispatch.yaml:
dispatch:
# Default service serves simple hostname request.
- url: "example.net/"
service: default
# Default service serves simple hostname request.
- url: "app-example.appspot.com/"
service: default
# Default service serves simple hostname request.
- url: "admin.example.net/"
service: admin
- url: "admin-dot-app-example.appspot.com/"
service: admin
admin_main.py:
service: admin
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /assets/css/styles.min.css
static_files: admin/assets/css/styles.min.css
upload: admin/assets/css/styles.min.css
- url: /.*
script: subdomain.app
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
main.py:
service: default
runtime: python27
api_version: 1
threadsafe: yes
default_expiration: "4d 5h"
handlers:
- url: /assets/css
static_dir: www/assets/css
- url: /assets/img
static_dir: www/assets/img
- url: /.*
script: main.app
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
in the .html the links to the CSS are
<link rel="stylesheet" href="assets/css/styles.min.css">
for both landing sites. But that shouldn't be a problem since the dispatch.yaml separate the incoming calls, right?
I'm not really sure what is causing the problem. Also, I'm new to yaml and I been reading it's documentation.
One solution for this is to store every CSS in the same file, and give them different names. Then in app.yaml in the handlers section add this:
- url: "/assets/css/(.*\\.(css))$"
static_files: {CSS_DIR_IN_PROJECT}/\1
upload: {CSS_DIR_IN_PROJECT}/.*\.(css)$
application_readable: true
that was my only way of solving the problem

404 page doesn't show when accessing a missing file in a static directory

I'm working on porting my blog from GitHub pages to Google App Engine. I've set up my app.yaml as follows.
runtime: python27
api_version: 1
version: 5
threadsafe: yes
handlers:
- url: /api/.*
script: main.app
- url: /
static_files: src/_site/index.html
upload: src/_site/index.html
- url: /(.*)/
static_files: src/_site/\1/index.html
upload: src/_site/*.*
- url: /(.*)
static_files: src/_site/\1
upload: src/_site/(.*)
error_handlers:
- file: static/404.html
You can get to my 404 page directly by going to http://joshuasnidercom.appspot.com/404.html and you can get to by accessing a missing page that the flask app handles like http://joshuasnidercom.appspot.com/api/invalidurl, but going to any page that is covered by the static files like http://joshuasnidercom.appspot.com/nonexistentpage just shows a Error: Not Found The requested URL /nonexistentpage was not found on this server. error.
What should I be doing differently?
I believe you need to create an error handler in your views file.
You can do this with the following code:
#app.errorhandler(404)
def page_not_found(e):
return render_template("404.html"),404
You can also replace 404 with 500, 403, etc,for other errors.

Google app engine running error

When I was trying to run one of the application on google app engine I keep on getting a yell icon the means error. How can I fix it ,so I can run my application.
The error is one the left end side of hello-udacity
Here is the link of the picture of the error: https://plus.google.com/u/1/115545843446144625696/posts/aoRyoN2r2Sd?pid=6168940381509733874&oid=115545843446144625696
In main.py
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello udacity!')
app = webapp2.WSGIApplication([('/', MainHandler)],
debug=True)
In app.yaml
application: hello-udacity
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
The app.yaml file uses the YAML syntax and so your app.yaml should comply with those. The following is a clearer configuration
application: hello-udacity
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
For more on this see about app.yaml in the developer docs

GAE WSGI Application URL Mapping

I have an App Engine project structure setup as follows:
ProjectRoot
app.yaml
index.yaml
main.py
static [directory]
index.html
app [directory]
script1.py
script2.py
My app.yaml looks like this
application: appname
version: 1
runtime: python27
api_version: 1
threadsafe: no
handlers:
- url: /(.*\.html)
mime_type: text/html
static_files: static/\1
upload: static/(.*\.html)
expiration: "1h"
# application scripts
- url: /app/(.+)
script: main.py
# index files
- url: /(.+)/
static_files: static/\1/index.html
upload: static/(.+)/index.html
expiration: "15m"
- url: /(.+)
static_files: static/\1/index.html
upload: static/(.+)/index.html
expiration: "15m"
# site root
- url: /
static_files: static/index.html
upload: static/index.html
expiration: "15m"
libraries:
- name: webapp2
version: "2.5.1"
My main.py is simply the default 'Hello World' sample application:
#!/usr/bin/env python
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write('Hello world!')
#print("Executing script!")
app = webapp2.WSGIApplication([(r'/app/(.*)', MainHandler)],
debug=True)
Now, the static html can be accessed as expected. The url mapping to the main.py script specified in app.yaml works and I know that the script is getting executed. The trouble I am having is with the URL mapping to be specified to WSGIApplication in main.py. I want to be able to access the application script using the url: localhost:808x/app/something
I have already tried using the patterns:
r'/app/(.*)'
r'/(.*)'
r'/'
r'/app/'
None of the above patterns lead to the 'get' response handler being invoked (i.e. I don't get the 'Hello World' response). I have tried gleaning what I am doing wrong from the documentation. I think it all boils down to my only just coming to grips with regular expressions. Would someone possibly be able to point me to what pattern I need to map the application handler to?
How about this pattern?
r'/app/.*'
If there is any regexp grouping, you need arguments for the view function.
Additionally, you need to add main() function in your main.py if you specify your script in the form like main.py. The main() function looks like:
from google.appengine.ext.webapp.util import run_wsgi_app
...
...
def main():
run_wsgi_app(app)
if __name__ == '__main__':
main()
You can also use this form:
script: main.app
With the latter form, you don't need the main() function.

Resources