I'm creating an API in Python using FastAPI and trying to map a custom service to a path on my domain, the case is as follows:
Default Service: defaultapp.uc.r.appspot.com mapped to api.mydomain.com and working fine.
Second Service service2-dot-defaultapp.uc.r.appspot.com, I want to map it to api.mydomain.com/service2/
Default service dispatch.yaml
dispatch:
- url: "*/service2/*"
service: service2
- url: "defaultapp.uc.r.appspot.com/service2/*"
service: service2
- url: "api.mydomain.com/service2/*"
service: service2
service2 service2.yaml
runtime: python37
entrypoint: gunicorn -w 8 -k uvicorn.workers.UvicornWorker service2:app
service: service2
When trying to access service2 from service2-dot-defaultapp.uc.r.appspot.com it works fine,
but when I try to access it from defaultapp.uc.r.appspot.com/service2/ or api.mydomain.com/service2/ it gives me {"detail":"Not Found"},
I believe it tries to access /service2/ as a FastAPI path (example: #app.get('/service2/') from the default service, and not from dispatch.yaml,
EDIT:
I've changed the default service from an API to a static site so any path given like /service2/ won't be taken from the API but from the routes in the dispath.yaml file, but still the same issue when accessing defaultapp.uc.r.appspot.com/service2/ or api.mydomain.com/service2/ it gives me {"detail":"Not Found"}
any idea how can I make it work?
Related
I'm having an Google App Engine service residing at https://service-dot-myproject.uc.r.appspot.com/
However I want it to be reachable via https://app.mydomain.com
I have set up the DNS for mydomain.com on Cloudflare and added the custom domain mapping in app engine and set up a dispatch.yaml file
- url: "app.mydomain.com/*"
service: service
Is there a way to somehow map
https://service-dot-myproject.uc.r.appspot.com -> https://app.mydomain.com ?
currently https://app.mydomain.com only resolves to defaultservice
The following dispatch.yaml file resolved the issue
dispatch:
- url: "app.mydomain.com/*"
service: service
Also the file must be separatly deployed via:
gcloud app deploy dispatch.yaml
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!
I used to have an Nginx proxy that would do below :
location /api {
proxy_pass http://www.myapiexample.com;
}
Then if I made a request to my website at www.example.com/api, it would take me to http://www.myapiexample.com.
With the app engine, and I'm using Nodejs as my backend, how I can achieve the same?
Can't find anything in the docs
UPDATE : If I'm understanding correctly, I can't have my Nginx anymore, so I need to find a way to have the same proxying functionality with App Engine.
You can use dispatch.yaml configuration file if both of your web and api modules are on app engine. dispatch.yaml sends incoming requests to a specific service depending on the path or hostname in the URL.
dispatch:
# Default service serves simple hostname request.
- url: " example.com"
service: default
# Send all api traffic to the api backend.
- url: "*/api/*"
service: api-backend
You can read more about how to use dispatch file.
If your api module is not on app engine you can redirect requests on application level
dispatch:
# Send all api traffic to the api backend.
- url: "*/api/*"
script: router.py
You can write your routing code in router.py something like
class RouteHandler(webapp.RequestHandler):
def get(self):
self.redirect("http://www.myapiexample.com;", True)
I am using App Engine Flexible environment and I have multiple services deployed in my App Engine. So I connected multiple sub domains to my app engine. In order to assign one subdomain to each service, I defined dispatch.yaml file. Here is how it looks like
dispatch:
- url: "wscfg.xxxxxxxxx.com/"
service: default
- url: "onboarding.xxxxxxxxx.com/"
service: default
- url: "dtnote.xxxxxxxxx.com/"
service: default
- url: "careco.xxxxxxxxx.com/"
service: careco
- url: "userman.xxxxxxxxx.com/"
service: user-management
After deploying it to app engine, it shows services with the custom domains specified in dispatch.yaml file. So my dispatch file is working correctly.
The problem is when I am trying to access https://careco.xxxxxxxxx.com/, it is taking me to default service whereas https://user-management.xxxxxxxxx.com/ is taking me to userman service (which is correct).
The problem is with dispatch.yaml file. It needs to have wildcard at the end. So dispatch.yaml should look like
dispatch:
- url: "wscfg.xxxxxxxxx.com/*"
service: default
- url: "onboarding.xxxxxxxxx.com/*"
service: default
- url: "dtnote.xxxxxxxxx.com/*"
service: default
- url: "careco.xxxxxxxxx.com/*"
service: careco
- url: "userman.xxxxxxxxx.com/*"
service: user-management
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.