pnxtech / hydra-router

A service aware router for Hydra Services. Implements an API Gateway and can route web socket messages.
MIT License
60 stars 26 forks source link

How should the root URL be handled? #77

Closed mozrat closed 7 years ago

mozrat commented 7 years ago

Having a confusing experience with hydra-router and 2 microservices (service-auth and service-ui)

root@89b2d7b901f2:/# hydra-cli nodes 
[
  {
    "serviceName": "ui-service",
    "serviceDescription": "User Interface services",
    "version": "0.0.1",
    "instanceID": "64cda9caf15e23d23fe7adabe3e96d43",
    "updatedOn": "2017-04-04T22:20:53.551Z",
    "processID": 15,
    "ip": "172.20.0.6",
    "port": 8080,
    "elapsed": 1
  },
  {
    "serviceName": "auth-service",
    "serviceDescription": "Authenticate users",
    "version": "0.0.1",
    "instanceID": "801f6d914aca66e28672e4ae1e321884",
    "updatedOn": "2017-04-04T22:20:53.204Z",
    "processID": 15,
    "ip": "172.20.0.5",
    "port": 8080,
    "elapsed": 2
  },
  {
    "serviceName": "hydra-router",
    "serviceDescription": "Service Router",
    "version": "1.0.12",
    "instanceID": "b8a32ef20fdfd3c2ea6c3882153dbe36",
    "updatedOn": "2017-04-04T22:20:52.711Z",
    "processID": 15,
    "ip": "172.20.0.8",
    "port": 5353,
    "elapsed": 2
  }
]

root@89b2d7b901f2:/# hydra-cli routes
{
  "hydra-router": [
    "[get]/v1/router/list/:thing",
    "[get]/v1/router/refresh/:service",
    "[get]/v1/router/refresh",
    "[get]/v1/router/version",
    "[post]/v1/router/message"
  ],
  "ui-service": [
    "[GET]/_config/ui-service",
    "[get]//"
  ],
  "auth-service": [
    "[get]/auth/login",
    "[get]/auth/",
    "[GET]/_config/auth-service"
  ]
}

With my Docker configuration I have access to each microservice on a port that bypasses the router and I can connect to each route

GET / HTTP/1.1
Cookie: sails.sid=s%3Ak7x4QABY5ecethGYCT91rOV6sAMggOlL.WkVy9oeGQySmnwm6LifA7DjwWptabtypaLlzsNp0SRQ
Host: 192.168.99.100:8082
Connection: close
User-Agent: Paw/3.0.16 (Macintosh; OS X/10.10.4) GCDHTTPRequest

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
X-Process-Id: 15
X-DNS-Prefetch-Control: off
X-Frame-Options: SAMEORIGIN
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Powered-By: ui-service/0.0.1
Content-Type: application/json
Content-Length: 146
X-Response-Time: 1.983ms
Date: Tue, 04 Apr 2017 22:23:24 GMT
Connection: close

{"statusCode":200,"statusMessage":"OK","statusDescription":"Request succeeded without error","result":{"greeting":"Welcome to Hydra Express! UI"}}
GET /auth HTTP/1.1
Cookie: sails.sid=s%3Ak7x4QABY5ecethGYCT91rOV6sAMggOlL.WkVy9oeGQySmnwm6LifA7DjwWptabtypaLlzsNp0SRQ
Host: 192.168.99.100:8081
Connection: close
User-Agent: Paw/3.0.16 (Macintosh; OS X/10.10.4) GCDHTTPRequest

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
X-Process-Id: 15
X-DNS-Prefetch-Control: off
X-Frame-Options: SAMEORIGIN
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Powered-By: auth-service/0.0.1
Content-Type: application/json
Content-Length: 148
X-Response-Time: 1.735ms
Date: Tue, 04 Apr 2017 22:24:39 GMT
Connection: close

{"statusCode":200,"statusMessage":"OK","statusDescription":"Request succeeded without error","result":{"greeting":"Welcome to Hydra Express! AUTH"}}
GET /auth/login HTTP/1.1
Cookie: sails.sid=s%3Ak7x4QABY5ecethGYCT91rOV6sAMggOlL.WkVy9oeGQySmnwm6LifA7DjwWptabtypaLlzsNp0SRQ
Host: 192.168.99.100:8081
Connection: close
User-Agent: Paw/3.0.16 (Macintosh; OS X/10.10.4) GCDHTTPRequest

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
X-Process-Id: 15
X-DNS-Prefetch-Control: off
X-Frame-Options: SAMEORIGIN
X-Download-Options: noopen
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Powered-By: auth-service/0.0.1
Content-Type: application/json
Content-Length: 132
X-Response-Time: 0.874ms
Date: Tue, 04 Apr 2017 22:25:34 GMT
Connection: close

{"statusCode":200,"statusMessage":"OK","statusDescription":"Request succeeded without error","result":{"greeting":"Login the user"}}

But connecting through the hydra-router I am having no success

GET / HTTP/1.1
Cookie: sails.sid=s%3Ak7x4QABY5ecethGYCT91rOV6sAMggOlL.WkVy9oeGQySmnwm6LifA7DjwWptabtypaLlzsNp0SRQ
Host: 192.168.99.100:81
Connection: close
User-Agent: Paw/3.0.16 (Macintosh; OS X/10.10.4) GCDHTTPRequest

HTTP/1.1 302 Found
Location: 
Date: Tue, 04 Apr 2017 22:26:26 GMT
Connection: close
Transfer-Encoding: chunked
GET /auth HTTP/1.1
Cookie: sails.sid=s%3Ak7x4QABY5ecethGYCT91rOV6sAMggOlL.WkVy9oeGQySmnwm6LifA7DjwWptabtypaLlzsNp0SRQ
Host: 192.168.99.100:81
Connection: close
User-Agent: Paw/3.0.16 (Macintosh; OS X/10.10.4) GCDHTTPRequest

HTTP/1.1 404 Not Found
Content-Type: application/json
Access-Control-Allow-Origin: *
Content-Length: 131
Date: Tue, 04 Apr 2017 22:27:19 GMT
Connection: close

{"statusCode":404,"statusMessage":"Not Found","statusDescription":"The requested resource was not found on the server","result":{}}

So a HTTP status 302 and 404 for the routes - how can I debug this further with hydra-cli?

Thanks for an awesome project

cjus commented 7 years ago

There is an issue where hydra-router expects that the root path on a service is actually an embedded website! This is really useful when you want to offer an admin interface to a service for example. We'd have to look into this further to fix that behavior and allow the root route of a service to be accessed normally.

cjus commented 7 years ago

PRs are welcomed ;-) The easiest work-around is not to use root routes for your services. I know this isn't ideal - we'll definitely look into this closer in the next day or so.

mozrat commented 7 years ago

@cjus Thanks - I understand why an embedded website on the root is a good idea, and that would work for my use case.

However, I'm not sure how to proceed. I modified my microservice so that the root URL is hooked up to this controller

api.get('/',
(req, res) => {
  res.render('index', { title: 'Hey', message: 'Hello there!' });
});

and that works if I connect directly to the service, but not through the router.

Can you share an example of an embedded website?

cjus commented 7 years ago

@mozrat I should have a PR for this today. But to answer your questions above you just need to create a public folder and drop in an HTML page.

cjus commented 7 years ago

@mozrat Try the latest Hydra-router. Just updated 1.0.13.

cjus commented 7 years ago

@mozrat closing this one. Definitely ping me if there's still an issue.