unable to redirect to a particular url - google-app-engine

Hi I am building a web site.
here requirement is to build a url like
http://localhost:10080/edit/?d=2014-02-22%2015:36:38.688000
and it have to be processed bu application
application = webapp2.WSGIApplication([
('/edit',EditPost),
('/',HomePage),
('/post',PostPosting),
], debug=True)
I am using above but its not recognizing EditPost class
my app.yaml is
application: your-app-id-indoor
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: IndoorBlog.application
- url: /stylesheets
static_dir: stylesheets
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
---could some one help me please...
thanks

According to your webapp2.WSGIApplication Class, you are handling edit url as http://localhost:8080/edit not /edit/.
If you need to handle this url http://localhost:10080/edit/?d=2014-02-22%2015:36:38.688000,
then webapp2.WSGIApplication class should be like this:
application = webapp2.WSGIApplication([
('/edit/',EditPost),
('/',HomePage),
('/post',PostPosting),
], debug=True)

Webapp2 routing has named routes for redirect_to:
Docs: https://webapp-improved.appspot.com/api/webapp2.html#webapp2.redirect_to

Related

App Engine dispatch.yaml doesn't affect service versions without traffic

Hi we are using App engine with many new and legacy java services.
we have 1 new frontend angular app and multiple services in java8
we access the application directly using without custom domains
frontend-dot-project-id.appspot.com
legacyapp-dot-project-id.appspot.com
My issue is the dispatch file doesn't affect app URLs with version specified
which broke the apis in the applications if opened in version url
for example :
frontend-dot-project-id.appspot.com/api/v2 => works
ver-dot-frontend-dot-project-id.appspot.com/api/v2 => doesn't work and will take me to frontend application as if there is no dispatch routes deployed
my dispatch.yaml looks something like this:
dispatch:
- url: "*/api/v2/*"
service: api-back
- url: "*/api/v1/*"
service: legacy-back
my app.yaml for the angular app
service: frontend
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /(.*\.(css|eot|gz|html|ico|js|map|png|jpg|svg|ttf|woff|woff2|json)(|\.map))$
static_files: dist/app/\1
upload: dist/app/(.*)(|\.map)
- url: /.*
static_files: dist/app/index.html
upload: dist/app/.*
login: required
skip_files:
- e2e/
- node_modules/
- src/
- coverage
- ^(.*/)?\..*$
- ^(.*/)?.*\.md$
- ^(.*/)?.*\.yaml$
- ^LICENSE
thanks in advance

Angular frontend with django rest backend on google cloud app engine error 502 Bad Gateway

