0

I have 2 services and I want to create an API gateway for these services.

Service 1

  • name : Leon
  • host : api.leon.com

endpoints:

  /
  /jungle

Service 2

  • name : Tiger
  • host : api.tiger.com

endpoints:

 /
 /jungle

Expected Behavior:

API Gateway handle a request as :

apigateway.com/leon          =======>> Leon service '/'
apigateway.com/leon/jungle   =======>> Leon service '/jungle'
apigateway.com/tiger         =======>> Tiger service '/'
apigateway.com/tiger/jungle  =======>> Tiger service '/jungle'

So I want to match the first path of the incoming request URL with services.

And my gateway. config.yaml file like that:

http:
  port: 8080
admin:
  port: 9876
  host: localhost
apiEndpoints:
  leon:
    host: localhost
    methods : [ 'POST', 'GET' ]
    paths: '/*'
  tiger:
    host: localhost
    methods : ['POST', 'GET']
    paths: '/*'
serviceEndpoints:
  leonsrv:
    url: 'http://leon-service.us-west-2.compute.amazonaws.com/'
  tigersrv:
    url: 'http://tiger-service.us-west-2.compute.amazonaws.com/'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  - name: leon
    apiEndpoints:
      - leon
    policies:
      - proxy:
          - action:
              serviceEndpoint: leonsrv
              changeOrigin: true
  - name: tiger
    apiEndpoints:
      - tiger
    policies:
      - proxy:
          - action:
              serviceEndpoint: tigersrv
              changeOrigin: true

But Its not working. I cannot send a request to tiger service.

How Can solve this problem please help!

akasaa
  • 1,282
  • 4
  • 13
  • 33

1 Answers1

0

The two endpoints defined in your apiEndpoints section are the same: http://localhost/*. You need to include more of your target URLs to get allow express-gateway to distinguish between them:

apiEndpoints:
  leon:
    host: localhost
    methods : [ 'POST', 'GET' ]
    paths: [ '/leon/', '/leon/*' ]
  tiger:
    host: localhost
    methods : ['POST', 'GET']
    paths: [ '/tiger/', '/tiger/*' ]
James McLeod
  • 2,381
  • 1
  • 17
  • 19
  • Does gateway forward requests or redirect them? What addresses should I see in browser? Tiger and Leon? Or just apigateway.com? – zolty13 Apr 05 '22 at 15:51
  • @zolty13 You would enter an address http://localhost/leon/ - The API gateway forwards the request to the defined service, potentially modifying it, adding authentication info, etc – James McLeod Apr 05 '22 at 15:56
  • I have some problem because my GW return redirect http 307 and I land at backend address e.g. 'http://leon-service.us-west-2.compute.amazonaws.com/' instead of apigateway.com. – zolty13 Apr 05 '22 at 15:58
  • 1
    This should be a new question with your full configs and a description of the hostnames involved. – James McLeod Apr 05 '22 at 15:59
  • I will send you link to my question https://stackoverflow.com/questions/71755121/express-gateway-return-redirect-307-instead-of-forward-requests – zolty13 Apr 05 '22 at 16:11