2

I'm the service url as GET REQUEST http://myipaddress:5000/api/Tenant/tenants/{TenantID}

The TenantID will be dynamic

I also have the POST as http://myipaddress:5000/api/Tenant/tenants

In this post request payload is passed in request body.

My gateway config yml file is as below

http:
  port: 8080
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  api:
    host: localhost
    paths: '/ip'
  tenant-api:
    host: localhost
    paths: '/api/Tenant/tenants/*'

serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  tenant-svc:
    url: 'http://localhost:5000'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - api
      - tenant-api
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: httpbin 
              changeOrigin: true
          - action:
              serviceEndpoint: tenant-svc 
              changeOrigin: true

I get 404 when i try to perform the GET Request via proxy. Also can you let me know how can i add the POST api endpoints in my gatewayconfig.yml

MAQ
  • 443
  • 7
  • 15

1 Answers1

3

The first issue is that you have multiple actions in proxy policy

- action:  # this one is always executing and goes to the httpbin
      serviceEndpoint: httpbin 
      changeOrigin: true
- action:
      serviceEndpoint: tenant-svc 
      changeOrigin: true

remove the first action to make all calls go to tenant-svc

- action:
      serviceEndpoint: tenant-svc 
      changeOrigin: true

This configuration will accept all methods GET,POST whatever on '/api/Tenant/tenants/*' urls

to make Express-Gateway process /api/Tenant/tenants url you can modify api endpoint like:

paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 

https://www.express-gateway.io/docs/configuration/gateway.config.yml/apiEndpoints#markdown

I would assume no special handling of GET POST is required. In case you need Gateway to filter only specific methods you can add

methods: 'POST,PUT' 

to your api Endpoint configuration

So final config can look like

http:
  port: 8080
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  tenant-api:
    host: localhost
    methods: 'GET,POST,PUT' 
    paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 

serviceEndpoints:
  tenant-svc:
    url: 'http://localhost:5000'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - tenant-api
    policies:
      - proxy:
          - action:
              serviceEndpoint: tenant-svc 
              changeOrigin: true

Or you can have multiple API Endpoints to the same pipeline with methods

  tenant-api-1:
    host: localhost
    methods: 'GET' 
    paths: '/api/Tenant/tenants/*'

  tenant-api-2:
    host: localhost
    methods: 'POST' 
    paths:  '/api/Tenant/tenants' 

Update: Multi service usage

http:
  port: 8080
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  tenant-api:
    host: localhost
    methods: 'GET,POST,PUT' 
    paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 
  product-api:
    host: localhost
    paths: ['/api/products/*'] 
serviceEndpoints:
  tenant-svc:
    url: 'http://localhost:5000'
  product-svc:
    url: 'http://localhost:6000'
policies:
  - proxy
pipelines:
  tenant:
    apiEndpoints:
      - tenant-api
    policies:
      - proxy:
          - action:
              serviceEndpoint: tenant-svc 
              changeOrigin: true
  products:
    apiEndpoints:
      - product-api
    policies:
      - proxy:
          - action:
              serviceEndpoint: product-svc 
              changeOrigin: true
Serhii Kuts
  • 429
  • 2
  • 9
  • So does it mean that I can only specify only one action. I want to use this solution for my microservices application. I've have more than 70 microservice , where each service is having atleast 3 endpoints. How can i have single config to accommodate all the services ? – MAQ Dec 15 '17 at 09:47
  • typically you create multiple pipelines for different and different api endpoints to match urls /tenants/* -> goes to "tenants" pipeline -> the pipeline has proxy policy to tenant-src endpoint /products/* -> goes to "products" pipeline -> the pipeline has proxy policy to products-src endpoint ---- multiple actions are possible. for example if you have condition so action will be executed only if condition is matched for the most use cases you need just one action – Serhii Kuts Dec 15 '17 at 12:53
  • I've updated the answer with example. Btw, when you want to see where exactly request is proxied run Express Gateway with env variable "LOG_LEVEL=debug" for example "LOG_LEVEL=debug npm start" it will output messages with target url if proxy policy is triggered – Serhii Kuts Dec 15 '17 at 12:57
  • Great that really helped me. Thanks – MAQ Dec 15 '17 at 13:16
  • so to route resource URLs to appropriate microservice I must declare an apiEndpoint, a serviceEndpoint and a pipeline with a proxy policy for each one? – Carmine Ingaldi Sep 10 '18 at 20:01