Name-Based Virtual Hosting

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Name-Based Virtual Hosting

skurfuerst
Hey everybody,

I'm currently having the use-case that inside *one* Ratpack application, I'd like to respond to *multiple* hostnames; and have that hostname information available in the system. I'm building a cloud system where the hostname is the "username" at the end.

While digging through the code, I think I need to replace the "DefaultPublicAddress" class with a custom one which essentially returns "new URI('http://' + context.request.getHeaders().get('Host'))" as current hostname.

However, I am not able to write a handler which does exactly that -- I tried two approaches which can be found at https://gist.github.com/skurfuerst/f7621b3a0140083aee18 .

So, my question now: How can I replace the DefaultPublicAddress with a custom one? Any hints are greatly appreciated!

Thanks a lot,
Sebastian
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

Luke Daley
Administrator
Is this a Guice based Ratpack app? (if you’re using ‘ratpack-groovy’ then it implicitly is)
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

skurfuerst
Hey,

yep, it is!

Greets, Sebastian
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

Luke Daley
Administrator
Are you using ratpack.groovy? or a custom HandlerFactory?
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

skurfuerst
Hey,

I am basically using a custom HandlerFactory which just returns a GroovyClosureHandlerFactory. So I have control over the startup.

Greets, Sebastian
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

Luke Daley
Administrator
Ok, so all you have to do is bind an implementation of PublicAddress with Guice that has the logic you want. Ratpack will use your implementation instead of the default one.
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

skurfuerst
Hey,

thanks for your answer! While I understand generally what I am supposed to do, i am not yet able to put it into code :-) For perspective, this is my current bootstrap: https://gist.github.com/skurfuerst/94c565cf848a40895dca -- probably this is a little too complicated, but I did not find good documentation on how to get started more easily...

I'd really appreciate if you could provide me any hints on how to reach the guice configuration object from this point :-) Thanks in advance!

Greets, Sebastian
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

Luke Daley
Administrator
That’s a rather odd way to start an app.

This might give you some more ideas: https://gist.github.com/alkemist/f5b01de44b0bae8d5fd7
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

skurfuerst
Hey Luke,

your example helps me very much; I am now using this for the startup and it works flawlessly -- so a huge *thank you* for the quick and helpful responses! (and of course all the work on Ratpack ;) )

I had some more trouble on how to make Pac4j Authentication work in a Name-Based Hosting context; but I now got it working. I'll just put my solution to a Gist for reference, if anybody else stumbles over this issue. There, the exact problem is also explained: https://gist.github.com/skurfuerst/920da93be0db058a1034

So, thank you again, and keep up the great work!
Sebastian
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

Luke Daley
Administrator
Hi Sebastian,

Thanks for posting back. I’m glad you had some success. It’s also cool that you were able to work around the issue in the pac4j stuff. One of my goals with Ratpack is to have things loosely coupled enough that such workarounds are possible. 

I’d be interested to learn more about what you are doing with Ratpack if you are able to share more information.

I’ll also ask the guys who know more about the pac4j stuff to have a look into this issue.
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

skurfuerst
Hi Luke,
Luke Daley wrote
I’d be interested to learn more about what you are doing with Ratpack if you are able to share more information.
Sure, basically it's quite simple -- I am building a single-page web application (Ember.js based).
I'm a huge fan of 12-factor apps (http://12factor.net) and Ratpack fits in there beautifully -- providing
 the API endpoints as well as the HTML, JavaScript etc.

Especially important for us is the quick startup time of Ratpack, as we have a custom "runner" which
 interprets the groovy application on the fly; allowing code changes to be picked up in well under one
second; and basically completely "rebooting" the web application on every code change.

Concerning Ratpack, we're using it twice in our application on different ports; one for internal
communication with custom SSL certificates, and one publicly-accessible. That all works really nicely!

I have done a few deep-dives into the Ratpack source code; as far as I got it, it's really well-written
 and "beautifully structured", and contains lots of inline documentation (once you find it). Still, I
sometimes have a hard time understanding the underlying high-level concepts. But it's getting better
all the time :)

Soon, I'll be developing an administrative interface based on Ratpack; where I'll probably do a
"normal" CRUD application with templates etc. I'll look into Scaffolding etc at that point in time
 I guess.

The source code is currently closed-source, but if anybody would be interested in some parts
of what I described above, it's not a problem to post snippets or code excerpts for the described
parts.

Luke Daley wrote
I’ll also ask the guys who know more about the pac4j stuff to have a look into this issue.
Cool, that is awesome. I think my "workaround" might actually be a missing pac4j feature
or a bug, depending on how you see that.

Greets,
Sebastian
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

Luke Daley
Administrator
Thanks for sharing.

I didn’t know about 12factor. Very interesting stuff.

WRT the “inline documentation” are you referring to the Javadoc? If so, the aim is for this to be the primary documentation. We have a lot of work to do though here.

I’ve refrained from too much high level documentation to date because a lot of concepts were in flux. This has stabilised though and I’ll be working on such docs soon as part of the manual.

It’s more than likely we’ll make changes to the pac4j stuff so your workaround won’t be necessary, Actually, if you could open an issue describing the problem that would be very helpful.
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

davidmc24
In reply to this post by skurfuerst
Your analysis looks sound.  I tend to agree, that the (seemingly intended) requirement of a constant callback URL to use Clients is a current deficiency in pac4j.  I recommend opening an issue on the pac4j issue tracker and discussing it there.  The maintainer has seemed quite open to making enhancements that help potential users.

https://github.com/leleuj/pac4j

As for the Ratpack PublicAddress stuff, I think that we should consider taking request header information into account in the default behavior of DefaultPublicAddress.
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

Luke Daley
Administrator
> As for the Ratpack PublicAddress stuff, I think that we should consider taking request header information into account in the default behavior of DefaultPublicAddress.

I’m open to this.
Reply | Threaded
Open this post in threaded view
|

Re: Name-Based Virtual Hosting

davidmc24
To close the loop on this, I submitted the following GitHub issue for enhancing Ratpack's default PublicAddress behavior.  I expect that the changes will make it into version 0.9.8.

https://github.com/ratpack/ratpack/issues/403