playframework, can you route on hostname/subdomains?

后端 未结 2 840
余生分开走
余生分开走 2021-01-01 06:59

We are porting a seam app(2 apps) to one play app to test out play right now(well, porting harder scenarios...so far, so good).

We would like to be able to route on

相关标签:
2条回答
  • 2021-01-01 07:14

    I think virtual hosting via the routes file is in play version 2.x not available anymore.

    But what you can do is preparing a controller for proxying such requests. Here is my sample code which is working in play 2.8.x:

    @Singleton
    class VirtualHostsController @Inject() (
      langs: Langs,
      ccs: ControllerComponents
    ) extends AbstractController(ccs) with I18nSupport {
    
      private val logger = Logger(classOf[VirtualHostsController])
    
      def index() = Action { implicit request =>
        logger.info(s"Handling Virtual Hosts for ${request.host}")
    
        request.host match {
          case "localhost:9000" => Redirect(routes.SomeController.show())
          case "sub.domain.tld" => Redirect(routes.OtherController.show())
          case _ => InternalServerError("unknown domain")
        }
      }
    
    }
    

    And in the routes file you can write something like that:

    GET     /                  controllers.VirtualHostsController.index()
    
    0 讨论(0)
  • 2021-01-01 07:15

    Some of the docs are more difficult to find for play. I couldn't find this in any of the real documentation but remembered it being part of a release...

    http://www.playframework.org/documentation/1.1/releasenotes-1.1#routeHost


    Virtual hosting in routes

    The routes file now supports Host matching. this can be useful if action parameters must be extracted from the host parameter. For example, for a SAAS application, you could use:

    GET    {client}.mysoftware.com/         Application.index
    

    and then automatically retrieve the client value as for any other request parameter:

    public static void index(String client) {
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题