Google App Engine routing with dispatch.yaml - google-app-engine

I try to deploy a web app on my sandbox GCP project where there is already an app deployed. So I'm trying to play with the path to have the two web apps deployed at the same time.
My dispatch looks like this
dispatch:
- url: "*/wc/api/.*"
service: wc-api
- url: "*/wc/.*"
service: wc-front
- url: "*/.*"
service: default
When I do so, all my calls to mysandbox.appspot.com/wc/ gets redirected to my default service and I don't understand why (I can see the calls in the logs of the default service).
If this helps, here is the app.yaml of my wc-front service.
runtime: python27
api_version: 1
threadsafe: yes
service: wc-front
default_expiration: "10m"
handlers:
- url: /wc/.*
script: app.APP
login: required
secure: always
Do you see any error in this ?
(Calling directly wc-front-dot-mysandbox.appspot.com/wc/ returns the typical App Engine 404 error)
Thanks

It appears the problem was coming from the .* notation. This should only be used for the very general */.* rule.
My new - working - dispatch
dispatch:
- url: "*/wc/api/*"
service: wc-api
- url: "*/wc/*"
service: wc-front
- url: "*/.*"
service: default

Yes, indeed, you need to configure your dispatch.yaml file, for App Engine to route your application based on the URL that you set there. It seems that your service: default it's getting all URLs and redirecting them to the service set there.
Considering that, I would recommend you to take a look at the official documentation about configuring the dispatch.yaml file - you can get some better ideas on how to configure it - and this other post from the Community, where another user has a similar use case as yours, that I believe should help you.
dispatch.yaml Configuration File
How to use GAE's dispatch.yaml with multiple development environments?
Let me know if the information helped you!

Related

App Engine matching any "subdomain" to my service

I have a Strapi application on Google App Engine as the Default service.
The default URL App Engine generates is https://my-project.uc.r.appspot.com
When I create any other version for my default service or deploy another service, the new URLs would be something like: https://[identifier]-dot-my-project.uc.r.appspot.com
My problem is that if I replace [identifier] with anything at all it opens my Strapi Application root page.
I don't think this has anything to do with Strapi at all, it's probably a feature of App Engine.
My question is: How do I stop this from happening? I want only proper URLs to be matched. That is, if I create a "dev" version, I should be able to access it with the following URL: https://dev-dot-my-project.uc.r.appspot.com, but I don't want any other URL to be matched, like: https://12345-dot-my-project.uc.r.appspot.com
I am using a Standard Environment with the default app.yaml from Strapi docs
runtime: nodejs16
instance_class: F2
env_variables:
HOST: '0.0.0.0'
NODE_ENV: 'production'
DATABASE_NAME: 'strapi'
DATABASE_USER: 'postgres'
DATABASE_PASSWORD: '<password>'
INSTANCE_CONNECTION_NAME: '<instance_identifier>'
beta_settings:
cloud_sql_instances: '<instance_identifier>'
When the app is deployed to App Engine, the app.yaml is automatically modified to add some default params.
runtime: nodejs16
env: standard
instance_class: F2
handlers:
- url: .*
script: auto
I thought maybe this url: .* was the cause of this and tried to change it to url: /.* (Docs), but App Engine still add the url: .* again anyway at the end and it will have both handlers.
This is expected behavior. Per the documentation
If a request matches the PROJECT_ID.REGION_ID.r.appspot.com portion of the hostname, but includes a service, version, or instance name that does not exist, then the request is routed to the default service.
In your example, when you hit the url - https://12345-dot-my-project.uc.r.appspot.com and it turns out '12345' is not a valid version, the default service - https://my-project.uc.r.appspot.com will take over.
If you really want to block it, you'll have to write code to read the incoming url (i.e. the original url that came in), determine the version and if it's not in your list of versions, you raise an error (maybe return 404). This is basically what you'd do if you were offering a service built on GAE where each of your users had their own custom domain (version of your app) e.g. a blog hosting platform, an ecommerce site (like Shopify)

URL Map is not working with backend service on Load Balancer Google Cloud

