App Engine; File referenced by handler not found: main.py - google-app-engine

First post here at stack overflow. Please forgive my posting errors.
I have spent a lot of time at this. I started with the 500 server error.
This long is stating python not found. My app is JS, CSS, and HTML only. (at this point) I have included the yaml, because I cant rule out for myself if I have errors there through my research.
Pointers are greatly appreciated.
Thanks.
My app.yaml:
application: application
version: secureable
runtime: python27
api_version: 1
threadsafe: false
handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css))
static_files: \1
upload: (.*\.(gif|png|jpg|ico|js|css))
- url: /robots.txt
static_files: robots.txt
upload: robots.txt
- url: .*
script: main.py
inbound_services:
- mail
The error:
httpRequest: {
status: 500
0: {
logMessage: "File referenced by handler not found: main.py"
severity: "WARNING"
time: "2017-09-24T21:12:30.191830Z"
}
]
megaCycles: "2"
method: "GET"
requestId: resource: "/index.html"
startTime: "2017-09-24T21:12:30.138333Z"
status: 500
traceId: "618d060203d57aea2bfddc905e350698"
urlMapEntry: "main.py"
userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0"
versionId: "secureable"
}
receiveTimestamp: "2017-09-24T21:12:30.926277443Z"
resource: {
labels: {
module_id: "default"
project_id: "Application"
version_id: "secureable"
zone: "us9"
}
type: "gae_app"
}
severity: "WARNING"
timestamp: "2017-09-24T21:12:30.138333Z"
}

If your app is only HTML, CSS, and JS, you can remove the catch-all pointer to the Python script all together and instead use an app.yaml format like the one shown in the Hosting a Static Website on App Engine tutorial:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: www/index.html
upload: www/index.html
- url: /(.*)
static_files: www/\1
upload: www/(.*)
Later if you want to add server-side logic with a Python module, you can add in a handler with a script associated with it. When you take that step, you use an import style pointer in the form of [script_name].[var_pointing_to_wsgi_application_in_script]. So if you have main.py and within that a variable called application that is set to your WSGI application, then you would use script: main.application.
Commonly a WSGI application is either webapp2 (example) or Flask (example).

Your script: main.py statement in the handlers section of the app.yaml file is wrong, it should be script: main.app.
From the script row in the Handlers element table (sadly not properly formatted, including the quote from the page source to make it readable):
script
A script: directive must be a python import path, for example,
package.module.app that points to a WSGI application. The last
component of a script: directive using a Python module path is
the name of a global variable in the module: that variable must be a
WSGI app, and is usually called app by convention.

Related

Why does Google App Engine Standard Edition for Java create a __static__ folder and double the number of files deployed to a project?

I am deploying a web application to Google App Engine Standard edition using Java 8.
It is a documentation web site plus servlets that includes a huge amount of static web content (built with Jekyll).
I recently started getting this error trying to deploy the app:
ERROR: (gcloud.app.deploy) INVALID_ARGUMENT: This deployment has too many files.
New versions are limited to 10000 files for this app.
- '#type': type.googleapis.com/google.rpc.BadRequest
fieldViolations:
- description: This deployment has too many files.
New versions are limited to 10000 files for this app.
field: version.deployment.files[...]
My src/main/webapp folder has 5016 files.
But after running:
mvn clean install appengine:stage
my target/appengine-staging folder has 10113 files.
I see that the files are in 2 locations, both
target/appengine-staging
and
target/appengine-staging/__static__
Why do they appear twice?
Is this normal?
Or am I doing something wrong?
How can I avoid this duplication?
Or do I have to limit my files to under 5000?
Thanks for any advice.
Note: The generated app.yaml looks like this:
runtime: java8
instance_class: F1
inbound_services:
- warmup
derived_file_type:
- java_precompiled
threadsafe: True
auto_id_policy: default
beta_settings:
'source_reference': 'https://...'
api_version: 'user_defined'
handlers:
- url: (/.*/)
static_files: __static__\1index.html
upload: __NOT_USED__
require_matching_file: True
login: optional
secure: always
- url: (/)
static_files: __static__\1index.html
upload: __NOT_USED__
require_matching_file: True
login: optional
secure: always
- url: (/.*)
static_files: __static__\1
upload: __NOT_USED__
require_matching_file: True
login: optional
secure: always
- url: /.*
script: unused
login: optional
secure: always
skip_files: app.yaml

GCloud Error: Not Found The requested URL was not found on this server

I followed this steps to deploy my angular 8 application in Google Cloud
https://medium.com/#karthiksagar/how-to-deploy-angular-8-app-on-google-cloud-platform-gcp-cdd79e5cc5cf
My app.yaml is as follows
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: sofbox-angular/index.html
upload: sofbox-angular/index.html
- url: /
static_dir: sofbox-angular
I have placed the app.yaml file inside dist/ directory as mentioned.
I am able to access the main url- http://www.myridemate.com/
but when clicked on blog the link is not accessible - http://www.myridemate.com/blog
It gives below error
Error: Not Found The requested URL /blog was not found on this server.
Tried adding extra handler for /blog in app.yaml and it didn't work.
This same code is working in my local and also in hostgator - https://www.myridemate.alexvijayraj.com/blog
Not sure what I am missing here. Is there some extra configuration required in app.yaml or anything in GCP?
Error Message in App Engine Logs:
{
"time": "2020-09-17T03:58:28.682252Z",
"severity": "WARNING",
"logMessage": "Static file referenced by handler not found: sofbox-angular/blog"
}
The answer in this thread worked for me. Angular 6 routes not found on Google App Engine
Remember to remove the two handlers that are already on the .yaml file (-url: ...)
handlers:
- url: /(.*\.(js|css|svg|png)(|\.map))$
static_files: dist/\1
upload: dist/(.*)(|\.map)
- url: /.*
static_files: dist/index.html
upload: dist/.*

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

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.

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