Appengine: Routing to a different appspot.com - google-app-engine

I have a app
abc.appspot.com
Now, I want a url abc.appspot.com/blog. This url should serve from abcblog.appspot.com. I don't want a redirect.
Is this possible with dispatch.yaml
Basically, I want to introduce a blog in my app, but from a different appspot.com.

You can use the default routing by routing via URL or you can use routing with a dispatch file.
For your case I think creating a dispatch file will be the suitable way to do what you want since it will override the App Engine's routing rules and you can define your own custom routing rules.
To create a dispatch file you should put it in your root directory of your default service and you can define up to 20 routing rules in the dispatch file.
An example from the documentation:
dispatch:
# Send all mobile traffic to the mobile frontend.
- url: "*/mobile/*"
service: mobile-frontend
# Send all work to the one static backend.
- url: "*/work/*"
service: static-backend
You can check this to see how to properly configure your dispatch.yaml.

Related

AppEngine Standard: how to dispatch based on service name and version?

Is there a way via the dispatch.yaml file to dispatch a request to the right service based on the version and the service name ?
I mean, I have this request:
https://va4-0-0-dot-api-acceptance-dot-myapp.appspot.com/auth/user
It is sent by my api-acceptance service but I'd like it to go to my auth-acceptance service.
I have written in my dispatch those rules, but it does not work...
- url: "va4-0-0.myapp.appspot.com/auth/*"
service: auth-acceptance
- url: "api-acceptance.myapp.appspot.com/auth/*"
service: auth-acceptance
According to the documentation, the URL pattern that you're using:
https://[VERSION_ID]-dot-[SERVICE_ID]-dot-[MY_PROJECT_ID].appspot.com
will be bypassed by the dispatch.yaml file, so it's not possible to reroute the request this way.
Using the above pattern you are already being very specific to which service and version you want to route the request.
So either you change the URL to https://[VERSION_ID]-dot-auth-acceptance-dot-[MY_PROJECT_ID].appspot.com/auth/user or use a default routing which can later be overriden by a dispatch.yaml.

How to make a service discovery for Google App Engine? How to get service URL?

I want to get URL's of services for particular project, how can I do this?
I need URL's like .appspot.com
I tried App Engine Admin api, but it can only provide names of the services.
you can get url's of App Engine services's versions through API calls, in 3 steps:
1) authenticate and get an access-token to the App Engine Admin API:
gcloud auth application-default print-access-token
2) with the access token, list all services in App Engine, and get their version ID (in the nested field "allocations"), and service ID:
curl -H "Authorization: Bearer [MY_ACCESS_TOKEN]" https://appengine.googleapis.com/v1/apps/[MY_PROJECT_ID]/services
3) with the version ID and service ID, get the full data on the version:
curl -H "Authorization: Bearer [MY_ACCESS_TOKEN]" https://appengine.googleapis.com/v1/apps/[MY_PROJECT_ID]/services/[SERVICE_ID]/versions/[VERSION_ID]/?view=FULL
The field versionUrl delivers the app URL for this specific version, in the following form:
default service:
https://[VERSION_ID]-dot-[PROJECT_ID].appspot.com
other services:
https://[SERVICE_ID]-dot-[VERSION_ID]-dot-[PROJECT_ID].appspot.com
From there, you can build your own service discovery.
The URL of a service other than default follows this pattern:
service-dot-projectID.appspot.com
So for example if your service's name is helloworld and your project ID is myprojectid then the URL would be helloworld-dot-myprojectid.appspot.com
If you're manually constructing urls to services within the same project, you may find it easier to setup some dispatch rules.
https://cloud.google.com/appengine/docs/standard/python3/reference/dispatch-yaml
The dispatch.yaml allows you to override routing rules. You can use the dispatch.yaml to send incoming requests to a specific service (formerly known as modules) based on the path or hostname in the URL.
Depending on your needs, you can construct your url off of app_identity.get_default_version_hostname() without worrying about the underlying service, since the dispatch rules will route to the service by the url path.

Authenticating a user in static_dir single-page-app so that it can call sibling GAE APIs

I have a React app that is being served on GAE via a static directory.
app.yaml:
- url: /my_admin_app
static_dir: admin_app
login: required
secure: always
When accessing via browser, GAE presents a login page as expected before continuing to the React app.
Because the React app is completely separate from the GAE app that is also running, I need the web app to call APIs that should require auth, as they control sensitive data.
The React app is calling sensitive APIs behind this URL:
app.yaml:
- url: /admin/.*
login: required // this causes a login page to be sent instead of data
script: main.app
secure: always
Is there a better way to serve my static files such that login isn't required again? Or is there a way to pass along the auth info when GAE presents its own login page?
I've learned that when Google presents the login, it passes along the auth cookie to the following web page.
Then, any requests that the page makes using fetch should specify that the cookie should be passed along with that request.
From the Mozilla docs: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
By default, fetch won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set).
So to make sure the auth gets passed on:
// to only pass to to same origin endpoints
fetch('/endpoint', {credentials:'same-origin'}).then(/*..*/)
// to pass without restriction
fetch('/endpoint', {credentials:'include'}).then(/*..*/)

Use Angular routing in an Apache-hosted app

My app's frontend is done in Angular while the backend is in PHP. For this last reason, I am using Apache as app server.
I want to use Angular's routing feature, that is, $routeProvider.when(), with conditional params (aka named groups) such as /user/:id/, where :id would be a parameter passed to the controller specified in the route.
Obviously, Apache tries to handle the request e.g., /user/21 by looking for a resource called 21 inside of the user directory, and thus returns a 404 error, instead of letting Angular routing load the resource at /user and using the value 21 to do internal stuff (such as calling an API).
How would I have to setup Apache so that some requests are left to be handled by Angular?

Backbone swtich from Http to Https

I have certain knowledge using Bakcbone.js, and my backend is Restlet Java, but have no idea regarding user authentication
I have built a single-page web app using them, but now the problem arises that, what I am going to do after user login? There are pages that can be viewed by both logged in and not logged in user, and logged in users should be able to see additional content.
By default the page uses http, but after user Login, suppose an Ajax Post, how do I switch entire Backbone App from http to https? And suppose user logs out, how do I switch back?
Are there any convient ways just to switch all my routes in Router, Url/Urlroots in Collection/Model between Http and Https? (using relative address)
Can I deal with this using Server Redirect..and how can I do that, doesn't that make Http and Https sections completely separated like two apps?
woha - that's a lot stuff you are thinking ..half of which doesn't even belong to Backbone's scope.
Let's split this up:
but have no idea regarding user authentication
read this. I just answered this yesterday.
what I am going to do after user login?
the normal pattern here is that login is usually one page. If the user successfully logs in then she is redirected to another page which has all the Backbone stuff in it e.g. take a look at a backbone app classdojo.com . Login is simple HTML with no fancy stuff. Once user logs in, she navigates to a single-page app with all heavy client side.
Are there any convient ways just to switch all my routes in Router, Url/Urlroots in Collection/Model between Http and Https? (using relative address)
Backbone Router ONLY looks at the route which comes '#' e.g. in example.com/user#details Backbone router will only navigate based on #details . It has nothing to do with your http protocol.
Your Url/Urlroots can be relative or absolute both. So you can specify full URL with http protocol in them.

Resources