GAE WSGI Application URL Mapping - google-app-engine

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.

Related

Unable to look up other files in appspot.com - problem with app.yaml

I have these files: index.php, filecallup.js, image.png, userprofile.php deployed in my xxx.appspot.com.
The index.php file is expected to collect some URL query variables and open the userprofile.php.
My problem is that when I click on the link (https://xxx.appspot.com/?user=peter) it still points to the index.php file and doesn't load userprofile.php.
I think it's a problem with my app.yaml file because it renders well in other servers except for GAE.
Here is the app.yaml code:
#Use the PHP 7.3 runtime (BETA) by replacing "php72" below with "php73"
runtime: php72
#entrypoint: serve main.php
service: default
#threadsafe: true
handlers:
# Serve a directory as a static resource.
#- url: /stylesheets
# static_dir: stylesheets
# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
static_files: \1
upload: .+\.(gif|png|jpg)$
- url: /(.+\.(htm|html|css|js))$
static_files: \1
upload: .+\.(htm|html|css|js)$
# Serve your app through a front controller at index.php or public/index.php.
- url: /mail.php
script: auto
Here is the index.php code:
<?php
some codes here`
header('Location:userprofile.php')
>?

google app engine: url not found on this server/404 error

I have code cloned from GitHub Zorya. I just added a www folder and an index.html file in it as I read somewhere that error was because there was no www directory.
Here's how my app structure looks like:
My app.yaml file:
runtime: python27
api_version: 1
threadsafe: true
service: default
builtins:
- deferred: on
# Handlers define how to route requests to your application.
handlers:
- url: /api/v1/(.*)
script: main.app
- url: /tasks/(.*)
script: main.app
- url: /
static_files: build/index.html
upload: build/index.html
- url: /favicon\.png
static_files: build/favicon.png
upload: build/favicon\.png
# unused for now
# - url: /service-worker\.js
# static_files: build/service-worker.js
# upload: build/service-worker\.js
- url: /manifest\.json
static_files: build/manifest.json
upload: build/manifest\.json
- url: /static/(.*)
static_files: build/static/\1
upload: build/static/(.*)
- url: .*
static_files: build/index.html
upload: build/index.html
# here if you want to use them. See
# https://developers.google.com/appengine/docs/python/tools/libraries27 for
# a list of libraries included in the SDK. Third party libs that are *not*
part
# of the App Engine SDK don't need to be listed here, instead add them to
your
# project directory, either as a git submodule or as a plain subdirectory.
#libraries:
#- name: jinja2
# version: latest
libraries:
- name: ssl
version: latest
- name: numpy
version: "1.6.1"
skip_files:
- ^\.git$
- ^\client$
- ^\venv$
# needed for dev_appserver.py, tracks too many changes otherwise
- .*/zorya/client
Here's one of the errors that I see in the logs :
Your requests for /favicon.ico (shown in the comment log) and for / (from the logs image) both match the .* handler pattern, for which you have configured serving a build/index.html static resource.
But you don't have a build directory under the zorya app/service directory, so your static resource doesn't exist. Hence the 404 error.
Maybe you mean to use www instead of build? If so you should match the name of the directory with the one used in the handler pattern. You could just rename the www directory to build (no, you don't need to use that exact name).
In particular for the favicon error, you may want to specify a handler for favicon.ico instead of favicon.png

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

Simple static website in GAE with custom 404 error page

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

Resources