typicode / hotel

🏩 A simple process manager for developers. Start apps from your browser and access them using local domains
MIT License
9.98k stars 426 forks source link

Support path-based routing #331

Open kamal opened 5 years ago

kamal commented 5 years ago

I have a top-level domain that fronts several services. For example:

Currently, the rewrite above is handled by Cloudflare. I'd like to emulate this setup in Hotel so that my local environment is similar to production. Is there somewhere in Hotel where I can perform the rewrite? My other option is to stand up a standalone rewriting proxy that sits on api.example.localhost and routes it to the other hosts.

bbtx0 commented 5 years ago

@kamal Did you find a solution for this scenario? I am interested as well

kamal commented 5 years ago

@gabrieleds I ended up writing a proxy with node-http-proxy and running it with hotel alongside the other apps. The proxy itself looks something like this:

const rules = [
  {
    prefix: "/a/billing",
    host: "billing.envoy.dev"
  },
  {
    prefix: "/a/users",
    host: "users.envoy.dev"
  }
];

proxy.on("proxyReq", (proxyReq, req, res, options) => {
  const fullUrl = `https://app.envoy.dev${req.url}`;

  rules.some(({ prefix, host }) => {
    if (req.url.startsWith(prefix)) {
      logger.info(`Rewriting Host to ${host}: ${req.method} ${fullUrl}`);
      proxyReq.setHeader("Host", host);

      return true;
    }
  });
});