I have deployed my angular front end with Django Rest Framework backend on Google App Engine. When I make a request to the backend from the frontend I get an error 502 Bad Gateway any help on identifying the problem will be really appreciated. I have tried several online recommendations are not working for me.
This is my front end app.yaml
runtime: nodejs12
handlers:
- url: /
static_files: smis/index.html
upload: smis/index.html
secure: always
- url: /
static_dir: smis
secure: always
This is my backend app.yaml file
runtime: python38
service: backend
handlers:
- url: /static
static_dir: /static/
secure: always
- url: /.*
script: auto
secure: always
This is my dispatch.yaml file
#routing rules
dispatch:
#api
- url: "*/api/*"
service: backend
The leading wildcard in your url routing is invalid. Try this:
#routing rules
dispatch:
#api
- url: "/api/*"
service: backend
Then, any url that begins with /api/... will go to the python backend
I found out I had not set main.py file. App engine handles request in the main.py file which should be in the root directory. The contents of main.py file can be derived from wsgi.py file. This is the content I place in the main.py file and it worked for me:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'smis.settings')
application = get_wsgi_application()
app = application
```

Hosting pure angularjs app on google app engine

I have an angular js app with following structure
app structure
my app folder look like
app folder
the app does use any back end interaction for now but in future its gonna interact with a separate app engine java project . I want to host this angularjs app to google app engine but I am not able to understand the right configuration . I am more confused how do I set up the app.yaml for google app engine ... and is it necessary to have a main.py file as at present I do not have any handler
If I understand correctly, you would like to host a static web page on app engine, specifically an AngularJS app.
You don't need any server side code and can configure your app.yaml as follows:
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/(.*)
This is assuming you will host your client side code in a www folder and you use a index.html as your index file.
Also take a look at the following guide for hosting a static website [0].
[0] https://cloud.google.com/appengine/docs/standard/python/getting-started/hosting-a-static-website

Using Google App Engine how do I open an html page?

Using Google App Engine I hope to learn some very basic knowledge with this question. I want to be able to open an index.html page that is placed in a folder when I open the application.
I generated a new application using 'Google App Engine Launcher'
I slightly modified the app.yaml and it now looks like the following...
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /templates
static_dir: templates
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
I also have a added a directory called 'templates'.
In the directory I placed a file called 'index.html'.
<html>
<header><title>This is title</title></header>
<body>
Hello world cls
</body>
</html>
My main.py hasn't been modified so it looks like
#!/usr/bin/env python
#
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world!')
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
Any ideas on what I need to do or change to find success?
Regards,
Chris
>
My code has changed in a big way because of a comment from Gwyn
I now have the standard Django template code from link (https://console.developers.google.com/start/appengine)
Gwyn eventually the index.html file will be something a little more than a static page so a direct URL that you described won't work. You did teach me though and that will come in handy as I progress. I want to bring in some Polymer code once I get the basics figured out here...
So if anyone can help me feed up a hello world from within a 'templates' folder using an index.html page from a standard django codeset generated from the Google Developers Console then your answer would be very much appreciated here.
Regards,
Chris
First, to use HTML, do you need a template engine, in app engine you can use this EZT, Cheetah, ClearSilver, Quixote, Django, and Jinja2 but for simplicity, you can modify your code to send the html directly
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('<html><header><title>This is title</title></header><body>Hello world cls</body></html>')
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
But as Gwyn Howell says, for more complicated stuff you must use a template engine
I wanted to update this so if anyone else is struggling they may find success also...
I want to thank both of Gwyn Howell and Kristian Damian. I used both of your comments to come up with an answer for anyone else that has the same question.
I went to clarify that I am using the downloaded Python SDK for Google App Engine
I made a sample python project using File|Create New Application - Python 2.7
I ran the project to make sure it was working without any of my changes
I then decided I would use 'Jinja2' for my template engine
I made a 'templates' folder and placed my 'about_v2.html' file in it
I modified the 'app.yaml' code to read
application: helloworld
version: 2
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: latest
- name: jinja2
version: latest
I modified the 'main.py' code to read
import os
import webapp2
import jinja2
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__),
'templates')))
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world! About.<br />')
class AboutPage_v2(webapp2.RequestHandler):
def get(self):
template_values = {
}
template = env.get_template('about_v2.html')
self.response.out.write(template.render(template_values))
app = webapp2.WSGIApplication([
('/', MainHandler),
('/about_v2.html', AboutPage_v2)],
debug=True)
I then ran the project on my local machine and was able to get to the url
I hope this answer helps someone else.

App Engine can't find static files

I'm using webapp2 and python 2.7 on my app engine application. However, I'm having a problem with the url on app.yaml.
I have my static files inside a static/ directory, in the root of my path. So inside of static/ I have static/scripts/MyScript.js
When I set app.yaml like:
application: myapp
version: 1
runtime: python27
threadsafe: true
api_version: 1
handlers:
- url: /.*
script: myapp.app
- url: /static
static_dir: static
libraries:
- name: webapp2
version: latest
In my HTML code, the js is called like:
<script src="static/scripts/Fuel.js"></script>
But, the file is not loaded, and I get a 404 error. (This problem happens also for .css files.)
However if I change the first url in app.yaml to:
handlers:
- url: /
script: myapp.app
The static files are loaded, but when I try calling the routes url in my app, like an url I call in a form to save some data on server, this route is not found and I also get a 404 error.
Here is my routing code in myapp.py file.
app = webapp2.WSGIApplication(
[('/', MainHandler),
('/save', SaveData),],
debug=True)
So if I try to access myapp.appspot.com/save, it returns me a 404 error, saying:
The requested URL /save was not found on this server.
Any ideas? If you need more information about it, just ask on the comments.
Thanks.
Put your catch all url /.* at the bottom since this is evaluated in order. Meaning it never pass after that.
handlers:
- url: /static
static_dir: static
- url: /.*
script: myapp.app

Resources