4

Not sure if the title is well suited but I am having three apps (static content) at the moment:

  • public
  • app
  • admin

public is just content which is just for the regular web, app is content which is rendered for mobile devices and admin is basically a tool for administrative stuff. They are all contained in the same Spring Boot application and talk to the REST API at example.com/api. Technically, I could just place them into resources/static such that

resources/static/public
resources/static/admin
resources/static/app

which would allow me to access the apps as such:

example.com/public/index.html
example.com/admin/index.html
example.com/app/index.html

However, my goal is to have the following structure:

      example.com     // For public
admin.example.com     // For admin
  app.example.com     // For app
      example.com/api // REST API

How can this be achieved or what can I do to make this possible?

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • customize spring web context and use multiple dispatcher servlets https://www.baeldung.com/spring-web-contexts – Ryuzaki L Nov 03 '18 at 21:46

2 Answers2

2

It sounds like you have three systems: public, admin and app sharing a common REST api. Probably the best approach would be to serve the static resources for each of the three systems using a content delivery network (CDN) like AWS CloudFront or Google Cloud CDN.

Another approach, though less desirable, would be to use a proxy to redirect app requests (for example https://admin.example.com to https://internal-spring-boot-server/admin). You could use Apache mod_proxy with reverse proxy (see https://httpd.apache.org/docs/2.4/mod/mod_proxy.html), or NGNX reverse proxy https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

Jean Marois
  • 1,510
  • 11
  • 19
  • Hi! Right now I am looking for the simplest possible solution. I have never worked with a CDN. Right now I am deploying on heroku, not sure if that information is relevant though. The proxy solution seems rather simple at first glance - I wouldn't know how to apply this to my Spring application though. In case you have more details I'd be glad to take a look. – Stefan Falk Nov 04 '18 at 07:22
  • I'm not familiar with Heroku but there is an addon called Edge (https://devcenter.heroku.com/articles/edge) for using Amazon's CloudFront CDN. There are probably other addons for other CDNs. – Jean Marois Nov 05 '18 at 13:22
  • With Heroku's HTTP Routing (https://devcenter.heroku.com/articles/http-routing) you may be able to setup multiple app entry points pointing to the same Spring Boot app, but I don't know anything about how Heroku works. – Jean Marois Nov 05 '18 at 13:28
  • This may be useful https://devcenter.heroku.com/articles/custom-domains – Jean Marois Nov 05 '18 at 13:29
  • Just out of curiosity, wouldn't it be a better UX to have a single app with the admin and non-public parts only presented via menus/links to authorized users with appropriate permissions? – Jean Marois Nov 05 '18 at 13:35
  • I think my problem is the my DNS host (https://www.ovh.com) - it would seem that the root is not SSL secured (to begin with) but I also don't know how to make the root-domain point to Heroku. From what I're seen so far is that this is not really possible. There has to be an `A record` which is a IPv4 address which points to a provided IP. Regarding the better UX: Not really - in my case. I am providing an administrative tool which is an application on it's own. The other is really just a web app which gets embedded for mobile devices. The rest is just "normal" web .. – Stefan Falk Nov 05 '18 at 14:17
1

I think that there are two ways to achieve what you are trying to do you.

Either you will configure your subdomains in your DNS plesk/cpanel/.. to look a specific Document root (The path to the each of your apps home directory.) for example when someone asks for example.com the request would ask for example.com/main, when someone asks for admin.example.com the request would ask for admin.example.com/admin etc. In this way, you essentially ignoring the subdomain in your Spring app and you manipulate the routes in the @Controller level using the RequestMapping values main/admin/etc..

Alternatively if you don't want to mess with the Provider's control panel, you could follow the steps of this answer. But then you'd have to implement these three custom RequestCondition classes and it may not be the most simple way to go.

Not the best documented answer but I hope I pointed you to the right direction.

Christos Karapapas
  • 1,018
  • 3
  • 19
  • 40
  • Hm, I wouldn't know how to do that first solution. I can imagine that a redirect would work e.g. `admin.example.com` redirects to `example.com/admin` but I'd actually like to hide this completely. I'm not even sure if that's how one does it in general. One solution that would definitely work is to deploy all apps an the server separately and configure my DNS settings s.t everyting points to the correct apps. The only thing is that this means I'd need 4 dynos (instances on heroku) which just costs too much at this point. – Stefan Falk Nov 04 '18 at 07:51
  • Yes the "legit" way I guess it would be to split into four different Spring Boot apps, with the economic consequences you just mentioned. The general idea on how to do it is to "redirect a subdomain to a subdirectory" and the subdirectory will play the role of RequestMapping value. This redirection is done in the .htaccess file. Take a look here for an example https://stackoverflow.com/questions/8481005/how-to-redirect-from-a-subdomain-to-a-subfolder-using-htaccess But depending on you DNS provider there might also be some gui to configure this. – Christos Karapapas Nov 04 '18 at 08:03
  • Alright I'll have to take a look into that but I right now I don't have a clue how I can do that to Spring's embedded Tomcat - I guess that's where I have to make that change. – Stefan Falk Nov 04 '18 at 08:08
  • Yeah that is true. You'd need to make the subdomain configurations to tomcat. The good thing is, since it's a boot application wherever you deploy you'll take that configured tomcat with you. – Christos Karapapas Nov 04 '18 at 08:37