I am setting up URL Maps on our Backend Services with Load Balancer. The issue is that my URL Maps are not working for some reason. When I tried to browse domain.com/path, it shows
The requested URL /bpd was not found on this server.
I do believe I did the correct way, but seems that its still not working.
Please see screenshot below:
[![image][1]][1]
/* - working
/path1 and /path2 - not working, shows error - was not found on this server
app.yaml file:
runtime: python27
api_version: 1
threadsafe: true
service:
handlers:
- url: /
static_files:
upload:
secure: always
redirect_http_response_code: 301
- url: /(.*)
static_files: www/\1
upload: www/(.*)
I also set-up Serverless network endpoint group and connected with Google App Engine for the Backend Services.
Thank you all for your help.
Thanks to your comment, I though I found the mistake. In fact, when you define a URL map in a load balancer, the query path in entry of the load balancer is, by default, forwarded as-is to the backend.
Let's take your case
you have this URL map: <URL>/address/*. Your URL path is /address/*
The backend is <myAddressAppEngine.appspot.com>. It received the request on this path <myAddressAppEngine.appspot.com>/address/*
And it doesn't work because in reality you expect <myAddressAppEngine.appspot.com>/*.
To solve that, you can use advanced mode in the URL map
Start by setting the default backends for any URL and any path
Then add a new path rule and configure it like this, with a path rewrite to /

Mapping a custom domain route to an application in App Engine

I have a custom domain with a route that I'd like to map to my React application (create-react-app production build being served on App engine). When I map the custom domain, none of my static files are found however. I've tried updating the handlers for the app but I'm getting the same error (404 on loading css, js, manifest). My dispatch.yaml is
dispatch:
- url: "example.service.org/test"
service: my-service
Accessing example.service.org/test correctly populates the page title but none of the assets load. How do I update my handlers to serve the files correctly?
If this is working in your appspot.com domain means that your static routes works fine.
I think that the issue is caused because your service doesn't know how to address this path
/test/static
I think that are multiple solutions to this issue:
1.- Map the domain with the service without /test
dispatch:
- url: "example.service.org"
service: my-service
with this workaround if you need a test url you can add an test subdomain in order to have 2 services one in prod and another in dev/test for example
dispatch:
- url: "example.service.org"
service: my-service
- url: "test.example.service.org"
service: test-my-service
2.- define in your app.yaml this handlers
handlers:
- url: /static
static_dir: static/
- url: /test/static
static_dir: static/

App Engine - subdomain pointing to particular service

I have two subdomains registered in my App Engine application:
service-a.my-app.com
service-b.my-app.com
I have added all the records (CNAME, A) on the server.
I have three services in my GAE:
default
service-a
service-b
And I want each subdomain to point to the correct service. However, each time I access them, only the default service is used.
Side note: the GAE is running a flexible environment for laravel 5.4 and my dispatch.yaml (located in default service is as follows:
dispatch:
-url: "service-a.my-app.com/*"
service: service-a
-url: "service-b.my-app.com/*"
service: service-b
This worked for me. Hope this helps someone.
GAE Standard:
I have an angular project which will load for any subdomain except one subdomain "api".
The backend is written in Go and all services are under a service named "api"
STEP1: Setting local env
Angular project has the following app.yaml
runtime: python27
api_version: 1
instance_class: F1
handlers:
- url: /
static_files: default/index.html
upload: default/index.html
- url: /
static_dir: default
My service.yaml file resides in a separate directory and has the following
runtime: go
api_version: go1
instance_class: F1
service: api
handlers:
- url: /.*
script: _go_app
secure: always
My dispatch.yaml has the following
dispatch:
- url: "api.MYDOMAINNAME.com/*"
service: api
//Add more subdomain : services mapping here
I deployed all these files using gcloud app deploy command
Step 2 - Configure Custom domains in GAE.
In GAE Console, goto Project Settings > Custom Domains
Add your domain
Verify your domainusing one of the methods provided by Google.
Update CNAME, A and AAA records in your domain service provider's DNS Settings
Step 3 - Configure Sub Domain
Add a subdomain api.MYDOMAINNAME.com
Add the CNAME in your domain service provider's settings.
// add more subdomains if required
Add a Wildcard subdomain *.MYDOMAINNAME.com
Add the CNAME in your domain service provider's settings to redirect * to google.
Finally:
Wait for few minutes for the settings to be applied.
Now your application will redirect MYDOMAINNAME.com, www.MYDOMAINNAME.com , *.MYDOMAINNAME.com to the Angular code
and
api.MYDOMAINNAME.com to your api service
Please note that dispatch.yaml is an app-level configuration, not a service-level one and occasionally updating the service containing it doesn't automatically update the app-level configs.
You should use the specific deployment commands for dispatch.yaml, executed from the directory containing the file:
gcloud app deploy dispatch.yaml if you're using the Cloud SDK
appcfg.py update_dispatch . if you're still using the GAE SDK
See also dispatch.yaml not getting updated.
The same is true for other app-level .yaml config files as well, which is probably one reason for each having its own update/deploy command (and also to allow deploying them independently of any particular app service. Somehow related: Why do I need to deploy a "default" app before I can deploy multiple services in GCP?
Actually the answer was really easy: You just need to map a wildcard subdomain and GAE would the use the service corresponding to the prefix.

Putting a Cloud Endpoints API in a separate App Engine module

I am developing an App Engine app and plan to also provide an API. I would like to separate this API from the main site, so I'm trying to use the "modules" feature to separate both apps. The main site would be the "default" module, and the API would lie in the "api" module. However, I'm having troubles with this.
Right now my main app's YAML file is like this:
application: my-app
module: default
runtime: python27
api_version: 1
...
handlers:
# Root handler
- url: /.*
script: main.app
secure: always
...
And the API module YAML file, like this:
application: my-app
module: api
runtime: python27
api_version: 1
handlers:
# Endpoints handler
- url: /_ah/spi/.*
script: api_main.app
secure: always
...
On the development server, the app is served on port 8000, and the API on port 7998.
With this configuration, my API doesn't work. Whenever I try to access it using localhost:7998/_ah/api/explorer, I don't get any result. If I try to run an API request manually, I get the following error: {"error": {"message": "BackendService.getApiConfigs Error"}}.
What's strange is I'm also seeing the following lines in the development server logs:
INFO 2014-06-15 18:00:32,368 module.py:639] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 500 -
INFO 2014-06-15 18:00:32,368 module.py:639] api: "GET /_ah/api/my-app/v1/events HTTP/1.1" 500 60
It seems like the API module is trying to POST data to the default module (as seen in the first line of logs).
Right now, the only workaround I found is to add the same handlers for /_ah/spi/.* in the default YAML file, but in this situation the separation between the main app and the API is not effective.
Can someone tell me if the configuration I'm trying to achieve is supported by Cloud Endpoints?
Thank you very much!
Same problem, I was able to make it work after may temptatives: the (only) way i found is to make the cloud endopoints module the default module. Then i have: on the dev server the two modules listening at different ports, you can see prot numbers on the log and on xxx.appspot.com: yourprojectid.appspot.com for the cloud endpoints and modulename-dot-yourprojectid.appspot.com for the other module
I had the same problem.
I solved using one file to publish my APIs. This file is indicated in app.yaml. I put my APIs in different files.
\
app.yaml
\apis
publish_api.py
\teacher
teacher_api.py
\student
student_api.py
**app.yaml:**
- url: /_ah/spi/.*
script: apis.publish_api.api
secure: always
**publish_api.py:**
import endpoints
from teacher.teacher_api import TeacherApi
from student.student_api import StudentApi
api = endpoints.api_server([TeacherApi, StudentApi])
**teacher_api.py:**
#endpoints.api( name='teacher',
version='v1',
allowed_client_ids=[WEB_CLIENT_ID, API_EXPLORER_CLIENT_ID],
scopes=[EMAIL_SCOPE])
class TeacherApi(remote.Service):
#endpoints.method(message_types.VoidMessage, StringMessage,
path='teacher', http_method='POST', name='writeTeacher')
**student_api.py:**
...
So, I get to mantain each file separately.

Resources