peterldowns / localias

custom local domain aliases for local dev servers
MIT License
598 stars 3 forks source link

Proxy different paths on the same domain to different ports #39

Open jochemkeller opened 1 week ago

jochemkeller commented 1 week ago

Hi Peter,

First of all thanks again for a great tool! As mentioned before it's become essential to my workflow 🙏 Now than, a feature request, or maybe question, of sorts.

I've ran into a situation where I'd like to have a main domain linked to a port, but then have a unique sub-directory assigned to a different port. In terms of localias config, something like:

# localias config file syntax

domain.local: 3000
domain.local/subdirectory: 3001

Right now, this gives the following error:

error: loading new config: loading http app module: provision http: server srv0: setting up route handlers: route 0: loading handler modules: position 0: loading module 'map': http.handlers.map: invalid configuration: mapping 1 has a duplicate input 'domain.local' previously used with mapping 0

which is understandable, but could this potentially work? Or do you see any blockers?

Thanks beforehand for taking the time to review this issue!

peterldowns commented 1 week ago

Hey Jochem, nice to hear from you — glad to hear that you're still using and benefiting from Localias :)

Something like this is certainly possible. Caddy, which handles all the reverse proxying, supports proxying based on route matching. For the example you gave, I think the generated Caddy config would need to look something like:

domain.local {
  handle /subdirectory/* {
    reverse_proxy :3001
  }
  handle {
    reverse_proxy :3000
  }

  tls {
    issuer internal {
      ca local
      # allow on_demand issuing, but doesn't turn it on
      on_demand
    }
    # turn on on_demand issuing to automatically renew certs
    # when they expire
    on_demand
  }
}

Right now, the generated configs look like this:

domain.local {
  reverse_proxy :3000

  tls {
    issuer internal {
      ca local
      # allow on_demand issuing, but doesn't turn it on
      on_demand
    }
    # turn on on_demand issuing to automatically renew certs
    # when they expire
    on_demand
  }
}

I'll think about how to extend localias with this capability — I could benefit from it myself. I think the only tricky bits will be managing the routing logic and making sure that any errors or problems are understandable and debuggable for users.

Once the door is opened to path-based routing, I could also see people asking for more features that Caddy offers. At the limit, Localias could offer the ability to write a full Caddyfile! I don't want to go all the way down that road, but I'm willing to look at a few possible features that we could expose.

In summary, I'm open to adding this directory-based proxying capability. I'm curious to know if anyone else is interested in this feature or in any other advanced Caddy routing/matching capabilities. I'll leave this issue open until there's a clear path forward. Thank you for the question/suggestion/request.

jochemkeller commented 1 week ago

Hi Peter,

Awesome! Thanks for considering the feature request & the thorough response. Indeed, one of the things I really like about the .localias.yaml configuration file is that it's essentially an abstraction layer that takes away a lot of the complexity of Caddyfile config. This allows colleagues who don't have a lot of experience in that field to still achieve complex stuff 👍

If you require any further information on how I / we would use this at an enterprise-type scenario, please let me know, I'd be happy to provide more details :-)